首页 > 解决方案 > 我如何在列标题反应表中检索访问器名称

问题描述

我的列如下:

const columns = React.useMemo(
    () => [
        {
            Header: "Name",
            accessor: "name", // accessor is the "key" in the data
        },
        {
            Header: "Email",
            accessor: "email",
        }
    ])

我的表格代码片段如下所示:

<table
    {...getTableProps()}
    id="basicTable"
    className="table table-bordered action-table table-striped table-hover mb-0"
>
    <thead>
        {headerGroups.map((headerGroup) => (
            <tr {...headerGroup.getHeaderGroupProps()}>
                {headerGroup.headers.map((column) => (
                    <th
                        {...column.getHeaderProps()}
                        className="align-top"
                    >
                        <div
                            // {...column.getSortByToggleProps()}
                            onClick={(e) => {
                                console.log(
                                    `col ${column.accessor}`
                                );
                            }}
                        >
                            {column.render("Header")}
                        </div>
                        <div>
                            {column.canFilter
                                ? column.render(
                                      "Filter"
                                  )
                                : ""}
                        </div>
                    </th>
                ))}
            </tr>
        ))}
    </thead>
</table>

现在,在这里,我正在尝试检索访问器,但我无法这样做,在安慰时column.accessor,它返回:

function accessor(row) {
        return getBy(row, accessorPath);
      }

如何,我可以获得访问器名称,以便我可以使用 col 名称触发 api 调用进行排序?

更新:

访问器函数如下所示:

在此处输入图像描述

标签: javascriptreactjsreact-table

解决方案


保留accessor作为函数有助于处理一些其他功能,对于这个用例,尝试在列配置中保留另一个键(例如 sortByBE)。


推荐阅读