0

I'm wondering if I can do something like the code below in AngularJS. The goal is to filter the array by all ids >= the search id. Can I do the search inline or does it have to be done as a custom filter in javascript?

<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>
1
  • You could have div(ng-if="row. EntryId > listFoodItems.searchid") to wrap around the tds. Commented Apr 4, 2016 at 16:36

2 Answers 2

2

Best way make a custom filter like:

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;
        }
    };
}

here its my fiddle with example: https://jsfiddle.net/javierif/mmoo3s8m/

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a difference between what you have and: return item.id >= $scope.searchid; ?
because i binding input with ng-model="searchid" and it stored in $scope.searchid
0

First, create a function

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

Next, in your ng-repeat

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

Original answer here: https://stackoverflow.com/a/24081564/5141584

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.