简体   繁体   English

动态Kendo UI Grid可编辑日期时间列不使用值

[英]Dynamic Kendo UI Grid editable datetime column not using value

I have a kendo UI Grid that has in-line editing on a single column. 我有一个Kendo UI Grid,它在单列上进行了行内编辑。 That column should be using a datepicker as the input when editing. 编辑时,该列应使用日期选择器作为输入。

However, after setting the value on the datepicker, and then returning to the same row/column, the date does not then show in the datepicker input. 但是,在日期选择器上设置值之后,然后返回相同的行/列,则日期不会在日期选择器输入中显示。

I have created a Dojo to show what I mean: https://dojo.telerik.com/eJEmoVEv/4 我创建了一个Dojo来显示我的意思: https : //dojo.telerik.com/eJEmoVEv/4

And a quick gif to explain the issue better: 并快速使用gif更好地解释了这个问题: 问题

Dealing with bindings on kendo is always tricky. 处理剑道上的绑定总是很棘手。 I have update your demo with a few changes: 我已对您的演示进行了一些更改:

  1. Editor: 编辑:

    When you're using data-bind you're not suposed to handle the widget's state. 当您使用data-bind您不会被迫处理小部件的状态。 Kendo should deal with it by itself, but you need to tell kendo to handle that using kendo.bind(element, model) ( bind() docs ). Kendo应该自己处理它,但是您需要告诉kendo.bind(element, model)使用kendo.bind(element, model)bind() docs )来处理它。 Hence, you don't need to set data-value attribute. 因此,您不需要设置data-value属性。

     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); } 
  2. Comment field type: Comment栏位类型:

    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: 为了使kendo知道如何将Comment字段值作为日期进行处理并将其正确设置为小部件,您需要在模型定义中设置正确的数据类型:

     Comment: { type: "date", editable: true } 
  3. 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. 我通过检查parseDate结果来确保Comment内容是有效日期。 If not valid, proceed to another condition where it verifies if Comment is not null , undefined , etc, if yes, prints an empty string. 如果无效,则继续执行另一种条件,在该条件下验证Comment是否为nullundefined等,如果是,则打印一个空字符串。

I hope it helps. 希望对您有所帮助。

Update 更新资料

Not sure why, but it seems that kendo saves the selected value as string to the bound property. 不知道为什么,但是kendo似乎将所选值作为字符串保存到bound属性。 I have added this handler to the widget's change event that seems to work: 我已将此处理程序添加到似乎起作用的小部件的change事件中:

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. 这迫使Comment属性为Date类型。

After the help from @DontVoteMeDown I finally found an answer to this. 在@DontVoteMeDown的帮助下,我终于找到了答案。 The datepicker is expecting the Comment field to be of date type, so adding in a kendo.parse and then resetting the comment field fixed this issue. 日期选择器期望Comment字段为日期类型,因此添加kendo.parse并重新设置注释字段可以解决此问题。

See updated kendo dojo: https://dojo.telerik.com/eJEmoVEv/4 查看更新的剑道道场: https//dojo.telerik.com/eJEmoVEv/4

var dateTimeComment = kendo.parseDate(options.model.Comment);
options.model.Comment = dateTimeComment;

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

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