Jeditable 和 jQuery UI Datepicker onblur 取消不适用于 showOn 按钮选项

发布于 2024-12-16 15:48:00 字数 2541 浏览 5 评论 0原文

我已将 datepicker 配置为激活,如下所示

$(function () {
    $(".editable_datepicker").click(function (event) {
        $(this).editable('ajax_save.php',{
            id          : 'id',
            type        : 'datepicker',
            style       : 'margin:0px;width:80%;display:inline',
            onblur      : 'submit',
            tooltip     : ''
        });
    });
});

我正在使用此插件

源: https://github.com/qertoip/jeditable-datepicker/blob/master/src/jquery.jeditable.datepicker.js

jQuery.expr[':'].focus = function( elem ) {
  return elem === document.activeElement && ( elem.type || elem.href );
};

$.editable.addInputType( 'datepicker', {
    element: function( settings, original ) {
      var form = $( this ),
          input = $( '<input />' );
      input.attr( 'autocomplete','off' );
      form.append( input );
      settings.onblur = 'nothing';
      return input;
    },
    plugin: function( settings, original ) {
        var form = this, input = form.find( "input" );
        settings.onblur = 'nothing';
        input.datepicker( {
        dateFormat:'yy-mm-dd', 
        showOn: "button",
        buttonImage: "img/calendar_icon.gif",
        buttonImageOnly: true, 
        onSelect: function() {
            form.submit();
        },
        onClose: function() {
            setTimeout( function() {
                if ( !input.is( ':focus' ) ) {
                  original.reset( form );
                } else {
                  form.submit();
                }
                }, 150 );
            }
        } );
    }
} );

该插件成功运行只要我不在这个插件中添加以下选项

    showOn: "button",
    buttonImage: "img/calendar_icon.gif",
    buttonImageOnly: true, 

我想实现这一点(下面有详细说明)

  • 双击字段,
  • 通过编辑或从日期选择器中选择新日期来加载输入类型日期选择器更改数据
  • 如果用户激活了可编辑插件,则通过从日历中选择日期来提交数据
  • ,除非我打开日历,否则 onblur:cancel 不起作用(如果用户单击其他地方,我想取消编辑)

,但我被卡住了。你能帮助我吗?

稍后编辑

重现问题的步骤

- 获取 jquery、jquery-ui、jeditable 以及附加的或来自 github 的插件代码 - 在相应文件中添加引用的脚本 -编写一个示例页面如下

<input type='editable_datepicker' value='2011-11-17'>

*当单击它时,它应该打开文本框和一个漂亮的日历图标

如果您单击该图标,则会出现日历,

您可以提交日期并发送数据 或者点击其他地方(失去焦点)并且不发送数据

问题就在这里...

如果您激活该字段(使其可编辑),则无法使其消失(失去焦点)除非你激活日历

非常感谢。

I have configured datepicker to activate as following

$(function () {
    $(".editable_datepicker").click(function (event) {
        $(this).editable('ajax_save.php',{
            id          : 'id',
            type        : 'datepicker',
            style       : 'margin:0px;width:80%;display:inline',
            onblur      : 'submit',
            tooltip     : ''
        });
    });
});

I'm using this plugin

source : https://github.com/qertoip/jeditable-datepicker/blob/master/src/jquery.jeditable.datepicker.js

jQuery.expr[':'].focus = function( elem ) {
  return elem === document.activeElement && ( elem.type || elem.href );
};

$.editable.addInputType( 'datepicker', {
    element: function( settings, original ) {
      var form = $( this ),
          input = $( '<input />' );
      input.attr( 'autocomplete','off' );
      form.append( input );
      settings.onblur = 'nothing';
      return input;
    },
    plugin: function( settings, original ) {
        var form = this, input = form.find( "input" );
        settings.onblur = 'nothing';
        input.datepicker( {
        dateFormat:'yy-mm-dd', 
        showOn: "button",
        buttonImage: "img/calendar_icon.gif",
        buttonImageOnly: true, 
        onSelect: function() {
            form.submit();
        },
        onClose: function() {
            setTimeout( function() {
                if ( !input.is( ':focus' ) ) {
                  original.reset( form );
                } else {
                  form.submit();
                }
                }, 150 );
            }
        } );
    }
} );

This plugin successfully works as long as I don't add the following options in this plugin

    showOn: "button",
    buttonImage: "img/calendar_icon.gif",
    buttonImageOnly: true, 

