本文介绍了无法在 Gatsby 站点的 Ant Design Table 中对列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Gatsby 站点中实现了一个 Ant 设计表.我正在从 graphql 中提取数据.到目前为止,一切都运行良好.数据显示正常,分页正常等

I have implement an Ant Design Table in a Gatsby site. I am pulling in the data from graphql. So far everything has worked just fine. The data is displaying properly, pagination works, etc.

现在我想添加对列进行排序的功能.为此,我按如下方式设置表和列:

Now I want to add the ability to sort the columns. To do so, I set up the table and columns as follows:

<Table
  dataSource={data.allNewsFeed.edges}
  onChange={onChange}
  rowSelection={rowSelection}
  rowKey="id"
>
  <Column
    title="Title"
    dataIndex="node.title"
    key="title"
    sorter={(a, b) => a.node.title - b.node.title}
    sortDirections={["descend", "ascend"]}
  />
</Table>

现在,用于对列进行排序的图标确实出现了,但是当我单击它时没有任何反应.

Now, the icon for sorting the column does shows up, but nothing happens when I click on it.

如果我从排序器函数中删除 .node 也会发生同样的事情:sorter={(a, b) =>a.title - b.title}.

Same thing happens if I remove .node from the sorter function: sorter={(a, b) => a.title - b.title}.

所以,我被卡住了 - 知道为什么这不起作用以及如何解决它吗?

So, I am stuck -- any idea why this is not working and how to fix it?

谢谢.

推荐答案

@norbitrial的回答是正确的,仅供参考这是一个通用排序器(用于数字和字符串):

@norbitrial's answer is correct, as for reference here is a generic sorter (for numbers and strings):

const sorter = (a, b) => (isNaN(a) && isNaN(b) ? (a || '').localeCompare(b || '') : a - b);
// Usage example with antd table column
[
  {
    title: 'Status',
    dataIndex: 'status',
    key: 'status',
    width: '10%',
    // status can be Number or String
    sorter: (a, b) => sorter(a.status, b.status),
    render: Name
  }
]

这篇关于无法在 Gatsby 站点的 Ant Design Table 中对列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 02:57