我继承了一段最初只需要可编辑日期的代码,这是使用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
}
});
现在我被要求在日期中包含一个时间(服务器端的数据库和模型已更改为时间戳),我正试图加入datetimepicker插件。
当我使用$(".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
}
});
非常感谢任何指导或帮助
答案 0 :(得分:1)
所以我终于意识到我需要将输入类型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);
}
});