react-data-grid icon indicating copy to clipboard operation
react-data-grid copied to clipboard

Possible to delete row on key press?

Open GenoD opened this issue 9 years ago • 2 comments

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.

GenoD avatar Jan 10 '17 01:01 GenoD

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 })
   }
}

diogofcunha avatar Jan 11 '17 13:01 diogofcunha

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?

LawrenShin avatar Oct 28 '20 18:10 LawrenShin