Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何管理页面上的多个Slickgrids事件?_Javascript_Javascript Events_Event Handling_Slickgrid_Multiple Tables - Fatal编程技术网

Javascript 如何管理页面上的多个Slickgrids事件?

Javascript 如何管理页面上的多个Slickgrids事件?,javascript,javascript-events,event-handling,slickgrid,multiple-tables,Javascript,Javascript Events,Event Handling,Slickgrid,Multiple Tables,我对在一个页面上使用多个网格有问题。由于网格的数量是动态变化的,所以我在javascript函数中创建它们,并使用网格数组来保持它们,如下所示 var columns = []; var options = []; for(var i=0; i<value; i++){ options[i] = { editable: true, enableAddRow: true, enableCellNavigation: true, asyncEditorLo

我对在一个页面上使用多个网格有问题。由于网格的数量是动态变化的,所以我在javascript函数中创建它们,并使用网格数组来保持它们,如下所示

var columns = [];
var options = [];
for(var i=0; i<value; i++){
   options[i] = {
    editable: true,
    enableAddRow: true,
    enableCellNavigation: true,
    asyncEditorLoading: true,
    autoEdit: false,
    forceFitColumns: false,
    //fullWidthRows: true,
    syncColumnCellResize: true,
    rerenderOnResize: true,
    topPanelHeight: 30,
    rowHeight: 22,
    cellHighlightCssClass: "changed",
    cellFlashingCssClass: "current-server"};
}
for(var i=0; i<value; i++) {
   columns[i].push({id: value, name: value, field: value});
   columns[i].push({id: value2, name: value2, field: value2});
   columns[i].push({id: value3, name: value3, field: value3});
}
var arrayOfGrids = [];

for(var i=0; i<value; i++) {
  dataView = new Slick.Data.DataView();
  arrayOfGrids.push(new Slick.Grid('#' + i, dataView, columns[i], options[i]));
    // ....
但我的问题是如何管理这些网格的事件,因为就我而言,这些事件应该单独实现。即使我的所有网格必须具有相同的事件,我也不知道如何确定触发了哪个网格的事件


请帮帮我!!!非常感谢您的关注

您熟悉Javascript中闭包的概念吗?如果不是,你应该用谷歌搜索它

以下是我认为他们可以如何解决您的问题:

1创建自己的函数来创建/初始化网格

2在for循环中调用此函数

3在initGrid函数中订阅您的事件。这样,您将始终拥有对正确网格的引用。看看下面代码中initGrid函数中的注释

function initGrid(index)
{
  var dataView = new Slick.Data.DataView();
  var grid = new Slick.Grid('#' + index, dataView, columns[index], options[index]);
  arrayOfGrids.push(grid);
  grid.onClick.subscribe(function (e) {
     //grid variable will always be the clicked gird
     //index variable will always be the correct one for this grid
     //columns[index] will always be the columns for the clicked grid
     //...
  });
}

for(var i=0; i<value; i++) {
    initGrid(i);
    // ....
}