显示 smart-table 的所有应用过滤器

Show all applied filters of smart-table

我想在 div 中显示 smart-table 的所有应用过滤器。我的想法是编写一个监视 ctrl.tableState().search.predicateObject 属性:

的指令
directivesModule.directive('filterBar', function() {
return {
    restrict:'E',
    require:'^stTable',
    template: '<div class="row"><div class="col-xs-12 filters"><span class="filters-header">Filters: </span><span class="tag label label-default" ng-repeat="filter in filters"><span>{{filter.name}}</span><a><i class="remove glyphicon glyphicon-remove-sign glyphicon-white"></i></a> </span> </div> </div>',
    scope: true,
    link:function(scope, element, attr, ctrl){
        scope.$watchCollection(ctrl.tableState().search.predicateObject, function(newVal, oldVal) {
            console.log(newVal, oldVal);
        });
    }
};
});

然而 console.log 只被调用一次,两次显示未定义。

我使用以下代码添加过滤器:

directivesModule.directive('addFilter', function() {
    return {
        restrict:'A',
        require:'^stTable',
        scope: {
            criterion: '@',
            value: '@'
        },
        link:function(scope, element, attr, ctrl){

            element.bind('click', function() {
                scope.$apply(function() {
                    ctrl.search(scope.value, scope.criterion);
                    console.log(ctrl.tableState().search.predicateObject);
                });
            });
        }
    };
});

我不太明白你为什么要在这里看一个合集

为什么不做这样的事情:

DirectivesModule.directive('filterBar', function() {
  return {
    restrict:'E',
    require:'^stTable',
    template: 'you template',
    scope: true,
    link:function(scope, element, attr, ctrl){
      scope.$watch(function () {
          return ctrl.tableState().search;
      }, function (newValue, oldValue) {
         if(newValue.predicateObject){
           console.log(newValue.predicateObject)
         }
      }, true);
    }
   };
});

作为解决方案,我选择了 ngTable。这使它变得容易得多。我现在使用此代码:

"use strict";
var directivesModule = angular.module("directives");

directivesModule.directive('addFilter', function() {
return {
    restrict:'A',
    scope: {
        criterion: '@',
        value: '@',
        filter: '='
    },
    link:function(scope, element){

        element.bind('click', function() {
            scope.$apply(function() {
                scope.filter[scope.criterion] = scope.value;
            });
        });
    }
};
});