ag-grid 使用隐藏列 B 按列 A 排序

ag-grid sort by column A using hidden column B

有没有办法按 A 列排序(用户点击 A 列 header)但在后台使用 B 列?

例如,我有一个显示用户名的“名称”列。但是后来我有一个隐藏的列“一般人群中的名字频率”。我想显示常规的“名称”,但在幕后按另一列排序。

创建您自己的客户比较器并使用 Custom Sorting 将其设置到您的 name 字段。

通过自定义排序,您可以访问每一行中的所有数据,您可以在其中选择应该放在哪里。

var columnDefs = [
  { field: 'name', comparator: customComparator },
  { field: 'name frequency in general population' },
];

function customComparator(valueA, valueB, nodeA, nodeB, isInverted) {
  const nodeAValue = nodeA.data['name frequency in general population'];
  const nodeBValue = nodeB.data['name frequency in general population'];
  return (nodeAValue > nodeBValue) ? 1 : -1;
}