简体   繁体   中英

ag-grid comparator function not being triggered

I'm using ag-grid 9.X with angularJs and when trying to set the comparator function for a date column, it doesn't get triggered.

      {
      headerName: 'Date',
      field: 'lastDate',
      width:100,
      sortable: true,
      comparator: dateComparator,
      }

I also set enableSorting: true . Is there any chance that the feature is not available in that version of ag-grid?

Thanks !

So, since I was not able to get the comparator function to work, I used the following tips to sort date columns:

  • Making sure that the original data that feeds into the grid has Javascript Date objects for that column and not strings (so that ag-grid's sort algorithm (which uses the ">" operator) can compare these dates as dates and not strings) Example:

     data.forEach(function(obj,i){ let dateStr = obj.date let year = dateStr.substring(0, 4) let month = dateStr.substring(5, 7) let day = dateStr.substring(8, 10) let time = dateStr.substring(11, 21) let date = new Date(year+"-"+month+"-"+day+" "+time) data[i].date = date }) $scope.gridConfig.data = data

In fact, what I've come to realize through this issue is that ag-grid sorting algorithm doesn't act on the data rendered by cellRenderer nor the valueGetter. It actually uses the original data.

  • Optional: These columns will be rendered in a Javascript date object format . If you'd like to have a specific format for these dates, you can use the cellRenderer function or a valueGetter for that column to parse these dates back into the original format. And no worries, sorting won't be impacted as it will still use the date objects from the original data.

I think implementation of your comparator is the problem. are you using a static function? if not try this and see if it works.

{
    headerName: 'Date',
    field: 'lastDate',
    width:100,
    sortable: true,
    comparator: (a, b) => {
        //implement your comparator here.
    },
}

You have to add the following:

sort: 'asc',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM