问题
I am working with the ng-grid in Angular js. I have this line of code which defines the type of filter added.
columnDefs: [{field:'date', displayName:'Birth Date', cellFilter:'date:\"dd MMM yyyy\"'},
{field:'pre', displayName:'Prev', cellFilter:'number'},
{field:'fct', displayName:'Fct', cellFilter:'number'},
{field:'act', displayName:'Act', cellFilter:'number'}
{field:'imp', displayName:'Important', cellFilter:'number'}]
};
the Important column has values "H" "L" and "M". By normal(alphabetical) sorting I can achieve a sort of H-L-M but is there a way to define a filter that sorts as "H-M-L".
Any help is appreciated!! Thanks :)
回答1:
Use a custom sort function:
var result = 1;
var test="HML";
function impSortFn(a, b) {
a=test.indexOf(a);
b=test.indexOf(b);
if (a == b) return 0;
if (a < b) return -1;
return result;
}
$scope.gridOptions = {
data: 'myData',
columnDefs: [{
field: 'name',
displayName: 'Name'
}, {
field: 'age',
displayName: 'Age'
}, {
field: 'imp',
displayName: 'Important',
sortFn: impSortFn
}]
See this Plunker
来源:https://stackoverflow.com/questions/23211914/custom-cellfilter-in-angular-js