And I'd like to achieve this (detailed explanation below)

  • double click on field, load the input type datepicker
  • change data either by editing it or selecting a new date from datepicker
  • submit data by selecting a date from the calendar
  • if an user activated the editable plugin, onblur:cancel doesn't work unless I open the calendar (i'd like to cancel editing if users clicks somewhere else)

But i'm stuck. Can you help me?

LATER EDIT

Steps to reproduce the problem

-get jquery, jquery-ui, jeditable and the plugin code attached or from github
-add the quoted scripts in according files
-write an example page as following

<input type='editable_datepicker' value='2011-11-17'>

*when clicking on it, it should open the textbox and a nice calendar icon

if you click on the icon, the calendar appears

you can either submit a date, and send data
or click somewhere else (lose focus) and don't send data

the problem is here...

if you activate the field (make it editable), you can't make it disappear (lose focus) unless you activate the calendar

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2024-12-23 15:48:00

我找到了一个我认为可以满足您要求的解决方案。与其尝试解释与您提供的示例的所有差异,不如向您展示我所做的事情的独立示例会更容易。

该示例使用 jquery、jquery-ui 和 jeditable。我没有使用 jquery.jeditable.datepicker.js,而只是使用 $.editable 的 addInputType 方法:

$(function () {
    $.editable.addInputType('datepicker', {
        element: function(settings, original) {
            var input = $('<input />');
            input.attr('autocomplete', 'off');
            $(this).append(input);

            // rather than assigning plugin separately, I'm just adding it here
            //     (but doing it this way isn't critical to this solution)
            input.datepicker({
                dateFormat:'yy-mm-dd', 
                showOn: "button",
                buttonImage: "calendar_icon.gif",
                buttonImageOnly: true,
                beforeShow: function() {
                    var editable_datepicker = $(this).parent().parent().get(0);
                    // This is the heart of the solution:
                    editable_datepicker.editing = false;
                },
                onClose: function(a, b) {
                    var form = $(this).parent();
                    form.submit();
                }
            });           
            return input;
        }
    });
});

此解决方案的关键在于 $.editable 的私有重置方法如何工作。如果原始父元素的 javascript 键“editing”设置为 false,则不会重置(取消)输入。我只是使用 .datepicker 的 beforeShow 来设置它。

以下是此示例的剩余代码:

$(function() {
    $(".editable_datepicker").editable('ajax_save.php',{
        id          : 'id',
        type        : 'datepicker',
        style       : 'margin:0px;width:80%;display:inline',
        onblur      : 'cancel', // use 'cancel'!
        tooltip     : 'Double click to edit',
        event       : 'dblclick'
    });
});

和 HTML:

<span class='editable_datepicker'>01/01/2012</span>

希望有所帮助。干杯。

I found a solution that I believe meets your requirements. Rather than try to explain all the differences from the example you provided, it would be much easier to instead show you a standalone example of what I did.

The example uses jquery, jquery-ui, and jeditable. I'm not using jquery.jeditable.datepicker.js, but rather just the addInputType method of $.editable:

$(function () {
    $.editable.addInputType('datepicker', {
        element: function(settings, original) {
            var input = $('<input />');
            input.attr('autocomplete', 'off');
            $(this).append(input);

            // rather than assigning plugin separately, I'm just adding it here
            //     (but doing it this way isn't critical to this solution)
            input.datepicker({
                dateFormat:'yy-mm-dd', 
                showOn: "button",
                buttonImage: "calendar_icon.gif",
                buttonImageOnly: true,
                beforeShow: function() {
                    var editable_datepicker = $(this).parent().parent().get(0);
                    // This is the heart of the solution:
                    editable_datepicker.editing = false;
                },
                onClose: function(a, b) {
                    var form = $(this).parent();
                    form.submit();
                }
            });           
            return input;
        }
    });
});

The key to this solution lies in how the private reset method of $.editable works. It will not reset (cancel) the input if the original parent element's javascript key "editing" is set to false. I just used .datepicker's beforeShow to set it as such.

And here's the remaining code for this example:

$(function() {
    $(".editable_datepicker").editable('ajax_save.php',{
        id          : 'id',
        type        : 'datepicker',
        style       : 'margin:0px;width:80%;display:inline',
        onblur      : 'cancel', // use 'cancel'!
        tooltip     : 'Double click to edit',
        event       : 'dblclick'
    });
});

And the HTML:

<span class='editable_datepicker'>01/01/2012</span>

Hope that helps. Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文