显示所有应用的智能表过滤器

时间:2015-03-19 10:57:27

标签: angularjs 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);
                });
            });
        }
    };
});

2 个答案:

答案 0 :(得分:1)

作为解决方案,我去了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;
            });
        });
    }
};
});

答案 1 :(得分:0)

我不确定你为什么在这里观看收藏

为什么不做类似的事情:

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);
    }
   };
});
相关问题