简体   繁体   English

如何在排序时同步Kendo UI网格数据源?

[英]How Do I Synch Kendo UI Grid Datasource On Sort?

Okay, so we have some legacy code which uses Kendo UI Grids. 好的,所以我们有一些使用Kendo UI网格的旧代码。

After sorting the grid, we need to be able to read the DataSource item which corresponds to each row, by iterating through the JQuery selected 'tr' list and then select the DataSource item based on the index of the row itself. 在对网格进行排序之后,我们需要能够通过遍历JQuery选定的“ tr”列表来读取与每一行相对应的DataSource项,然后根据行本身的索引选择DataSource项。

The problem exists in that the DataSource doesn't sort when the grid sorts, so you cannot simply get the index of each row and look up the corresponding DataSource item, like this... 问题在于网格排序时DataSource不会排序,因此您不能简单地获取每一行的索引并查找相应的DataSource项,就像这样...

    $('td:nth-child(' + colIndex + ')', $('tbody', grid.element).eq(0)).each(
        function (iIndex) {
            var td = $(this);
            var tr = td.parent();
            var data = grid.dataSource.data()[iIndex];

            if (data.Status.toLowerCase() !== 'c') {
                totalBalanceDue = (totalBalanceDue - data.Payment) > 0 ? (totalBalanceDue - data.Payment) : 0;
            };
            td.html('$' + totalBalanceDue.formatMoney(2, '.', ','));
        }
    ); 

This is all not needed. 这都是不需要的。 All you need to use is the view () method of the dataSource. 您需要使用的只是dataSource的view ()方法。

Looks like I have to answer this question myself. 看来我必须自己回答这个问题。

I have found, by scouring the documentation and the web, that the way I'm approaching this is not necessarily accurate. 通过搜索文档和网络,我发现我的解决方法不一定是准确的。 Instead of attempting to sort the array to match the rows of the gridview, I can use the data-uid attribute of the row, to get the corresponding dataitem from the DataSource.data() array, by using the dataSource.getByUid() function. 我可以尝试使用该行的data-uid属性,而不是尝试对数组进行排序以匹配gridview的行,而是通过使用dataSource.getByUid()函数从DataSource.data()数组中获取相应的dataitem。 。

This is working just as I need it to. 正如我需要的那样,它正在工作。

       $('td:nth-child(' + colIndex + ')', $('tbody', grid.element).eq(0)).each(
            function (iIndex) {
                var td = $(this);
                var tr = td.parent();

                //gets the corresponding dataitem for the selected row.  
                var data = grid.dataSource.getByUid(tr.data("uid"));
                if (data.Status.toLowerCase() !== 'c') {
                    totalBalanceDue = (totalBalanceDue - data.Payment) > 0 ? (totalBalanceDue - data.Payment) : 0;
                };
                td.html('$' + totalBalanceDue.formatMoney(2, '.', ','));
            }
        );

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

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