如何在单个搜索框中使用角度中的多个参数进行搜索?

时间:2015-07-14 12:30:48

标签: javascript json angularjs

我有一个搜索字段,我想对它应用一个过滤器,我希望我的json数据被多个参数搜索,我想以这样的方式指定参数,以便我可以在时间更改它们api调用,这里的代码按名称和分支类别,但我只能按名称搜索,我不知道如何解决它,请帮助

这是我的代码

的index.html

<!DOCTYPE HTML>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="utf-8">
    <title>Dynamic Pagination w/ Filtering</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Kim Maida">

    <!-- JS Libraries -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js" type="text/javascript"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js" type="text/javascript"></script>

    <!-- Angular Scripts -->
    <script src="script.js" type="text/javascript"></script>

    <!-- Bootstrap -->
    <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />

</head>

<body>

    <div ng-controller="PageCtrl">
        <label>Search:</label> <input type="text" ng-model="search.name" placeholder="Search" />
        <br />


        <h1>Items</h1>
        <ul>
            <li ng-repeat="item in filtered = items | filter:search | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">{{item.name}}  {{item.branch}}</li>

        </ul>

<!--         <pagination page="currentPage" max-size="noOfPages" total-items="totalItems" items-per-page="entryLimit"></pagination> -->
 <ul class="pagination">
                        <li ng-class="DisablePrevPage()">
                            <a href ng-click="prevPage()">« Prev</a>
                        </li>
                        <li ng-repeat="n in range()" ng-class="{active: n == currentPage}" ng-click="setPage(n)">
                            <a href="#">{{n+1}}</a>
                        </li>
                        <li ng-class="DisableNextPage()">
                            <a href ng-click="nextPage()">Next »</a>

                        </li>
                    </ul>

</body>
</html>

这是我的app.js代码

var app = angular.module('myApp', ['ui.bootstrap']);

app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start;
            return input.slice(start);
        }
        return [];
    };
});

app.controller('PageCtrl', ['$scope', 'filterFilter', function ($scope, filterFilter) {
    $scope.items = [{
        "name": "name 1",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "West"
    }, {
        "name": "name 2",
        "category": [{
            "category": "engineering"
        }],
        "branch": "West"
    }, {
        "name": "name 3",
        "category": [{
            "category": "management"
        }, {
            "category": "engineering"
        }],
        "branch": "West"
    }, {
        "name": "name 4",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "West"
    }, {
        "name": "name 5",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "name 6",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "name 7",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "name 8",
        "category": [{
            "category": "business"
        }],
        "branch": "West"
    }, {
        "name": "name 9",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "name 10",
        "category": [{
            "category": "management"
        }],
        "branch": "East"
    }, {
        "name": "name 11",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "name 12",
        "category": [{
            "category": "engineering"
        }],
        "branch": "West"
    }, {
        "name": "name 13",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "West"
    }, {
        "name": "name 14",
        "category": [{
            "category": "engineering"
        }],
        "branch": "East"
    }, {
        "name": "name 15",
        "category": [{
            "category": "management"
        }, {
            "category": "engineering"
        }],
        "branch": "East"
    }, {
        "name": "name 16",
        "category": [{
            "category": "management"
        }],
        "branch": "West"
    }, {
        "name": "name 17",
        "category": [{
            "category": "management"
        }],
        "branch": "East"
    }, {
        "name": "name 18",
        "category": [{
            "category": "business"
        }],
        "branch": "West"
    }, {
        "name": "name 19",
        "category": [{
            "category": "business"
        }],
        "branch": "West"
    }, {
        "name": "name 20",
        "category": [{
            "category": "engineering"
        }],
        "branch": "East"
    }, {
        "name": "Peter",
        "category": [{
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "Frank",
        "category": [{
            "category": "management"
        }],
        "branch": "East"
    }, {
        "name": "Joe",
        "category": [{
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "Ralph",
        "category": [{
            "category": "management"
        }, {
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "Gina",
        "category": [{
            "category": "business"
        }],
        "branch": "East"
    }, {
        "name": "Sam",
        "category": [{
            "category": "management"
        }, {
            "category": "engineering"
        }],
        "branch": "East"
    }, {
        "name": "Britney",
        "category": [{
            "category": "business"
        }],
        "branch": "West"
    }];

    // create empty search model (object) to trigger $watch on update
    $scope.search = {};

    $scope.resetFilters = function () {
        // needs to be a function or it won't trigger a $watch
        $scope.search = {};
    };

    // pagination controls
    // $scope.currentPage = 1;
    // $scope.totalItems = $scope.items.length;
    // $scope.entryLimit = 2; // items per page
    // $scope.noOfPages = Math.ceil($scope.totalItems / $scope.entryLimit)-1;
             $scope.entryLimit = 2;
            $scope.currentPage = 0;
            $scope.totalLeadItems = $scope.items.length;

            $scope.range = function() {
              var rangeSize = 3;
              var ps = [];
              var start;

              start = $scope.currentPage;
              if ( start > $scope.pageCount()-rangeSize ) {
                start = $scope.pageCount()-rangeSize+1;
              }

              for (var i=start; i<start+rangeSize; i++) {
                ps.push(i);
              }
              return ps;
            };

            $scope.prevPage = function() {
              if ($scope.currentPage > 0) {
                $scope.currentPage--;
              }
            };

            $scope.DisablePrevPage = function() {
              return $scope.currentPage === 0 ? "disabled" : "";
            };

            $scope.pageCount = function() {
              return Math.ceil($scope.totalItems / $scope.entryLimit)-1;
            };

            $scope.nextPage = function() {
              if ($scope.currentPage < $scope.pageCount()) {
                $scope.currentPage++;
              }
            };

            $scope.DisableNextPage = function() {
              return $scope.currentPage === $scope.pageCount() ? "disabled" : "";
            };

            $scope.setPage = function(n) {
              $scope.currentPage = 0;
            };


    // $watch search to update pagination
    $scope.$watch('search', function (newVal, oldVal) {
        $scope.filtered = filterFilter($scope.items, newVal);
        $scope.totalItems = $scope.filtered.length;
        $scope.noOfPages = Math.ceil($scope.totalItems / $scope.entryLimit);
        $scope.currentPage = 1;
    }, true);
}]);

1 个答案:

答案 0 :(得分:0)

你刚刚错过了model.object:

使用 过滤器:search.name

<li ng-repeat="item in filtered = items | filter:search.name | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">{{item.name}}  {{item.branch}}</li>