krasnit
krasnit

Reputation: 1

How to get selected row in slickgrid from outside slickgrid event

My rows in slickgrid are selected (highlighted) mainly using the arrow keys, but mouse selection is allowed. I have a button outside of the slickgrid, which, when pressed, is to get the row that is highlighted (selected) in the slickgrid. There is no event occurring in slickgrid after the selection (highlighting) has been made (eg grid.onClick, grid.onKeyDown, etc) so I have no idea on how to find the row that is highlighted (selected), specifically when clicking on a button. Could an event be programmatically generated into slickgrid to do this? Any help would be appreciated.

Upvotes: 0

Views: 1240

Answers (1)

ghiscoding
ghiscoding

Reputation: 13214

I can only assume that you're not using the dedicated plugin for that which is slick.checkboxselectcolumn and slick.rowselectionmodel.js and you can see this Example and from that you'll get access to onSelectedRowsChanged event and if you choose to make it single click then clicking anywhere on the row will trigger the event (if however you're using multiple row selection then it will only trigger from the left column checkboxes)

import '../plugins/slick.checkboxselectcolumn.js';
import '../plugins/slick.rowselectionmodel.js';

grid = new Slick.Grid("#myGrid", data, columns, options);
grid.setSelectionModel(new Slick.RowSelectionModel({selectActiveRow: false}));
grid.registerPlugin(checkboxSelector2);

grid.onSelectedRowsChanged.subscribe(function (e, args) {
  // debugging to see the active row in response to questions
  // active row has no correlation to the selected rows
  // it will remain null until a row is clicked and made active
  // selecting and deselecting rows via checkboxes will not change the active row
  var rtn = args.grid.getActiveCell();
  var x = args.rows;
});

From the row indexes, there's multiple way to retrieve the associated item object, in my case I use SlickGrid DataView and I retrieve it with the following

for (const row of args.rows) {
  const itemObj = dataView.getItem(row);
  // do something
}

There's multiple methods for different behavior in SlickGrid and in DataView

// -- DataView --//
/** Get DataView item at specific index */
getItem(index: number) => any;

/** Get an item in the DataView by its Id */
getItemById(id: string | number) => any | null;

/** Get an item in the DataView by its row index */
getItemByIdx(idx: number) => any;

/** Get row index in the DataView by its Id */
getIdxById(id: string | number): number | undefined;
// -- SlickGrid --//
/**
 * Returns the databinding item at a given position.
 * @param index Item row index.
 */
getDataItem(rowIndex: number): any;

Upvotes: 0

Related Questions