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

including key of rowKeyGetter on the onRowsChange handler

Open hussamkhatib opened this issue 2 years ago • 1 comments

Use case

I am feeding initial data from server and enabling editing for some columns. I want to track the changes as and when any of the cell is edited. A simple useState should help me with that where I will be storing Map of key with row Id and and object of updated key/value pairs.

eg Map { id1: { name: "Bobby", age:22 }} // there are more columns, name and age are only cells updated for row with that id.

Why ? I will later use event handlers like discard changes, save changes.
For Save changes handler, I would want to pass the list of id 's back to the server to make the mutation cal.

Note:

  • idx is not useful in this case.
  • I could still access the value by using index from the second arg in onRowsChanged and the newly updated row. But, I feel this should be something ready available to consume from the API // something like
    updatedRows[RowsChangeData[indexes][0]]]

Proposed solution

Pass the key to the onRowsChanged handler if the user have used rowKeyGetter.

function rowKeyGetter(row) {
  return row.id;
}
export interface CalculatedColumn<TRow, TSummaryRow = unknown,KeyT> extends Column<TRow, TSummaryRow> {
  readonly idx: number;
  .....other props
  readonly key?: KeyT;  // optionally add this value if the user has provided a rowKeyGetter function 
}

or maybe pass as a sepearte key

    onRowsChange(updatedRows, {
      indexes: [rawRowIdx],
      column,
      key: getRowGetterkey(row) // optionally add this value if the user has provided a rowKeyGetter function
    });

If it sounds good, I can send a pr If you'd like.

hussamkhatib avatar Feb 05 '23 01:02 hussamkhatib

This sounds like a good feature to me, which store the id of rows for changed values. Cool

DVGY avatar Jul 05 '23 16:07 DVGY

As it is easy to get it outside RDG so at moment I think we are not going to add it

const key = getRowGetterkey(updatedRows[indexes[0]]);

amanmahajan7 avatar Nov 25 '24 16:11 amanmahajan7