AngularJS:获取复杂过滤数组的大小

时间:2015-11-20 16:37:26

标签: angularjs ng-repeat angularjs-filter

我在ng-repeat上有一组复杂的过滤器。我正在尝试获取数据集的计数,因为它是通过每个选择列表过滤的。

这是我的ng-repeat:

		<div class="trials-item {{class}}" ng-repeat="data in dataObject | byCountry : selectRegion | byRegion : selectState | byCity : selectCity | filterBy:['Phase']: selectPhase | filterBy:['Compound']: selectCompound | filterBy:['TherapeuticArea', 'TherapeuticArea_2',]: selectTherapy : 'strict' | unique: 'Id' | orderBy:'Phase' : reverse track by $index " ng-class="{'open':$index == selectedRow}" id="anchor-{{$index}}" >

尝试使用{{data.length}}不起作用。我尝试使用本文中详述的一些解决方案:How to display length of filtered ng-repeat data,但它们都处理了一组更简单的过滤器,如果它们可以使用上面的过滤器设置,我一定不能得到语法/顺序正确。

1 个答案:

答案 0 :(得分:3)

您只需要为过滤的dataObject分配一个变量,并确保在in关键字之后和track关键字之前用括号括起赋值表达式。

<div class="trials-item {{class}}" ng-repeat="data in (filteredData = (dataObject | byCountry : selectRegion | byRegion : selectState | byCity : selectCity | filterBy:['Phase']: selectPhase | filterBy:['Compound']: selectCompound | filterBy:['TherapeuticArea', 'TherapeuticArea_2',]: selectTherapy : 'strict' | unique: 'Id' | orderBy:'Phase' : reverse)) track by $index " ng-class="{'open':$index == selectedRow}" id="anchor-{{$index}}" ></div>
Result: {{ filteredData.length }}

<强>更新

如果您遇到变量范围问题,请创建一个存储过滤数据的对象$scope变量。

<强>的Javascript

$scope.filtered = {};

<强> HTML

<div class="trials-item {{class}}" ng-repeat="data in (filtered.data = (dataObject | byCountry : selectRegion | byRegion : selectState | byCity : selectCity | filterBy:['Phase']: selectPhase | filterBy:['Compound']: selectCompound | filterBy:['TherapeuticArea', 'TherapeuticArea_2',]: selectTherapy : 'strict' | unique: 'Id' | orderBy:'Phase' : reverse)) track by $index " ng-class="{'open':$index == selectedRow}" id="anchor-{{$index}}" ></div>
Result: {{ filtered.data.length }}