简体   繁体   English

根据选择值angularjs进行过滤

[英]Filter based on select value angularjs

I want to write a filter according selected value. 我想根据所选值编写一个过滤器。 For example, if i select name option i can search using "input text" all user that have the same name entered. 例如,如果我选择名称选项,则可以使用“输入文本”搜索所有输入相同名称的用户。 is it possible to use a similar syntax "filter:{serach:name}". 是否可以使用类似的语法“ filter:{serach:name}”。

` `

<input type="text" data-ng-model="search" /> 
<select name="option">
  <option value="name">name</option>
  <option value="address">address</option>
  <option value="email">email</option>
</select>


        <table>
            <thead>
                <tr>
                    <th>name</th>
                    <th>address</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <tr data-ng-repeat="user in usersList | filter:{option:search}">
                    <td><span data-ng-bind="user.name"></span></td>
                    <td><span data-ng-bind="user.address"></span></td>
                    <td><span data-ng-bind="user.email"></span></td>
                </tr>
            </tbody>
        </table>
    </div>
    </div>
</div>`

Try like that: 像这样尝试:

<tbody>
    <tr data-ng-repeat="user in usersList | filter:{name:user.name, address: user.address}">
        <td><span ng-model="user.name"></span></td>
        <td><span ng-model="user.address"></span></td>
    </tr>
</tbody>

Maybe creating a custom filter that accepts your select option value as a parameter and the array of items so that you can filter by that option and the value on the text input. 也许创建一个自定义过滤器,以接受您的选择选项值作为参数和项目数组,以便您可以通过该选项和文本输入中的值进行过滤。 May be overkill for what you need. 可能对于您需要的东西来说是过大的。

My lousy example: 我糟糕的例子:

app.controller("CurrencyController", function ($scope, $http, $filter) {
    $scope.option = '';//select value
    $scope.search = '';//input text
    $scope.usersList = [
        {'name':'luis', 'id':3, 'address':'somewhere 1'},
        {'name':'charles', 'id':4, 'address':'somewhere 2'},
        {'name':'john', 'id':5, 'address':'somewhere 1'},
        {'name':'mike', 'id':6, 'address':'somewhere 3'}
    ];

    $scope.filterOut = function(){
//create a copy of usersList to keep original if no value for filter
        $scope.usersListCopy = angular.copy($scope.usersList);
        $scope.usersListCopy = $filter('aFilter')($scope.usersListCopy, $scope.search, $scope.option);
    };

}).
filter('aFilter', function() {
    return function(input, searchingFor, searchOnField) {
        if ( searchOnField == '' || searchingFor == '' ) {
            return input;
        } else {
            var returnItems = [];

            for (var a = 0; a < input.length; a++){
                //cycle thru every item key
                if(input[a][searchOnField] == searchingFor){
                    returnItems.push(input[a]);
                }

            }
            console.log(returnItems);
            return returnItems;
        }
    };
});

On my html i just iterate usersListCopy on the table row. 在我的html上,我只遍历表行上的usersListCopy。 And call filterOut with ng-init somewhere at the start so that there is data on usersListCopy. 并在开始的某个位置使用ng-init调用filterOut,以便usersListCopy上有数据。 Also set ng-change="filterOut();" 同时设置ng-change =“ filterOut();” on element. 在元素上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM