所以我想使用react-table创建一个可排序的列,对每一行进行编号(根据它在数据集中的位置)。
根据文档,排序是通过比较accessor
值的排序函数来完成的。但是,与Cell
选项不同,accessor
选项不公开索引。
所以这意味着这不会起作用:
const columns = [
{
Header: '#',
id: 'index',
accessor: (row) => row.index // 'index' is undefined
},
/* ... */
]
我目前的解决方法是将索引直接注入到数据集中,如下所示:
myIndexedData = myData.map((el,index) => ({index, ...el})) //immutable
return (
<ReactTable
data={myIndexedData}
columns={columns}
/>
);
这并不是一个真正的最佳解决方案,尤其是对于大型数据集。有没有更好的办法让我看不到?
发布于 2018-06-01 08:47:15
{
Header: "",
id: "row",
maxWidth: 50,
filterable: false,
Cell: (row) => {
return <div>{row.index}</div>;
}
},
发布于 2021-02-08 16:28:09
{
Header: "Index",
accessor: "",
Cell: (row) => {
return <div>{row.row.id + 1}</div>;
},
disableSortBy: true,
disableFilters: true,
},
版本“react-table”:"^7.6.3",
发布于 2021-05-09 16:15:22
您可以使用第二个参数作为索引。
{
Header: '#',
id: 'index',
accessor: (_row: any, i : number) => i + 1
}
https://stackoverflow.com/questions/47518201
复制相似问题