【问题标题】:How do I determine which DataView generated an event?如何确定哪个 DataView 生成了事件?
【发布时间】:2013-04-08 02:46:50
【问题描述】:

我正在通过网格的 HeaderRow 实现过滤,但遇到了一个似乎无法解决的问题。

所有显示 dataView 事件处理程序的示例都使用全局变量来识别相关网格,例如:

gridQMsgsDataView.onRowCountChanged.subscribe(rowCountChanged);

(...elsewhere...)

function rowCountChanged(e, args) {
    grid.updateRowCount();   //'grid' is a global variable assigned to a slickGrid.
    grid.render();
}

与我找到的示例不同,我在运行时动态创建 slickjGrids 和 dataViews,并在创建它们时将它们的引用变量保存在列表中。

我无法在编译时知道会有多少,因此我不能使用全局“grid”变量来引用相关的数据视图和/或 slickgrid。

所以我有两个问题,感谢任何见解:

当我的 rowCountChanged 处理程序被调用时...

A) 我如何知道是哪个 dataView 生成了事件?

B) 一旦知道了,我怎么知道 dataView 与哪个 slickgrid 关联?

【问题讨论】:

    标签: events slickgrid dataview


    【解决方案1】:

    A) rowCountChanged 在 dataGrid 的上下文中被调用;所以this 是回调中的dataGrid,或者使用B) 中描述的相同闭包

    B) 使用闭包,即使多个网格(显示)绑定到同一个 dataView 也可以工作(这只是可能的,因为 dataView 与特定的网格)。

    当您使用 dataView(或稍后分配)创建网格时,在同一范围内(grid 可用),使用辅助函数订阅回调以包围网格:

    function watchRowCountChanges(grid, dataView) {
      dataView.onRowCountChanged.subscribe(function (e, args) {
        grid.updateRowCount();   // `grid` is a _local_ variable
        grid.render();
    
        console.log(grid.name, this === dataView); // true story, use either!
      });
    }
    
    // elsewhere...
    
    var grid1 = new Slick.Grid(element1, dataView, columns1, options1);
    grid1.name = 'grid1';
    watchRowChanges(grid1, dataView);
    
    
    var grid2 = new Slick.Grid(element2, [], columns2, options2);
    grid2.name = 'grid2';
    grid2.setData(dataView);
    watchRowChanges(grid2, dataView);
    

    当行数改变时:

    grid1 true
    grid2 true
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2017-09-17
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      相关资源
      最近更新 更多