Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于选择值angularjs的过滤器_Javascript_Angularjs - Fatal编程技术网

Javascript 基于选择值angularjs的过滤器

Javascript 基于选择值angularjs的过滤器,javascript,angularjs,Javascript,Angularjs,我想根据所选值编写一个过滤器。 例如,如果我选择名称选项,我可以使用“输入文本”搜索输入了相同名称的所有用户。是否可以使用类似的语法“filter:{serach:name}” ` 名称 地址 电子邮件 名称 地址 电子邮件 ` 试着这样做: <tbody> <tr data-ng-repeat="user in usersList | filter:{name:user.name, address: user.address}"> <td

我想根据所选值编写一个过滤器。 例如,如果我选择名称选项,我可以使用“输入文本”搜索输入了相同名称的所有用户。是否可以使用类似的语法“filter:{serach:name}”

`


名称
地址
电子邮件
名称
地址
电子邮件
`
试着这样做:

<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>

可能会创建一个自定义过滤器,接受您的选择选项值作为参数和项目数组,以便您可以根据该选项和文本输入上的值进行过滤。对于你所需要的可能是过度的

我糟糕的例子是:

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;
        }
    };
});
app.controller(“CurrencyController”,函数($scope,$http,$filter){
$scope.option='';//选择值
$scope.search='';//输入文本
$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=函数(){
//如果筛选器没有值,则创建usersList的副本以保持原始状态
$scope.usersListCopy=angular.copy($scope.usersList);
$scope.usersListCopy=$filter($aFilter”)($scope.usersListCopy、$scope.search、$scope.option);
};
}).
过滤器('aFilter',函数(){
返回函数(输入、搜索、搜索字段){
如果(searchOnField=''| | searchingFor=''){
返回输入;
}否则{
var returnItems=[];
对于(var a=0;a

在我的html中,我只是在表行上迭代usersListCopy。并在开始的某个地方使用ng init调用filterOut,以便在usersListCopy上有数据。还可以在元素上设置ng change=“filterOut();”。

Ok,但此搜索仅根据用户名进行。我想根据姓名、电子邮件或地址进行更具扩展性的搜索,只需将其他属性添加到筛选器中即可。更新了我的答案谢谢马克西姆斯。这是一个很好的解决方案,但我必须写3个输入(姓名、地址和电子邮件)。在我的示例中,我想用一个select来编写一个输入文本,在这种情况下这是可能的吗?您想在html中有一个输入吗?
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;
        }
    };
});