Mor*_*ana 6 reactjs react-table
React-table 正在对小数进行排序,如下所示:
我的猜测是,虽然我从服务器接收数字,但 react-table 将它们作为文本处理。所以,我像这样修改了访问器:
accessor: d => Number(d.Invoice_Weight).toFixed(2)
Run Code Online (Sandbox Code Playgroud)
但我一直在排序错误。
这是列的代码:
{
Header: () =>
<DropDownMenu
header={content[lang].Invoice_Weight}
openModal = {this.onOpenSelectColumnsModal}
/>,
id: 'Invoice_Weight',
sortable: true,
accessor: d => Number(d.Invoice_Weight).toFixed(2),
//width: 200,
getProps: () => {
return {
style: {
textAlign: 'right'
}
}
},
show: Invoice_Weight,
},
Run Code Online (Sandbox Code Playgroud)
正如其他答案中所建议的,问题是toFixed返回一个字符串,因此将使用字符串比较来完成排序。但是,在这种情况下,强制返回 number 将不起作用,因为那样您将丢失尾随0s,我猜您仍然需要。
另一种解决方案是使用自定义排序:
accessor: d => Number(d.Invoice_Weight).toFixed(2),
sortMethod: (a, b) => Number(a)-Number(b)
Run Code Online (Sandbox Code Playgroud)
您可能想要改进sortMethod以处理 NaN 和无穷大(如果有的话),但这是总体思路
您可以使用它来将字符串强制回数字,但只能在排序的上下文中而不影响显示的值
| 归档时间: |
|
| 查看次数: |
5144 次 |
| 最近记录: |