6

我有一个带有内联过滤的 slickgrid(使用 DataView)。我为每一行数据分配了唯一的 ID,并将这个 ID(不是行号)传递给一个函数,该函数在 UI 的其他位置更新 div。

如果我不过滤,这可以正常工作。但是,如果我在传递 ID 之前过滤列,它会更改 ID 以反映行#。它甚至会将字符串 ID 更改为行号。

这似乎很奇怪。知道发生了什么吗???

grid_msc.onClick.subscribe(function(e, args) {
    var cell = grid_msc.getCellFromEvent(e);
    var row = cell.row;             // get row #
    var row_ID = data_msc[row].id;  // get the row ID, not row #
    var msc = data_msc[args.row][grid_msc.getColumns()[args.cell].field];

    alert("Row#:"+row+", RowID:"+row_ID+", Value:"+msc);
    mscToUI(msc, row_ID);
});


// Add the selected item to the UI
    function mscToUI(addC, cellNum) {
        alert(addC+", "+cellNum);
        $('#selectedMsc').append('<a href="javascript:removemsc('+cellNum+')" id="'+cellNum+'" class="rSel"><img src="images/remove.png" align="texttop" border="0" style="padding-right:4px;">'+addC+'<br /></a>');
    }
})
4

1 回答 1

16

If you're already using DataView then you should be getting the rows/data from it (dataView_msc), rather than the original data source (data_msc).

grid_msc.onClick.subscribe(function(e, args) {
  var cell   = grid_msc.getCellFromEvent(e);  // get the cell
  var row    = cell.row;  // get the row's index (this value will change on filter/sort)
  var item   = dataView_msc.getItem(row);  // get the row's item (see: object, data)
  var msc    = item[grid_msc.getColumns()[cell.cell].field];  // get the value of the cell

  alert("Row Index:"+row+", RowID:"+item.id+", Cell Value:"+msc);
  console.log(item);
  mscToUI(msc, item.id);
});

I'm not quite sure what you're planning on doing inside mscToUI() with value of the clicked cell and the value of its row's id property. I think it might be smarter to simply pass the row's entire data object (item) to the function and preform any other operations using the DataView's lookup methods:

  • getIdxById(id) - With the item's id, find the relative row index in the grid`
  • getItem(i) - With the row index of the filtered grid, return the data/item for said row
  • getItemById(id) - With the item's id, return the data/item for said item
  • getItemByIdx(i) - With the row index of the unfiltered grid, return the data/item for said row
于 2012-09-30T20:12:21.773 回答