Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SlickGrid Row ID changes after filtering

I have a slickgrid with inline filtering (using DataView). I've assigned unique ID to each row of data and I pass this ID (not the row number) to a function which updates a div someplace else on the UI.

This works fine if I don't filter. But if I filter the column prior to passing the ID, it changes the ID to reflect the row #. It will even change a string ID to a row number.

That just seems weird. Any idea what's going on???

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>');
    }
})
like image 599
user1415445 Avatar asked Sep 27 '12 16:09

user1415445


1 Answers

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
like image 51
idbehold Avatar answered Nov 12 '22 01:11

idbehold