'ag-grid: How to sort set-filter values after translation while keeping the raw (untranslated) filter values

I have a problem using ag-grid and set filters.

  • The values of the set-filter are a list of enums (strings) received from the backend
  • We translate these enum values to different languages using valueFormatter
  • Now we want the set filter to be sorted according to the translated texts

Here is a snapshot of our filter params:

filterParams: {
   values: this.filterValuesFromBackend,
   valueFormatter: (params: ValueFormatterParams) => this.translate(params.value)
}

As far as I can see sorting is done using unformatted values (enum values) and value formatting (here: translation) happens afterwards.

Important: Our table is a server side table where we use the filters in database queries. Just using already translated texts as filter set values is no solution: We need the raw enum values from the set filter for database filtering afterwards.

Our current workaround ist to use a comparator which processes many additional translations just for sorting:

filterParams: {
   ...
   comparator: (a: string, b: string) => {
               const textA = this.translate(a);
               const textB = this.translate(b);
               return textA.localeCompare(textB);
   }
},

Here is a simple example:

  • Filter values: bike, car, bus
  • Frontend display translated and sorted values:
    • in Englisch: bike, bus, car (alphabetic order)
    • in German: Auto, Bus, Fahrrad (alphabetic order, English: car, bus, bike)
    • in French: bus, vélo, voiture (alphabetic order, English: bus, bike, car)
  • Backend received "red", "green" and "blue" for database filtering

Do I miss something? Is using valueFormatte correct for translation tasks?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source