简体   繁体   English

使用jQuery UI日期选择器进行内联编辑

[英]Inline Edit with jQuery UI date picker

I am trying to implement inline edit for the date field (use jQuery UI date picker) 我正在尝试对日期字段实施内联编辑(使用jQuery UI日期选择器)

The code I am using is as below; 我使用的代码如下:

$(document).on("click",".editableDateTxt", function () {
    var currElmModelId = $(this).attr('data-model-id');
    var currElmModelAttr = $(this).attr('data-model-attr');
    var input = $('<input />', {'type': 'text','name':currElmModelAttr, 'style':'width:100px' ,'data-model-id': currElmModelId, 'data-model-attr': currElmModelAttr, 'class':'datePicker', 'value': $(this).html()});
    var parent = $(this).parent();
    parent.append(input);
    $(this).remove();

    input.datepicker().focus();

});

$(document).on("change", ".datePicker", function () {
    //alert($(this).val());
    var dataValid = $(this).attr('data-valid');
    if (dataValid == "Y") {
        var currElmModelId = $(this).attr('data-model-id');
        var currElmModelAttr = $(this).attr('data-model-attr');
        var divEle = $('<div />', {'class': 'editableDateTxt','name':currElmModelAttr, 'data-model-attr':currElmModelAttr,'data-model-id':currElmModelId,'html':$(this).val()});
        var parent = $(this).parent();
        parent.append(divEle);
        $(this).remove();
    }
});

$(document).on("blur",".datePicker", function () {
    if (this.hasAttribute('data-model-id')) {
        var dataValid = $(this).attr('data-valid');

        if (typeof dataValid == "undefined" || dataValid == "Y") {
            var currElmModelId = $(this).attr('data-model-id');
            var currElmModelAttr = $(this).attr('data-model-attr');
            var divEle = $('<div />', {'class': 'editableDateTxt','name':currElmModelAttr, 'data-model-attr':currElmModelAttr,'data-model-id':currElmModelId,'html':$(this).val()});
            var parent = $(this).parent();
            parent.append(divEle);
            $(this).remove();
            return false;
        }

    }   
});

Now when I select any date or do blur, I get the following error; 现在,当我选择任何日期或模糊时,会出现以下错误;

Missing instance data for this datepicker 该日期选择器缺少实例数据

I think the issue is related to the jQuery UI datepicker using data() So I have tried using detach() instead of remove()... 我认为这个问题与使用data()的jQuery UI datepicker有关,所以我尝试使用detach()而不是remove()...

So I just used $(this).detach(); 所以我只是用$(this).detach();

Could you guide me on the correct way of using detach() which might fix the issue... 您能否指导我使用detach()的正确方法来解决问题...

You are probably having the same problem I was having. 您可能遇到了与我相同的问题。 The code in your onblur is firing before the datepicker is completing its work so you are losing the input object before datepicker is done with it. 在日期选择器完成其工作之前,onblur中的代码将触发,因此在完成日期选择器之前您将丢失输入对象。

I was able to resolve this with one of the suggestions in the accepted answer on this question: https://stackoverflow.com/a/1814308/3434790 我可以通过对此问题的可接受答案中的一项建议来解决此问题: https : //stackoverflow.com/a/1814308/3434790

The solution that worked in my scenario was to wrap my code inside my on blur function in a setTimeout which effectively made it so that code ran after the datepicker was done doing its thing. 在我的方案中起作用的解决方案是将我的代码包装在setTimeout中的on模糊函数内,该方法可以有效地使代码在datepicker完成其工作后运行。

In your case I would suggest the following: 对于您的情况,我建议以下几点:

$(document).on("blur",".datePicker", function () {
setTimeout(function(){
    if (this.hasAttribute('data-model-id')) {
        var dataValid = $(this).attr('data-valid');

        if (typeof dataValid == "undefined" || dataValid == "Y") {
            var currElmModelId = $(this).attr('data-model-id');
            var currElmModelAttr = $(this).attr('data-model-attr');
            var divEle = $('<div />', {'class': 'editableDateTxt','name':currElmModelAttr, 'data-model-attr':currElmModelAttr,'data-model-id':currElmModelId,'html':$(this).val()});
            var parent = $(this).parent();
            parent.append(divEle);
            $(this).remove();
            return false;
        }
    }   
},100);

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM