jQuery jeditable 尝试使用日期时间选取器时无法读取未定义的属性'plugin'



我继承了一段代码,它最初只需要一个可编辑的日期,这是使用jquery datatables,jquery-editable和datepicker实现的。

这是仅日期的当前工作代码

$('.editdate')
    .editable(function (value, settings) {
    if ((value == null) || (value == '')) {
        return ($(this)
            .parent()
            .find('label')
            .text());
    }
    hasBeenEdited(this);
    return (value);
}, {
    type: 'datepicker',
    datepicker: {
        dateFormat: 'mm-dd-yy',
        changeMonth: true,
        changeYear: true
    }
});

现在我被要求在日期中包含时间(服务器端的数据库和模型已更改为时间戳),并且我正在尝试合并日期时间选择器插件。

当我使用$(".editdatetime").datetimepicker()在可编辑功能之外执行此操作时,它按预期工作,但是当我尝试使用可编辑函数执行此操作时,出现以下错误

firefox: TypeError: $.editable.types[settings.type] is undefined
chrome: Uncaught TypeError: Cannot read property 'plugin' of undefined 

这是我可编辑的datetimepicker

$('.editdatetime')
    .editable(function (value, settings) {
    if ((value == null) || (value == '')) {
        return ($(this)
            .parent()
            .find('label')
            .text());
    }
    hasBeenEdited(this);
    return (value);
}, {
    type: 'datetimepicker',
    datetimepicker: {
        dateFormat: 'mm-dd-yy',
        changeMonth: true,
        changeYear: true,
        showHour: true,
        showMinute: true
    }
});

任何指导或帮助将不胜感激

所以我

终于意识到我需要将输入类型datetimepicker添加到可编辑的。所以我设法使用以下方法解决这个问题:

$.editable.addInputType( 'datetimepicker', {
    /* create input element */
element: function (settings, original) {
    var form = $(this),
        input = $('<input />');
    input.attr('autocomplete', 'off');
    form.append(input);
    return input;
},
/* attach jquery.ui.datetimepicker to the input element */
plugin: function (settings, original) {
    var form = this,
        input = form.find("input");
    // Don't cancel inline editing onblur to allow clicking datetimepicker
    settings.onblur = 'nothing';
    datetimepicker = {
        onSelect: function () {
            // clicking specific day in the calendar should
            // submit the form and close the input field
            form.submit();
        },
        onClose: function () {
            setTimeout(function () {
                if (!input.is(':focus')) {
                    // input has NO focus after 150ms which means
                    // calendar was closed due to click outside of it
                    // so let's close the input field without saving
                    original.reset(form);
                } else {
                    // input still HAS focus after 150ms which means
                    // calendar was closed due to Enter in the input field
                    // so lets submit the form and close the input field
                    form.submit();
                }
                // the delay is necessary; calendar must be already
                // closed for the above :focus checking to work properly;
                // without a delay the form is submitted in all scenarios, which is wrong
            }, 150);
        }
    };
    if (settings.datetimepicker) {
        jQuery.extend(datetimepicker, settings.datetimepicker);
    }
    input.datetimepicker(datetimepicker);
}
});

最新更新