2

I'm implementing filtering via the HeaderRow of my grids, and I've run across a problem I can't seem to solve.

All the examples that show dataView Event handlers employ global variables to identify the related grid, such as in:

gridQMsgsDataView.onRowCountChanged.subscribe(rowCountChanged);

(...elsewhere...)

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

Unlike in the examples that I find, I'm creating slickjGrids and dataViews dynamically at runtime, and keep their reference variables in a list as I create them.

I can't tell at compile time how many there will be, thus I can't use something like the global "grid" variable to reference the relevant dataview and/or slickgrid.

So I have two questions, and appreciate any insight:

when my rowCountChanged handler is called...

A) How do I know which dataView generated the event?

B) Once I know that, how do I know which slickgrid that dataView is associated with?

4

1 回答 1

0

a) rowCountChanged在dataGrid的上下文中调用;回调中的 dataGrid 也是如此this,或者使用 B 中描述的相同闭包)

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

当您使用 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
于 2014-12-18T06:47:06.943 回答