简体   繁体   中英

angularJs - how to get filtered array

I have a mover directive with the following html for the list

 <select class="select-list" multiple ng-model="unassigned" name="unAssignedList" data-no-dirty-check ng-options="unassignedItem.descrip for unassignedItem in unassignedItems | orderBy:'descrip' | filter: filterCriteria"></select> 

So, when I use this directive I can specify my filter-criteria like this

 <data-sm:duallist-directive ng-required="false" keep-pristine="true" unassigned-items-title="'@String.Format(Labels.availableX, Labels.items)'" unassigned-items="currentItemGroup.unassignedItems" assigned-items-title="'@String.Format(Labels.assignedX, Labels.items)'" assigned-items="currentItemGroup.assignedItems" sortable="false" filter-criteria="{categoryId:selectedCategoryId}" selected-item="currentItemGroup.selectedItem"> </data-sm:duallist-directive> 

The problem is with the MoveAllLeft (or MoveAllRight) buttons. They have the following code:

 $scope.moveRightAll = function() { var unassignedItems = $scope.unassignedItems.slice(0); var smItems = $scope.unassignedItems.slice(0); angular.forEach(smItems, function (value, key) { $scope.assignedItems.push(value); removeItem(unassignedItems, value); }); $scope.unassignedItems = unassignedItems; if (!$scope.keepPristine) $scope.form.$setDirty(); $scope.assigned = null; }; 

The problem is that it works against original unfiltered array. Say, if I have 642 items in total and I filtered them by category to only, say, 5, I only want to move these 5 items when I press my button, not all 642 which I don't even see on the screen.

How can I modify my code to get only items which are filtered? Also, I don't have to enter filter-criteria, so it should work correctly when nothing is entered in the filter-criteria.

I solved the problem - it turned out to be very easy. I added the following code at the top

 var filteredData ; if ($scope.filterCriteria) filteredData = $filter('filter')($scope.unassignedItems, $scope.filterCriteria); else filteredData = $scope.unassignedItems; var unassignedItems = filteredData.slice(0); var smItems = filteredData.slice(0); 

and now only filtered items are moved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM