【问题标题】:AngularJS: Get size of complex filtered arrayAngularJS:获取复杂过滤数组的大小
【发布时间】:2015-11-20 16:37:25
【问题描述】:

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

这是我的 ng 重复:

		<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,但它们都处理一组更简单的过滤器,如果它们可以与我上面设置的过滤器一起使用,我一定不会得到语法/顺序正确。

【问题讨论】:

    标签: angularjs ng-repeat angularjs-filter


    【解决方案1】:

    您只需为过滤后的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 }}
    

    【讨论】:

    • 这不会轰炸查询,但它也不会给我在过滤数据.length 输出上返回的值。
    • 如果我可以问,filteredData 的结果是 object 还是 array
    • 如果filteredData 未定义,那么ng-repeat 结果肯定也应该为空。如果是这种情况,那么问题不在于分配,而在于过滤器。
    • filteredData 的范围是什么 - 即,我将在 console.log 中使用什么来获取值/类型?我得到了一个数据集,所以它一定有一些价值。我怀疑我有错误的 $scope 进行日志记录。正在使用 console.log($scope.filteredData);
    猜你喜欢
    • 2016-10-22
    • 2017-11-14
    • 1970-01-01
    • 2019-08-16
    • 2018-02-01
    • 1970-01-01
    • 2016-10-03
    • 2011-06-27
    • 1970-01-01
    相关资源
    最近更新 更多