Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use Kendo UI sortable widget on MVVM table

I am using kendo UI MVVM to populate this table.

<div class="col-md-12">
    <table id="table" class="table table-bordered col-md-12 col-xs-12 inspection-table">
        <thead>
            <tr>
                <th>#</th>
                <th>Image</th>
                <th>Description</th>
                <th">Actions</th>
            </tr>
        </thead>
        <tbody data-template="rowTemplate" data-bind="source: photos"> </tbody>
    </table>
</div>

<script type="text/x-kendo-template" id="rowTemplate">
    <tr>
        <td data-bind="text: number"></td>
        <td>
            <img class="" width="100" height="80" data-bind="attr:{ src:src }, events: { click: showPhoto }" />
        </td>
        <td data-bind="text: desc"></td>
        <td>
            <i class="kendo-sortable-move"></i>
        </td>
    </tr>
</script>

I want to be able to move the table rows in the UI by clicking on the user account.
If I add data-role="sortable" to the table, nothing works. Ref: https://www.telerik.com/forums/sortable-handler-inside-kendo-template

How do I get kendo sortable and its handlers to work inside the MVVM observable?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try using the jQuery equivalent kendoSortable widget. Using sortable inside MVVM templates seems to cause inconsistent compilations. This might cause the handlers to not work correctly inside the sortable widget. See: kendo sortable widget mvvm UI glitch.

Assign an id to the parent <div> wrapping the <table> and create the sortable object on it.

Then you can bind observable methods to the jQuery widget handlers as a proxy:

kendo.jQuery("#" + id).kendoSortable({
    filter: ">div.container",
    cursor: "move",
    placeholder: function (element) {
        return element.clone().css("opacity", 0.1);
    },
    hint: function (element) {
        return element.clone().removeClass("k-state-selected");
    },
    change: function (e) {
        let oldIndex = e.oldIndex,
            newIndex = e.newIndex;
        viewModel.updatePositions(oldIndex, newIndex);
    }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading