sorting - PrimeReact DataTable 使用 sortFunction 对列进行预排序
问题描述
我一直在尝试设置与此类似的预排序:https ://www.primefaces.org/primereact/showcase/#/datatable/sort
我面临的问题是,我想按具有我定义的 sortFunction 的列对渲染进行排序。
我有一个可用的 sortFunction,但如果我给 DataTable 一个 sortField,它就不会使用它。 在渲染时,空值在前面,这是可排序的默认排序,但 单击标题会使用正确的 sortFunction,它按字母顺序排序并将空值发送到末尾
是否有可能以某种方式解决这个问题,或者我应该放弃预分类?
编辑:我对数据进行了预排序并以这种方式将其提供给 DataTable,所以它可以工作,但它远非优雅,它也没有显示列在渲染时具有排序的箭头,但它可能对某人有帮助:
const dataTableSortFunction = (value: any[]) => (event: ColumnSortParams): any[] => {
return [...value].sort((data1, data2) => {
const value1 = data1[event.field];
const value2 = data2[event.field];
if (value1 == null && value2 == null) return 0;
if (value2 == null) return -1;
if (value1 == null) return 1;
if (typeof value1 === "string" && typeof value2 === "string")
return value1.localeCompare(value2) * event.order;
if (value1 < value2) return -1 * event.order;
if (value1 > value2) return 1 * event.order;
return 0;
});
};
const myProductSort = dataTableSortFunction(products);
const preSortedProducts = myProductSort({ field: "category", order: -1 });
...
<DataTable value={preSortedProducts} className="p-datatable-sm">
<Column field="code" header="Code" sortable></Column>
<Column field="name" header="Name" sortable></Column>
<Column
field="category"
header="Category"
sortable
sortFunction={myProductSort}
></Column>
<Column field="quantity" header="Quantity" sortable></Column>
<Column field="price" header="Price" sortable></Column>
</DataTable>
解决方案
推荐阅读
- javascript - 如何编辑和删除一行?
- plot - (过度)在折线图上绘制点
- razor - Razor 组件标签助手实际上并未加载 razor 组件
- python - 写入 Parquet 时 PySpark 出现异常
- vue.js - Vue:等到父函数完成运行
- ubuntu - 使用 ImageMagick 从文本生成图像?
- haskell - 无法在堆栈项目中使用 nix 构建 hzk(haskell zookeeper)
- python - Django 不读取通过 AJAX 发布请求传递的 JSON 数据
- javascript - 散景触发 js_on_change 以首次显示图形
- github - 无法从 GitHub API 触发调度