問題描述
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 }}