index column #2224
-
I need to add a index column as the first column, which doesn't get effected by the sorting, Before Sorting After Sorting |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 9 replies
-
Render index column as custom cell |
Beta Was this translation helpful? Give feedback.
-
Bad approach - but what upfront is coming in my mind - Try to check on |
Beta Was this translation helpful? Give feedback.
-
I was able to solve this issue, defined a actual row inside the actual table component which count the row by using the index value in the map function of row |
Beta Was this translation helpful? Give feedback.
-
viewIndex was accessible in V6, which solved the challenge listed in this discussion. It looks like V7 removed |
Beta Was this translation helpful? Give feedback.
-
You could use // columns
[
{
Header: 'No',
id: 'id',
Cell: ({ row, flatRows }) => {
return flatRows.indexOf(row) + 1;
},
},
// ...other columns
]
|
Beta Was this translation helpful? Give feedback.
-
@henry61024 any idea on how to do this with Tanstack table 8? Dont see flatRows exposed anymore |
Beta Was this translation helpful? Give feedback.
-
I had to modify pieces from above comments to make it work with sorting applied, maybe this will help someone as well:
|
Beta Was this translation helpful? Give feedback.
-
@lazy-ocean its working Good, Thank you for providing |
Beta Was this translation helpful? Give feedback.
-
From the future ... April 11, 2024. @lazy-ocean answer works perfectly |
Beta Was this translation helpful? Give feedback.
-
@lazy-ocean solution works perfectly, Therefore, my idea is built upon @lazy-ocean solution, just cache the array in map upon the sorting change, and do the idx on rendering. const sortedRows = table.getSortedRowModel().flatRows;
// this will only run upon sorting change.
const sortedMap = useMemo(() => {
const output: Record<string, number> = {};
sortedRows.forEach((row, idx) => {
output[row.id] = idx;
});
return output;
}, [sortedRows]); <div>
{cell.column.id === "id"
? sortedMap[row.id] + 1
: flexRender( cell.column.columnDef.cell, cell.getContext())
}
</div> |
Beta Was this translation helpful? Give feedback.
I was able to solve this issue, defined a actual row inside the actual table component which count the row by using the index value in the map function of row