Search code examples
javascriptslickgrid

SlickGrid javascript get current row data


I would like to remove a row when I press the Delete key. But I can't get any data from the source below:

var selectedrows = grid.getSelectedRows();

grid.onKeyDown.subscribe(function(event) {
    var item = data[selectedrows.cell];
    if (event.keyCode == 46) {
        alert(item.hostname);
    }
}); 

Solution

  • First, the getSelectedRows() function returns an Array of the selected column numbers. The way you are trying will return undefined, since that Array doesn't contain any property called cells.

    Open this official SlickGrid example demo and try the following:

    Select the first row in grid.

    Try issuing the following command in your JS debugger console:

    grid.getSelectedRows()
    

    Will return you an Array with the selected row number as:

    Array [ 0 ]

    The returned Array'sfirst element is the row number that you have just selected.

    Now that we know the selected row number issue this:

    data[grid.getSelectedRows()[0]]
    

    Will return you the selected Object as:

    Object { name: "Make a list", complete: true }

    If you want to reach a property of the returned Object you could do it as:

    var selectedRow = data[grid.getSelectedRows()[0]];
    console.log(selectedRow.name);
    

    Will return the property name's value as:

    Make a list

    Hope this clears up your confusion.