2016-04-04 84 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