简体   繁体   English

ag-grid 比较器 function 未被触发

[英]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.我将 ag-grid 9.X 与 angularJs 一起使用,当尝试为日期列设置比较器 function 时,它不会被触发。

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

I also set enableSorting: true .我还设置enableSorting: true Is there any chance that the feature is not available in that version of ag-grid?该功能是否有可能在该版本的 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:因此,由于我无法让比较器 function 工作,我使用以下技巧对日期列进行排序:

  • 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:确保输入网格的原始数据具有该列的 Javascript 日期对象而不是字符串(以便 ag-grid 的排序算法(使用“>”运算符)可以将这些日期作为日期而不是字符串进行比较)示例:

     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.事实上,我通过这个问题了解到的是,ag-grid 排序算法不会作用于 cellRenderer 渲染的数据,也不会作用于 valueGetter。 It actually uses the original data.它实际上使用原始数据。

  • Optional: These columns will be rendered in a Javascript date object format .可选:这些列将以 Javascript 日期 object格式呈现。 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.如果您想要这些日期的特定格式,您可以使用 cellRenderer function 或该列的 valueGetter 将这些日期解析回原始格式。 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?您使用的是 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',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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