Custom “cellfilter” in Angular js

五迷三道 提交于 2019-12-25 12:12:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!