【问题标题】:How do I check if a Kendo Grid has had changes made to it?如何检查剑道网格是否对其进行了更改?
【发布时间】:2012-09-25 02:12:57
【问题描述】:

如何检查Kendo Grid 是否有更改?听说有dirty属性,但是找不到。

【问题讨论】:

  • 如果您设置了batch: true,则有一个change 事件指示何时发生更改。
  • 刚刚注意到,如果您希望 hasChanges() 函数工作(或脏属性出现在 dataItem 上),您需要定义 dataSource 架构。

标签: javascript kendo-ui kendo-grid


【解决方案1】:

您可以在 Grid 的底层 DataSource 上使用“hasChanges”方法:

grid.dataSource.hasChanges();

$('#divGrid').data('kendoGrid').dataSource.hasChanges();

【讨论】:

    【解决方案2】:

    添加的行会将dirty 属性设置为true,更新的行也会如此。但是,删除的行存储在其他地方(在 _destroyed 集合中)。将此函数传递给网格的数据源以查看它是否有更改。

    function doesDataSourceHaveChanges(ds)
    {
        var dirty = false;
    
        $.each(ds._data, function ()
        {
            if (this.dirty == true)
            {
                dirty = true;
            }
        });
    
        if (ds._destroyed.length > 0) dirty = true;
    
        return dirty;
    }
    

    【讨论】:

      【解决方案3】:

      您可以获得通知并使用数据源的更改事件,该事件将在您分页/排序/分组/过滤/创建/读取/更新/删除记录的任何位置发生。

      要将处理程序附加到它,请使用:

      $('#YourGrid').data().kendoGrid.dataSource.bind('change',function(e){
          //the event argument here will indicate what action just happned
          console.log(e.action)// could be => "itemchange","add" or "remove" if you made any changes to the items
      })
      

      更新:如果用户更新了任何模型,数据源的 .hasChanges() 方法将返回 true。

      【讨论】:

        【解决方案4】:

        grid.dataSource.hasChanges 会通知您数据源是否已更改

                                    if (datasource.hasChanges() === true) {
                                        alert('yes');
                                    } else {
                                        alert('no');
                                    }
        

        【讨论】:

          【解决方案5】:

          最方便的方法是使用datasource.hasChanges()。但是,这需要在架构中定义 Id 字段。

          来自docs

          检查数据项是否已更改。需要 [ID 字段] 在 schema.model.id 中配置,否则将始终返回 true。

          如果您没有定义 Id 字段,您可以使用无数种方法中的一种来迭代数据本身。

          var isDataSourceDirty = function (ds) {
              var dirty = ds._data.filter(function(x){
                  return x.dirty === true;
              });
              return dirty.length > 0 || ds._destroyed.length;
          };
          

          【讨论】:

            【解决方案6】:

            值得一试:

            var hasDirtyRow = $.grep(gridDataSource.view(), function(e) { return e.dirty === true; });
            if (hasDirtyRow.length != 0)
            {
                 // grid has dirty row(s)
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-07-25
              • 2013-01-08
              • 1970-01-01
              • 2019-01-19
              • 2014-06-28
              • 1970-01-01
              相关资源
              最近更新 更多