Dealing with bindings on kendo is always tricky. I have update your demo with a few changes:
Editor:
When you're using data-bind
you're not suposed to handle the widget's state. Kendo should deal with it by itself, but you need to tell kendo to handle that using kendo.bind(element, model)
(bind()
docs). Hence, you don't need to set data-value
attribute.
function commentEditor(container, options) {
var datePicker = $('<input data-role="datepicker" data-format="dd/MM/yyyy" type="date" name="Comment" data-bind="value:Comment">');
datePicker.appendTo(container);
kendo.bind(datePicker, options.model);
}
Comment
field type:
In order to make kendo to know how to handle the Comment
field value as a date and to set it properly to the widget, you need to set the right data type in it's model definition:
Comment: { type: "date", editable: true }
Template:
A small fix to the template:
template: function (dataItem) {
if (dataItem.Comment != "") {
let date = kendo.parseDate(dataItem.Comment);
if (date) {
return kendo.toString(date, "dd/MM/yyyy");
}
}
return (dataItem.Comment || "");
}
I'm making sure that the Comment
content is a valid date by checking the parseDate
result. If not valid, proceed to another condition where it verifies if Comment
is not null
, undefined
, etc, if yes, prints an empty string.
I hope it helps.
Update
Not sure why, but it seems that kendo saves the selected value as string to the bound property. I have added this handler to the widget's change
event that seems to work:
datePicker.data("kendoDatePicker").bind("change", function(e) {
let model = this,
widget = e.sender;
model.Comment = widget.value();
}.bind(options.model));
Updated demo
That forces Comment
property to be of Date
type.