[英]antd table, disable unsorted option for a column (force sort)
要对列启用排序,请使用值为['ascend', 'descend']
sortDirections
['ascend', 'descend']
的sortDirections
参数。 使用此设置有 3 个排序选项:“升序”、“降序”、“未排序”。
有没有一种简单的方法来强制排序,删除未排序的选项?
例如:
给定一个未排序的数字列表 [1, 5 ,2 ,10] 。 目前,我可以使用值['ascend', 'descend']
sortDirections
['ascend', 'descend']
设置sortDirections
。 这将提供 3 个选项,升序排序、降序排序和未排序,例如与原始数字相同。
期望的行为:
我想强制排序,这意味着我不希望“未排序”选项可用。 如果用户单击列标题,则应按升序或降序对整个列进行排序。
来自文档但实际上很有帮助。
“您可以设置为 ['ascend', 'descend', 'ascend'] 以防止分拣机回到默认状态。”
我也一直在摸索,没有办法(截至目前)自定义列标题排序行为来忽略未排序状态。 此外,开发人员在这里明确表示他们想坚持这种方式:
https://github.com/ant-design/ant-design/issues/12905
@mkkekkonen 确保您在指向它之前使用过文档...
我最近想要类似的行为,我做了以下操作,效果很好。
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'],
}),
},
从文档:
sortDirections: ['ascend' | 'descend']
sortDirections: ['ascend' | 'descend']
为每列定义可用的排序方法,当设置在 table props 上时对所有列都有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.