[英]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.它实际上使用原始数据。
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.