jamesbcn
jamesbcn

Reputation: 347

angularjs ng-repeat ng-options custom filter

.filter('statusFilter', function() {

        return function(listings) {
        var filtered = [];
        var arr = [];

        angular.forEach(listings, function(listing) {
            if(listing.status != 0){
            filtered.push(listing);
            }
        });
        return filtered;
        };
    })

The filter above is working fine in order to remove entries with a zero status from selection in a dropdown list on each row of an ng-repeat table.

However, my boss has said that he needs entries with zero status to remain if they are the current default selection. With the above filter the default selection is blank for entries with zero status.

This is the ng-repeat:

ng-repeat="task in tasksCtrl.tasks"

And below is the ng-repeat table cell containing the ng-options:

<td level="{{tasksCtrl.level}}" check-permissions="onlyAdmin">
                            <select name="selValidator-{{ task.id }}"
                                    class="full-width select-width-tasks"
                                    ng-options="item.id_user as item.name for item in tasksCtrl.validators | statusFilter" 
                                    ng-model="task.validator_id" 
                            </select>
                        </td>

I need to also add to the filter listings that have a listing.status of zero but have a listing.id_user which is equal to task.validator_id

I believe I therefore need to add task.validator_id as a parameter to pass through the function. I am struggling though!

Any help would be greatly appreciated.

Upvotes: 0

Views: 53

Answers (1)

jamesbcn
jamesbcn

Reputation: 347

SOLUTION

.filter('statusFilter', function() {

        return function(listings, id) {
        var filtered = [];
        angular.forEach(listings, function(listing) {
            //console.log([id, listing.id_user, listing.name, id == listing.id_user]);
            if(listing.status != 0 || id == listing.id_user){
            filtered.push(listing);
            }
        });
        return filtered;
        };
    })

ng-options:

ng-options="item.id_user as item.name for item in tasksCtrl.validators | statusFilter:task.validator_id"

Upvotes: 0

Related Questions