5

I'm using the react-table package. I made a simple table with row selection. The problem is: The row selection result is an object with indexes:

{
 0: true,
 1: true,
 ...
}

But I want to be the primary key of my data like this:

{
  EXoxMjyd4leYABdpuy8m: true,
  2gXlClA38AU8sSM5jnZ7: true,
  ...
}

Code example

In the documentation, I can't find a configuration where I can set the selection key. The question is, how can I achieve the second example?

2 Answers 2

9

You need to overwrite the table option getRowId.

Example:

const getRowId = (row, relativeIndex, parent) => {
   // In row object you have access to data. 
   // You can choose parameter. In this example I used uniqueId
   return parent ? [parent.id, row.uniqueId].join('.') : row.uniqueId;
}

Here is live example:

Edit tannerlinsley/react-table: row-selection-controlled (forked)

Sign up to request clarification or add additional context in comments.

Comments

2

If you are using version 8 react-table; you can use instance.getSelectedRowModel() in order to get the original (data model of the rows) of the selected rows as well as the row indexes.

For instance:

const arraySelectedUsersId = instance.getSelectedRowModel().rows.map(({ original }) => (original as User).id)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.