5

I tried filtering a group of checkboxes with an array like so:

<ion-checkbox ng-repeat="user in users | filter: {id: group.members}" ng-model="user.checked">{{user.info.name}}</ion-checkbox>

where group.members is an array of user.id and it just doesn't show anything.

users Array:

[12345,123456]

group.members Array:

[12345]

I'm trying to accomplish not showing the group.members in the list of users, because in this case a user is trying to invite another user to the group and why invite someone who's already a member?

I tried creating my own filter, but its just a mess:

.filter('existingMembers', function() {
    return function(users, members) {
        return users.filter(function(user) {

            for (var i in user.id) {

                if (members.indexOf(user.id[i]) != -1) {
                    return;
                }
            }
            return user;

        });
    };
})
1
  • @NikhileshKV everything shows correctly. Commented Apr 25, 2016 at 12:47

2 Answers 2

2

After some messing around, this is the solution. See the plunkr for a working example. I think this should do the trick:

Template:

<ion-checkbox ng-repeat="user in users | filter: excludeMembers:group.members" ng-model="user.checked">{{user.info.name}}</ion-checkbox>

Angular filter:

app.filter('excludeMembers', function(){
  return function(users, members){
    return users.filter(function(user){
      return members.indexOf(user.id) === -1;
    });
  }
})

Long explanation

The filter takes the array you are filtering against as a first parameter as a default, then with the colon notation (:) you can supply optional arguments, in your case: the group. The filter should return a function, which will be run. The return value should be a filtered array. We also use a native javascript filter function (confusing, whoa) to check against the group array.

Sign up to request clarification or add additional context in comments.

6 Comments

@rcpilotp51 Updated.
@rcpilot51 Updated (bis)
@JustusRomijn Now it is working, but in the opposite fashion i would like it to. How would I modify the filter to not return the group members?
return members.indexOf(user.id) === -1; ??
@JustusRomijn === -1; did it. Thank you for your help!
|
0

By utilizing a look up table (LUT) object you may do some filtering like this with pure JS.

var gmems = [12345, 567890],
    users = [12345,123456,432567,1234987,567890],
      lut = gmems.reduce((p,c) => {p[c]=true; return p},{}),
      res = users.filter(e => !lut[e]);

document.write('<pre>' + JSON.stringify(res, 0, 2) + '</pre>');

2 Comments

thanks but I would like to use a filter. any other suggestions? thank you
Do you mean a filtering function? Then you may utilize the above code as function userFilter(u, g) { var lut = g.reduce((p,c) => {p[c]=true; return p},{}); return u.filter(e => !lut[e])};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.