Kendo UI 列表视图 - 在编辑时切换模型实例

Kendo UI List View - switches model instance on Edit

我有一个简单的 Kendo 列表视图,其中包含来自四个 Note 个对象的数组的静态数据

var notes = [{"note_id":1,"content":"This is Note 1","created":"2019-05-08 00:39:34"},
            {"note_id":2,"content":"This is note 2","created":"2015-06-04 15:49:26"},
            {"note_id":3,"content":"This is note 3","created":"2015-06-03 15:49:26"},
            {"note_id":4,"content":"This is note 4","created":"2015-06-02 15:49:26"}];

我有单独的 template 用于显示和编辑注释

<script type="text/x-kendo-tmpl" id="NoteTemplate">
        <div class="product-view k-widget">
            <dl>
                <dt>#:kendo. toString(created, "dd/MM/yyyy HH:mm")#</dt>
                <dd>#=(content)#</dd>
            </dl>
            <div class="edit-buttons">
                <a class="k-button k-edit-button" href="\#"><span class="k-icon k-i-edit"></span></a>
                <a class="k-button k-delete-button" href="\#"><span class="k-icon k-i-close"></span></a>
            </div>
        </div>
        <input type="hidden" name="type_id" value="0" data-bind="value:type_id" />
</script>

<script type="text/x-kendo-tmpl" id="NoteEditTemplate">
        <div class="product-view k-widget">
            <dl>
                <dt>#:kendo. toString(created, "dd/MM/yyyy HH:mm")#</dt>
                <dd>
                    <div data-bind="value:content">
                #=content#
                    </div>
                </dd>

            <div class="edit-buttons">
                <a class="k-button k-update-button" href="\#"><span class="k-icon k-i-check"></span></a>
                <a class="k-button k-cancel-button" href="\#"><span class="k-icon k-i-cancel"></span></a>
            </div>
        </div>
</script>

问题是,当用户单击 "pencil" 图标进行编辑时 "Note 2",编辑模板被呈现 但使用的是 Note 3 的模型

如果用户取消编辑更多,他们会再次看到显示模板渲染注释 2

所以当我们进入编辑模式时,Kendo 组件似乎正在从注释 2 切换到注释 3...为什么要这样做?

请在此处查看 运行 演示: https://dojo.telerik.com/oNosOCUv/3

我做了 3 处更改:-

正在将架构添加到数据源。

正在关闭 EditNoteTemplate 中的 dl 标签。

将隐藏输入移至父元素 div,因为 Kendo 正在将数据 uid 分配给此元素。

<script type="text/x-kendo-tmpl" id="NoteTemplate">
        <div class="product-view k-widget">
            <dl>
                <dt>#:kendo. toString(created, "dd/MM/yyyy HH:mm")#</dt>
                <dd >#=(content)#</dd>
                <input type="hidden" name="type_id" value="0" data-bind="value:type_id" />
            </dl>
            <div class="edit-buttons">
                <a class="k-button k-edit-button" href="\#"><span class="k-icon k-i-edit"></span></a>
                <a class="k-button k-delete-button" href="\#"><span class="k-icon k-i-close"></span></a>
            </div>
        </div>      
</script>

<script type="text/x-kendo-tmpl" id="NoteEditTemplate">
        <div class="product-view k-widget">
            <dl>
                <dt>#:kendo. toString(created, "dd/MM/yyyy HH:mm")#</dt>
                <dd>
                    <div data-bind="value:content">
                        #=content#
                    </div>
                </dd>
            </dl>
            <div class="edit-buttons">
                <a class="k-button k-update-button" href="\#"><span class="k-icon k-i-check"></span></a>
                <a class="k-button k-cancel-button" href="\#"><span class="k-icon k-i-cancel"></span></a>
            </div>
        </div>
</script>

  <script>

    var notes = [
                        {"note_id":1,"content":"This is Note 1","created":"2019-05-08 00:39:34"},
                        {"note_id":2,"content":"This is note 2","created":"2015-06-04 15:49:26"},
                        {"note_id":3,"content":"This is note 3","created":"2015-06-03 15:49:26"},
                        {"note_id":4,"content":"This is note 4","created":"2015-06-02 15:49:26"}
                ];

    $(document).ready(
            function() {    
                var dataSource = new kendo.data.DataSource({   
                                 data: notes,
                                 schema: {
                                   model: {
                                   id: "note_id",
                                   fields: {
                                    note_id: { type: "number" },
                                    content: { type: "string" },
                                    created: { type: "date" }
                                   }
                                }
                            }});

                var listView = $("#notes-list").kendoListView({
                    dataSource: dataSource, 
                    template: kendo.template($("#NoteTemplate").html()),
                    editTemplate: kendo.template($("#NoteEditTemplate").html()) 
                }).data("kendoListView");
      });
  </script>

  <div id="notes-list"></div>