Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sorting key insensitive in ag-grid?

I am working in some grids and I notice that the sorting on all of them is key sensitive is there any way to change that. This is a part of my code.

 columnDefs = [
{
  headerName: 'Id', field: 'id', sort: 'asc', sortable: true, filter: true,
  checkboxSelection: true, resizable: true,
},
{
  headerName: 'Name', field: 'name', sortable: true, filter: true,
  checkboxSelection: false, editable: true, resizable: true,
},
{
  headerName: 'Description', field: 'description', sortable: true, filter: true,
  checkboxSelection: false, editable: true, resizable: true,
},
  ];

Thank you for any possible given help.

This is how i have implement the solution by ##wentjun##:

  constructor(private dialog: MatDialog, private adminService: AdminService) {}

  columnDefs = [
    {
      headerName: 'Id', field: 'id', sortable: true, filter: true,
      checkboxSelection: true, resizable: true,
    },
    {
      headerName: 'Name', field: 'name', sortable: true, filter: true,
      checkboxSelection: false, editable: true, resizable: true,
      comparator: this.customComparator,
    },
    {
      headerName: 'Description',  field: 'description', sortable: true, filter: true,
      checkboxSelection: false, editable: true, resizable: true,
    },
  ];

  customComparator(valueA, valueB) {
    return valueA.toLowerCase().localeCompare(valueB.toLowerCase());
  }
like image 861
Ilia Tapia Avatar asked May 23 '19 10:05

Ilia Tapia


People also ask

How do you hide the sort order indicator in Ag grid?

Easiest way is to override the default css header template used by ag-grid. You'll note that i have removed the span tag "eSortOrder" in my template code. Apply same approach to other column for whom you want to remove sort order reference. Handle to css template also gives you power to manipulate the header display.

How do I set the default sorting on Ag grid?

Sorting is made available very easy in Ag-Grid. All you have to do is add a property sortable: true in column defination. Default sorts: If you are wishing to have to default sorting order, then add sortingOrder:['desc'] which will give you default descending sorting order.

How do I turn off filtering on Ag grid?

Enable filtering by setting grid property enableFilter=true. This turns on filtering on all columns. To turn off filtering for particular columns, set suppressFilter=true on the individual column definition.

How does sorting work in ag grid?

Enable Sorting Turn sorting on for the grid by enabling sorting in the grid options. Sort a column by clicking on the column header. To do multi-column sorting, hold down shift while clicking the column header.


1 Answers

Note that by now you can just set the accentedSort property in the GridOptions to true to achieve case-insensitive sorting. See here for more information.

If you use the custom comparator solution from wentjun please make sure to only apply it to string columns or, when applying to all columns, check if the value is a string. Otherwise the sorting won't work anymore on number based columns:

const customLowerCaseComparator = (valueA, valueB) => {
  if (typeof valueA === 'string') {
    return valueA.toLowerCase().localeCompare(valueB.toLowerCase());
  }

  return (valueA > valueB? 1 : (valueA < valueB ? -1 : 0));
};
like image 150
Hannes Avatar answered Oct 14 '22 19:10

Hannes