2016-04-04 88 views
0

我想知道是否可以在AngularJS中执行类似下面的代码。目标是通过所有ID> =搜索ID来过滤数组。我可以在内嵌搜索还是必须在JavaScript中以自定义过滤器的形式进行搜索?在AngularJS中按数字过滤数组

<div style="margin-top:5px;margin-left:30px;"> 
    <div> 
     <div>ID</div> 
     <span class="glyphicon glyphicon-search"></span>&nbsp; 
     <input type="text" ng-model="listFoodItems.searchid" /> 
    </div> 
    <table class="table table-responsive"> 
     <thead> 
      <tr> 
       <th>ID</th> 
       <th>Description</th> 
       <th>Discount</th> 
      </tr> 
     </thead> 
     <tfoot></tfoot> 
     <tbody> 
      <tr ng-repeat="row in listFoodItems.fullitemdescs | filter: EntryId >= searchid"> 
       <td>{{row.EntryId}}</td> 
       <td>{{row.ItemDesc}}</td> 
       <td>{{row.ItemDisc}}</td> 
      </tr> 
     </tbody> 
    </table> 
</div> 
+0

你可以有DIV(NG-IF =“行。 EntryId> listFoodItems.searchid“)来包装tds –

回答

2

最佳方式使自定义过滤器,如:

HTML

<div ng-app> 
    <div ng-controller="MyCtrl"> 
    <input type="text" ng-model="searchid" /> 
     <ul> 
      <li ng-repeat="person in people | filter:myFilter"> 
       {{person.id}} 
       - 
       {{person.name}} 
      </li> 
     </ul> 
    </div> 
</div> 

JS

function MyCtrl($scope){ 

    $scope.people = [ 
     {id: 1, name: "Mark"}, 
     {id: 2, name: "John"}, 
     {id:3, name: "Joe"} 
    ]; 
    $scope.myFilter = function(item){ 
     if(item.id >= $scope.searchid){ 
      return item; 
     } 
    }; 
} 

这里的我拨弄例如:https://jsfiddle.net/javierif/mmoo3s8m/

+0

你有什么和:'return item.id> = $ scope.searchid;'? –

+0

有区别,因为我绑定输入与ng-model =”searchid “并存储在$ scope.searchid中 – Javierif

0

杉杉T,创建一个功能

$scope.lessThan = function(prop, val){ 
    return function(item){ 
     return item[prop] < val; 
    } 
} 

接下来,在你的ng-repeat

<tr ng-repeat="row in listFoodItems.fullitemdescs | filter: lessThan('EntryId', searchid)"> 

这里原来的答复: https://stackoverflow.com/a/24081564/5141584