Possible to delete row on key press?
Hello all,
I am trying to implement deletion of a row on "delete" key pressed. My current attempt has me working with onGridKeyDown() to perform an action when the delete key is pressed:
const onKeyDown = (e) => {
if (e.keyCode == 46) {
e.preventDefault();
this.setState({ confirmActionOpen: true, index: e.rowIdx })
}
}
I open a modal to confirm the action and if confirmed the row will be removed. It seems "e" does not contain row information and only details the action performed.
I have considered keeping current row in the state. However, after performing up and down actions as well as clicking around, keeping track of this value seems troublesome. I can fall back on this but I would like to know first if there is an easier way.
Hi @GenoD.
You can inject a onCellSelected handler as a prop to ReactDataGrid. That callback will run when a new cell is selected, you will receive the cells coordinates as a param in that same callback.
Something like this will do the trick:
const handleCellSelect = ({ rowIdx: selectedRowIdx }) => this.setState({ selectedRowIdx })
const onKeyDown = ({ keyCode, preventDefault }) => {
if (keyCode == 46) {
preventDefault();
this.setState({ confirmActionOpen: true })
}
}
I don't get it. Trying to implement the same functionality with functional components. Is it possible to make cell uneditable but provide onkeydown handler so it would be possible to clear cell?