javascript - 在 AngularJS 中按数字过滤数组

标签 javascript angularjs

我想知道我是否可以在 AngularJS 中执行类似下面的代码的操作。目标是通过所有 id >= 搜索 id 来过滤数组。我可以内联搜索还是必须作为 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>

最佳答案

制作自定义过滤器的最佳方法如下:

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/

关于javascript - 在 AngularJS 中按数字过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36407972/

相关文章:

javascript - slideToggle() 动画期间出现跨文本换行问题

javascript - Angularjs limitTo 在使用(键,值)时不起作用

javascript - ckeditor - 如何将 css 类添加到对话框中的输入元素

javascript - 如何在 YUIDoc 中注释对象字面量

javascript - React-Native 应用程序因 JS 异常而崩溃,提供无用的堆栈信息(Android)

javascript - 如何将文本区域中的某些文本对齐为右对齐?

javascript - 如何在 angularjs 中将外部文件中的 HTML 表转换为 JSON

node.js - NodeJS/ExpressJS 在 1 个流中发送大量数据的响应

javascript - 如何使用 AngularJS 将数据传递到不同的页面?

javascript - 如何在 AngularJS 中查找二维数组中的访问元素