javascriptjquerykendo-uikendo-listview

Kendo UI List View - switches model instance on Edit


I have a simple Kendo List view, with static data from an array of four Note objects

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"}];

I've got separate templates for display and edit of the Notes

<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>

The issue is, when the user clicks the "pencil" icon to edit "Note 2", the edit template is rendered but with the model for Note 3

If the user cancels edit more, they again see the display template rendering Note 2

So it seems like the Kendo component is switching from Note 2 to note 3 when we go into edit mode... Why is it doing this?

See the running demo here: https://dojo.telerik.com/oNosOCUv/3


Solution

  • I made 3 changes:-

    Adding schema to the datasource.

    Closing dl tag in EditNoteTemplate.

    Move the hidden input into the the parent div, because Kendo is assigning the data uid to this element. Look at the HTML elements rendered

    <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>