@tanstack/react-table v8:如何在排序时获取下一行

问题描述 投票:0回答:1

我有一个排序(!)@tanstack-table v8(又名react-table)。

在每个 tr 中都有一个 onClick-Handler,它使我可以访问当前的行对象。

到目前为止一切顺利。

但是我如何访问下一行(或上一行)?

遗憾的是 row.index 不起作用,因为它不反映排序。据我所知,没有“sortedIndex”?

row react-table tanstack
1个回答
0
投票

经过一番挖掘,我发现了 Table.getSortedRowModel() 这会返回 - 你可能会猜到它 😉 - sorted 行模型!

文档链接:https://tanstack.com/table/v8/docs/api/features/sorting#getsortedrowmodel

我使用以下命令来获取表的特定列中包含 mySearchValue 的行的行索引:

let currentIndex = table
    .getSortedRowModel()
    .rows.findIndex((row) => row.getValue("<ColumnKey>") == mySearchValue);

使用它很容易在前一行的列中获取值

let prevRowFieldValue = table.getSortedRowModel()
    .rows[currentIndex - 1].getValue("<ColumnKey>");

对我来说这非常有效,也许对某人有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.