简体   繁体   English

antd 表,禁用列的未排序选项(强制排序)

[英]antd table, disable unsorted option for a column (force sort)

To enable sort on a column, using the sortDirections parameter with values ['ascend', 'descend'] .要对列启用排序,请使用值为['ascend', 'descend'] sortDirections ['ascend', 'descend']sortDirections参数。 With this setting there are 3 sorting options: 'ascend', 'descend', 'unsorted'.使用此设置有 3 个排序选项:“升序”、“降序”、“未排序”。

Is there a simple way to force sorting, remove the unsorted option?有没有一种简单的方法来强制排序,删除未排序的选项?

For example:例如:

Given a list of unsorted numbers [1, 5 ,2 ,10] .给定一个未排序的数字列表 [1, 5 ,2 ,10] 。 Currently, I can set sortDirections with values ['ascend', 'descend'] .目前,我可以使用值['ascend', 'descend'] sortDirections ['ascend', 'descend']设置sortDirections This will provide 3 options, sort in ascending order, sort in descended order AND unsorted, eg same as the original numbers.这将提供 3 个选项,升序排序、降序排序和未排序,例如与原始数字相同。

Desired behavior:期望的行为:

I would like to force sort, meaning I don't want the 'unsorted' option to be available.我想强制排序,这意味着我不希望“未排序”选项可用。 If the user clicks the column header, the whole column should be sorted either in ascending order or in descended order.如果用户单击列标题,则应按升序或降序对整个列进行排序。

From theDocumentation but actually helpful.来自文档但实际上很有帮助。

"You can set as ['ascend', 'descend', 'ascend'] to prevent sorter back to default status." “您可以设置为 ['ascend', 'descend', 'ascend'] 以防止分拣机回到默认状态。”

I've been poking around too and there is no way (as of now) of customizing the column header sorting behavior to ignore the unsorted state.我也一直在摸索,没有办法(截至目前)自定义列标题排序行为来忽略未排序状态。 Moreover, the developers clearly state that they want to sticky with this way here:此外,开发人员在这里明确表示他们想坚持这种方式:

https://github.com/ant-design/ant-design/issues/12905 https://github.com/ant-design/ant-design/issues/12905

@mkkekkonen make sure you used the docs before pointing to it... @mkkekkonen 确保您在指向它之前使用过文档...

I have recently wanted similar behavior, and I did the following and it works fine.我最近想要类似的行为,我做了以下操作,效果很好。

const [sort, setSort] = useState('ascend');

{
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      sorter: (a, b) => a.age - b.age,
      sortOrder: sort,
      onHeaderCell: () => ({
        onClick: () => setSort(sort === 'ascend' ? 'descend' : 
      'ascend'),       
      sortDirections: ['ascend', 'descend', 'ascend'],
      }),
},

From thedocumentation :文档

sortDirections: ['ascend' | 'descend'] sortDirections: ['ascend' | 'descend'] defines available sort methods for each columns, effective for all columns when set on table props. sortDirections: ['ascend' | 'descend']为每列定义可用的排序方法,当设置在 table props 上时对所有列都有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM