要解决这个问题,我们需要实现一个自定义set filter并使用过滤器值对象。
TranslatedSetFilter
。import React, { Component } from "react";
import { SetFilter } from "ag-grid-react";
class TranslatedSetFilter extends SetFilter {
// Override the mapping function to sort the translated values
mapCustomSortValues = (values) => {
// This code assumes that the translated value is located in the `translation` property of the filter value object
// You may need to adjust it depending on the structure of your filter value object
return values
.map((v) => ({ ...v, translation: this.props.translate(v.value) }))
.sort((a, b) => a.translation.localeCompare(b.translation))
.map((v) => v.value);
};
}
export default TranslatedSetFilter;
这个组件扩展了AgGridReact中的SetFilter,同时将所需的排序逻辑添加到mapCustomSortValues
函数中。这个函数使用this.props.translate
来翻译并排序过滤器值。我们将排序后的值中的translation属性从过滤器值对象中删除,以便保留原始的未翻译值。
TranslatedSetFilter
并将其传递给AgGridReact
组件的frameworkComponents
属性。import React, { Component } from "react";
import { AgGridReact } from "ag-grid-react";
import TranslatedSetFilter from "./TranslatedSetFilter";
class MyGridComponent extends Component {
render() {
const { rowData } = this.props