Next.js Discord

Discord Forum

Selection row in shadcn data table

Answered
Frieren posted this in #help-forum
Open in Discord
Hello, I just tried using a data table, I want to export its data to Excel. But before that, how do we get the value for each selected row?

code: https://github.com/herukhoeruman/sdm-fe2/blob/master/app/(dashboard)/generator/_components/data-table.tsx
Answered by Blue orchard bee
Hello! You can use getSelectedRowModel to extract the currently selected rows in the table. It returns an array of RowModels from which you can easily extract the data from.

More: https://tanstack.com/table/v8/docs/api/features/row-selection#getselectedrowmodel
View full answer

3 Replies

Blue orchard bee
Hello! You can use getSelectedRowModel to extract the currently selected rows in the table. It returns an array of RowModels from which you can easily extract the data from.

More: https://tanstack.com/table/v8/docs/api/features/row-selection#getselectedrowmodel
Answer
Blue orchard bee
Just by doing
useEffect(() => {
  console.log(table.getSelectedRowModel())
}, [table.getSelectedRowModel()])

You should be able to check for changes when you select a row.