【问题标题】:How to apply ng-if with filter for filtering records and present two different views in the same page?如何使用带有过滤器的 ng-if 过滤记录并在同一页面中呈现两个不同的视图?
【发布时间】:2015-06-08 10:45:56
【问题描述】:

我对 AngularJS 很陌生,正在从 here 学习,但我坚持以下我的实验。

我有以下数据:

var people = ([
    {
    id: 1,
    name: "Peter",
    age: 21},
{
    id: 2,
    name: "David",
    age: 10},
{
    id: 3,
    name: "Anil",
    age: 22}
    ]);

我要做的是,如果年龄大于 20,那么我必须显示所有三个字段(即 id、name、age)

ID    Name  Age
1     Peter  21
3     Anil   22

如果年龄小于20,,那么只会出现2个字段(例如id,name)

ID    Name  
2     David  

到目前为止我尝试了什么?

app.js

var myApp=angular.module('myApp', []);

myApp.controller('PeopleCtrl', function($scope,$window) {

    $scope.people = ([
    {
    id: 1,
    name: "Peter",
    age: 21},
{
    id: 2,
    name: "David",
    age: 20},
{
    id: 3,
    name: "Anil",
    age: 22}
    ])  
});

index.html

<!DOCTYPE html>
<html>
  <head>
    <script data-require="angular.js@1.4.0-beta.6" data-semver="1.4.0-beta.6" src="https://code.angularjs.org/1.4.0-beta.6/angular.js"></script>   
    <script src="app.js"></script>
  </head>
  <body>
        <div ng-app="myApp" ng-controller="PeopleCtrl">

                <table  border="1" >
                    <tr>
                        <th>Id</th>
                        <th>Name</th>
                        <th>Age</th>        
                    </tr>
                    <tr ng-repeat="person in people" >
                        <td><span>{{person.id}}</span></td>
                        <td><span>{{person.name}}</span></td>
                        <td><span>{{person.age}}</span></td>       
                    </tr>
                </table>

        </div>
  </body>
</html>

我在网上浏览了一些教程,并想到了 ng-if 的概念,这将有助于与 filter 一起完成此任务。 (我想是的..如果我的方向错误,请纠正我)

但我不能只将它放入应用程序中。

另外,我想在同一页面中显示记录(即 index.html)

为此寻求帮助。

Plunker: http://plnkr.co/edit/hLaw38fCMaclrHiaCH3X?p=preview

【问题讨论】:

    标签: javascript angularjs html-table angularjs-ng-repeat


    【解决方案1】:

    如下所示。

    标记

    <tr ng-repeat="person in people| filter: search">
        <td><span>{{person.id}}</span></td>
        <td><span>{{person.name}}</span></td>
        <td ng-if="person.age<20"><span>{{person.age}}</span></td>
    </tr>
    

    Working Plunkr

    编辑 1

    您将需要创建额外的过滤器,这将为您提供过滤器内容是否低于 20 人。

    标记

    <table border="1">
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th ng-if="(people| filter: search| filter: underTwenty: 20).length > 1">Age</th>
        </tr>
        <tr ng-repeat="person in people| filter: search">
            <td><span>{{person.id}}</span></td>
            <td><span>{{person.name}}</span></td>
            <td ng-if="person.age<20"><span>{{person.age}}</span></td>
        </tr>
    </table>
    

    过滤器

    myApp.filter('underTwenty', function() {
        return function(values, limit) {
            var returnValue = [];
            angular.forEach(values, function(val, ind) {
                if (val.age < limit) returnValue.push(val);
            });
            return returnValue
        }
    });
    

    Updated Plunkr

    编辑 2

    根据 OP 要求的更改

    HTML

    <table border="1" ng-init="ageToShow=(people| underTwenty: 20).length >= 1">
      <tr>{{ageToShow}}
        <th>Id</th>
        <th>Name</th>
        <th ng-if="!ageToShow">Age</th>
      </tr>
      <tr ng-repeat="person in people| filter: search">
        <td><span>{{person.id}}</span>
        </td>
        <td><span>{{person.name}}</span>
        </td>
        <td ng-if="!ageToShow"><span>{{person.age}}</span>
        </td>
      </tr>
    </table>
    

    Plunkr

    【讨论】:

    • 当年龄小于 20 岁时,它应该只显示 2 个字段,即 ID 和姓名,但年龄不应该显示 ....
    • 你的意思是应该隐藏列?
    • @anilchean 看看我的 plunkr
    • 但我要问的是,如果任何 1 人的年龄小于 20 岁,那么整个年龄列不应该出现....请帮我解决这个问题
    • 真棒答案(过滤器:搜索)喜欢它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 2021-04-02
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多