首页 > 解决方案 > 如何在 Kendo UI Grid 的 onDataBound 事件中检查现有行值是否更改?

问题描述

我在 Asp.Net MVC 中使用 Kendo UI Grid。

我在文档就绪函数中调用了以下事件。

function RefreshGrid() {
        setInterval(function () {
            $("#MediaBatchGrid").data("kendoGrid").dataSource.read();
            $(".k-loading-image").hide();
        }, 5000);
    }

每当向服务器发出请求时调用上述函数。整个数据将绑定在剑道网格 UI 中。所以,问题是,网格闪烁甚至保留网格中的现有值。我担心的是,如何在客户端停止这种闪烁?如果新数据与现有值不同,那么只有在数据绑定事件上才会发生怎么办,否则返回并且不需要在网格中更改任何 UI

标签: jquerykendo-uikendo-gridkendo-asp.net-mvc

解决方案


我为此挣扎了一段时间,最后,我找到了简单的解决方案。

我们应该在 HTML 中为数据绑定定义一个事件。IE,

 .Events(x => x.DataBinding("Grid_DataBinding"))

jQuery代码

Grid_DataBinding = function (e) {



    var displayedData = $("#Grid").data().kendoGrid.dataSource.view();// this is to get Existing values from Grid

    var displayedDataAsJSON = JSON.stringify(displayedData);
// then convert it as JSON
    var newData = e.sender._data;// to get new data coming from Server

    var newDataAsJSON = JSON.stringify(newData);// convert it too as JSON

    if (newDataAsJSON === displayedDataAsJSON) {// Equal check

        e.preventDefault();// if both are equal, just prevent data binding event
        $(".k-loading-mask").hide();// this is to avoid grid flickering, hiding loader too


    }


}

推荐阅读