【问题标题】:Dynamically add filter options in angularjs在angularjs中动态添加过滤器选项
【发布时间】:2014-02-10 12:42:49
【问题描述】:

您好,我有一个小项目,我想从多个动态添加的文本字段中执行搜索。

这是我添加搜索字段的方式:

<div class="form-group" ng-repeat="choice in choices">
     <button ng-show="showAddChoice(choice)" ng-click="addNewChoice()">Add another choice</button>
     <input type="text" ng-model="choice.name" name="" placeholder="Search criteria">
</div>

后来我有一个带有 ng-repeat 的表,这就是那部分:

    <tr ng-repeat="todo in todos | filter: {filter from all fields}">
     .......
    </tr>

我想要做的是使用所有动态添加的搜索字段过滤内容。

【问题讨论】:

  • 你能为此提交一个 jsFiddle。我还没有测试过,但是尝试 ng-repeat="todo in todos | filter: selection" 可能会起作用。
  • 这是一个jsFiddle。并且没有过滤器:选择不起作用:/

标签: javascript angularjs angularjs-filter


【解决方案1】:

您必须创建自己的过滤器来处理它。我已经开始让你开始了。

        $scope.myFilter = function(input){
            for(var key in input){
                for(var x = 0; x < $scope.choices.length; x++){
                    if(input[key] == $scope.choices[x].name){
                     return true;   
                    }
                }
            }
            return false;
        }

这里是输出的jsFiddle:http://jsfiddle.net/wsPrv/

【讨论】:

    【解决方案2】:

    与其使用过滤器,不如自己在控制器中进行过滤。 Here 是解决方案的更新小提琴。在第一个文本框中,将choice1 替换为“some”,您将看到显示文本“Some stuff”的待办事项。

    请参阅下面的相关部分。有关详细信息,请参阅小提琴。

            $scope.$watch('choices', function(newValue) {
                $scope.DisplayedTodos = [];
                // Filter items here and push to DisplayedTodos. Use DisplayedTodos to display todos
            }, true);
    

    【讨论】:

    • 是的,但这仍然不能满足我的需要。我需要通过第一个过滤器过滤的内容,然后用第二个、第三个等再次过滤这些结果。
    • 您好,您只需要调整 $watch 中的代码以按照您想要的方式过滤数据。我可以更改代码并为您完成,但这实际上不是 stackoverflow.com 的目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多