AngularJS:獲取複雜過濾數組的大小 (AngularJS: Get size of complex filtered array)


問題描述

AngularJS:獲取複雜過濾數組的大小 (AngularJS: Get size of complex filtered array)

我在 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 }} 不起作用。我嘗試使用這篇文章中詳述的一些解決方案:如何顯示過濾後的 ng‑repeat 數據的長度,但它們都處理一組更簡單的過濾器,


參考解法

方法 1:

You only have to assign a variable towards the filtered dataObject, and make sure to enclose the assignment expression with a parenthesis after the in key word and before the track key word.

<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 }}

Update

In case you're having problems with the scope of your variables, create an object $scope variable that stores the filtered data.

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 }}

(by TWLATLryeballar)

參考文件

  1. AngularJS: Get size of complex filtered array (CC BY‑SA 2.5/3.0/4.0)

#ng-repeat #angularjs #angularjs-filter






相關問題

ng-repeat 加載後不顯示表行 (ng-repeat not showing table rows after loading)

僅在 ng-repeat 中將 ng-if 應用於所選項目 (Apply ng-if in ng-repeat only to selected item)

AngularJS:獲取複雜過濾數組的大小 (AngularJS: Get size of complex filtered array)

單擊時更改 ng-class(嵌套 ng-repeat) (Changing ng-class when clicked (nested ng-repeat))

帶有選擇/選項下拉框的嵌套 ng-repeat 問題 (Nested ng-repeat issue with select/options drop down box)

錯誤:[ngRepeat:dupes] 不允許在轉發器中重複。使用“track by”表達式指定唯一鍵 (Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys)

在 AngularJS 中保存在 ng-repeat 中生成的字段 (Saving a field generated in ng-repeat in AngularJS)

遍歷對像數組以構建 div 結構 (Iterate over array of object to build a div structure)

ng-repeat 中 IE 下拉列表中的 Angularjs 無法正確呈現 (Angularjs in IE dropdowns in ng-repeat do not render correctly)

動態 ng-repeat 表單的優雅解決方案 (Elegant solution for a dynamic ng-repeat form)

如何區分 Rs 和 % 值? (How to differentiate Rs and % values?)

難以為一組對象運行 ng-repeat (difficulty running ng-repeat for an array of objects)







留言討論