AngularJS search with multiple textboxes

时间:2015-10-29 15:46:25

标签: angularjs angularjs-ng-repeat

I want to do a search of a table with multiple textboxes. The user should be able to enter address in the address box and search the table my address, and enter city in the city search box and search the table by city. I can't get it to work, I'm getting the error message: Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

Here's my html:

<table>
    <thead>
        <tr>
            <th>
                Address
            </th>
            <th>
                City
            </th>
        </tr>
        <tr>
            <td>
                <input ng-model="vm.search_address" />
            </td>
            <td>
                <input ng-model="vm.search_city" />
            </td>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="search in searchesFound = ( vm.searches | filter: {address: search_address, city: search_city})">
            <td>
                {{ search.address }}
            </td>
            <td>
                {{ search.city }}
            </td>
        </tr>
    </tbody>
</table>

ANd here's my controller:

(function () {   

angular.module('crm.ma')        
    .controller('AdvancedSearchCtrl', function () {
        var vm = this;

        vm.search_address = "";
        vm.search_city = "";


        vm.searchs = [
            {
                address: '202 This St',
                city : 'Columbus'
            },
            {
                address: '205 That St',
                city: 'Dayton'
            }
        ]

    });
})();

Any clues on what I'm doing wrong?

2 个答案:

答案 0 :(得分:0)

You forgot the vm object. In your filter vm.search_city and vm.search_address should be used:

<tr ng-repeat="search in vm.searchs | filter: {address: vm.search_address, city: vm.search_city}">

答案 1 :(得分:0)

看看这个:

var app = angular.module('ngApp', []);

app.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.smartphones = [
        {brand: 'Apple', model: 'iPhone 4S', price: '999'},
        {brand: 'Samsung', model: 'SIII', price: '888' },
        {brand: 'LG', model: 'Optimus', price: '777'},
        {brand: 'htc', model: 'Desire', price: '666'},
        {brand: 'Nokia', model: 'N9', price: '555'}
    ];
    
    $scope.search = function(){
        angular.forEach($scope.smartphones, function(value, key) {
            var brand = ($scope.queryBrand? $scope.queryBrand.toLowerCase():' ');
            var model = ($scope.queryModel? $scope.queryModel.toLowerCase():' ');
           
            value.show = (value.brand.toLowerCase().indexOf(brand) > -1 || 
                          value.model.toLowerCase().indexOf(model) > -1 || (brand == ' ' && model == ' '));
            
             console.log(!brand && !model)
        });
    }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

    <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.0/css/bootstrap-combined.min.css" rel="stylesheet">


<div ng-app="ngApp" ng-controller="MainCtrl" class="container">

<input class="span4 pull-right" type="text" placeholder="Filter by brand" ng-model="queryBrand" ng-change="search()">
<input class="span4 pull-right" type="text" placeholder="Filter by model" ng-model="queryModel" ng-change="search()">

<div class="row"> 
    <table id="results" class="table table-striped table-bordered table-hover">
        <thead>
        <tr>
            <th>Brand</th>
            <th>Model</th>
            <th>Price</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="smartphone in smartphones" ng-init="smartphone.show=true" ng-show="smartphone.show">
            <td id="brand">{{smartphone.brand}}</td>
            <td id="model">{{smartphone.model}}</td>
            <td id="price">{{smartphone.price | currency}}</td>
        </tr>
        </tbody>
    </table>
</div>
  </div>