2016-04-04 11 views
0

AngularJSで以下のコードを実行できるかどうかは疑問です。目標はすべてのids> =検索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 = "行可能性があります。 –

答えて

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

ファーズトン、関数を作成

$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

関連する問題