根据子对象属性过滤 ng-repeat 列表 [英] Filtering an ng-repeat list based on a sub-object property

查看:21
本文介绍了根据子对象属性过滤 ng-repeat 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jsfiddle http://jsfiddle.net/KfSBq/

通过子对象,我的意思是我用 ng-repeat 显示的对象都包含一个内部对象列表,我希望根据这些子对象之一的属性进行过滤.

仅此一项就非常简单.我有一个 dailies 对象,每个对象包含一个 date 和一个 entries 对象列表:

function Ctrl($scope) {$scope.dailies = [{date: new Date('07/07/2013'),条目:[{类别:'A',注意:'Lorem ipsum'},{类别:'B',注意:'Lorem ipsum'}]},{日期:新日期('05/02/2013'),条目:[{category: 'A', note: 'Lorem ipsum'}]}];}

我显示它们,按类别过滤:

<div class="daily" ng-repeat="daily in dailies | orderBy:'-date'">{{ 每日日期 |日期:'dd/MM/y' }}<div class="entry" ng-repeat="entry in daily.entries | filter:{ category: 'B'} "><span>{{ entry.category }}</span>, <span>{{ entry.note }}</span>

我的问题是现在根本不包含任何条目的日常对象仍然显示.我如何实现这样一种情况,如果过滤使 dailyentries 列表为空,那么 daily 也不会显示?>

解决方案

您可以在表达式中创建新的作用域成员.

这使您可以将过滤列表分配给新变量,该变量可在整个本地范围内使用.这样,您就可以将过滤列表的长度传递给 ng-show:

<div ng-controller="Ctrl"><div类=每天"ng-repeat="每日日报 | orderBy:'-date' "ng-show="filteredEntries.length">{{ 每日日期 |日期:'dd/MM/y' }}<div 类=条目"ng-repeat="过滤条目中的条目 = (daily.entries | filter:{ category: 'B'})"><span>{{ entry.category }}</span>, <span>{{ entry.note }}</span>

FIDDLE

顺便说一句,很好地提出问题!

jsfiddle http://jsfiddle.net/KfSBq/

By sub-object I mean that the objects I am displaying with ng-repeat all contain a list of objects within themselves, and I am looking to filter based on the property of one of those sub-objects.

This alone was quite straightforward to do. I have an object of dailies, each containing a date and an entries list of objects:

function Ctrl($scope) {
    $scope.dailies = [{date: new Date('07/07/2013'), 
                       entries: [{category: 'A', note:'Lorem ipsum'}, 
                                 {category: 'B', note: 'Lorem ipsum'}]},
                      {date: new Date('05/02/2013'), 
                       entries: [{category: 'A', note: 'Lorem ipsum'}]}];
}

I display them, filtering by category:

<div ng-controller="Ctrl">
        <div class="daily" ng-repeat="daily in dailies | orderBy:'-date' ">
            {{ daily.date | date:'dd/MM/y' }}
            <div class="entry" ng-repeat="entry in daily.entries | filter:{ category: 'B'} ">
                <span>{{ entry.category }}</span>, <span>{{ entry.note }}</span>
            </div>
        </div>
    </div>

My issue here is that the daily objects that now contain no entries at all are still displayed. How do I achieve a situation where, if the filtering makes the entries list of a daily empty, that daily is not displayed either?

解决方案

You are allowed to create new scope members inside the expressions.

This lets you assign a filtered list to a new variable, which can be used throughout the local scope. With that, you can pass the length of the filtered list to ng-show:

<body ng-app>
  <div ng-controller="Ctrl">
    <div class="daily" 
      ng-repeat="daily in dailies | orderBy:'-date' " 
      ng-show="filteredEntries.length"
    >
      {{ daily.date | date:'dd/MM/y' }}
      <div class="entry" 
        ng-repeat="entry in filteredEntries = (daily.entries | filter:{ category: 'B'})"
      >
        <span>{{ entry.category }}</span>, <span>{{ entry.note }}</span>
      </div>
    </div>
  </div>
</body>

FIDDLE

Btw, nicely put question!

这篇关于根据子对象属性过滤 ng-repeat 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