UI网格中的自定义过滤器可过滤完全匹配

时间:2017-03-08 15:00:53

标签: angularjs angular-ui-grid ui-grid

我在UI中创建了一个自定义过滤器 - 一列的网格。 我想只过滤与应用过滤器完全匹配且不包含in的值。

以下是我的自定义过滤器控制器:

   myapp2.controller('activityFilterCtrl', function( $scope, $compile, $timeout ) {
var $elm;
$scope.showAgeModal = function() {
    $scope.listOfAges = ['TRL1','TRL2','TRL3','TRL4','TRL5','TRL6','Launch Gate','Alpha Gate','Beta Gate','H-O','Intermediate H-O','Internal Review','DEL','Evaluation','MIL'];




    $scope.gridOptions = { 
            data: [],
            enableColumnMenus: false,
            onRegisterApi: function( gridApi) {
                $scope.gridApi = gridApi;

                if ( $scope.colFilter && $scope.colFilter.listTerm ){
                    $timeout(function() {
                        $scope.colFilter.listTerm.forEach( function( activityType ) {
                            var entities = $scope.gridOptions.data.filter( function( row ) {
                                return row.activityType === activityType;
                            }); 

                            if( entities.length > 0 ) {
                                $scope.gridApi.selection.selectRow(entities[0]);
                            }
                        });
                    });
                }
            } 
    };

    $scope.listOfAges.forEach(function( activityType ) {
        $scope.gridOptions.data.push({activityType: activityType});
    });

    var html = '<div class="modal" ng-style="{display: \'block\'}"><div class="modal-dialog"><div class="modal-content"><div class="modal-header">Filter Ages</div><div class="modal-body"><div id="grid1" ui-grid="gridOptions" ui-grid-selection class="modalGrid"></div></div><div class="modal-footer"><button id="buttonClose" class="btn btn-primary" ng-click="close()">Filter</button></div></div></div></div>';
    $elm = angular.element(html);
    angular.element(document.body).prepend($elm);

    $compile($elm)($scope);

};

$scope.close = function() {
    var ages = $scope.gridApi.selection.getSelectedRows();
    $scope.colFilter.listTerm;
    $scope.colFilter.listTerm=[];
    ages.forEach( function( activityType ) {
        $scope.colFilter.listTerm.push( activityType.activityType );
    });

    $scope.colFilter.term = $scope.colFilter.listTerm.join(', ');
    $scope.colFilter.condition = new RegExp($scope.colFilter.listTerm.join('|'));

    if ($elm) {
        $elm.remove();
    }
};

}) }

这是我对该列的列def:

   {

                                     name: "Activity Type",
                                     field: 'activityType',
                                     visible: false,
                                     headerTooltip:'Activity Type',
                                     enableHiding: false,cellClass: 'scheduleData',
                                     minWidth:150,enableFiltering: true,
                                     filterHeaderTemplate: '<div class="ui-grid-filter-container" ng-repeat="colFilter in col.filters"><div activity-filter-model></div></div>',
                                     filter: {
                                            condition: function(searchTerm, activityType) {
                                              return activityType.indexOf(searchTerm) > -1
                                            }

                                              },
                                 },

如果我必须在控制器中使用正则表达式,同时应用col.Filter条件或者如果列def中的过滤条件有效,请指导我。

1 个答案:

答案 0 :(得分:1)

解决:

通过在自定义过滤器控制器中设置启动值:

$ scope.colFilter.condition = 2;