AngularJS ng-options to exclude specific object
Asked Answered
G

4

9

I'm using angular ng-options to show a with several options as the choices of parent category of the current category, basically these options contain all the existing categories, and I need to exclude the current category from ng-options, which quite make sense because a category cannot be the parent category of itself. So how do I do that? Currently I have the following code:

<tr ng-repeat="category in allCategories">
    <th ng-bind="category.name"></th>
    <th>
        <select ng-options="everyCategory.name for everyCategory in allCategories">
            <option value="">Select parent category</option>
        </select>
    </th>
</tr>
Gherardo answered 6/10, 2014 at 11:28 Comment(0)
E
31

You should use filter.

<select ng-options="everyCategory.name for everyCategory in allCategories | filter: { name: '!' + category.name }">...</select>
Endodontist answered 6/10, 2014 at 11:48 Comment(3)
Works like a charm. Thanks, I'm accepting both as the correct answerGherardo
I see this has a problem, let's say current category name is 'horse', there is another category named 'white horse', using this filter filters out both these two categories, but what I want is to filter just the current category which is 'horse'.Gherardo
Look at this: https://mcmap.net/q/330766/-exact-filter-in-angular/…Endodontist
Z
2

You could use a filter

<tr ng-repeat="category in allCategories">
  <th>{{category.name}}</th>
  <th>
    <select ng-options="everyCategory.name for everyCategory in allCategories | filter: {name: '!' + category.name}" ng-model="somthing">
      <option value="">Select parent category</option>
    </select>
  </th>
</tr>

I've created a small fiddle with a exmaple of how to use it: http://jsfiddle.net/krausekjaer/tnqrqk2w/3/

Zach answered 6/10, 2014 at 11:59 Comment(1)
Works like a charm! Thanks, I'm accepting both as the correct answer.Gherardo
G
0

Thanks to @Krause and @Kamil R for the answer, yet I found an issue when using their solution, as I've mentioned in previous comments, if there are 2 categories with one name as a substring of another, the filter will cross out both of them. For instance, categories like: candy candybar Using

filter: { name: '!' + category.name }

will filter both of them out, in order to make sure only one gets filtered, I ended up writing a custom filter:

app.filter('parentTaxonomyFilter', function(){
    return function(items, name){
        var arrayToReturn = [];        
        for (var i = 0; i < items.length; i ++){
            if (items[i].name != name) {
                arrayToReturn.push(items[i]);
            }
        }
        return arrayToReturn;
    };
});

and in html, I use the filter like this:

<select class="form-control" ng-init="taxonomy.parentTaxonomy=getParentTaxonomy(taxonomy)" ng-model="taxonomy.parentTaxonomy" ng-options="everyTaxonomy.name for everyTaxonomy in data.allTaxonomies|parentTaxonomyFilter:taxonomy.name">
    <option value="">select parent taxonomy</option>
</select>
Gherardo answered 17/10, 2014 at 2:33 Comment(0)
B
0

Try this

<tr ng-repeat="category in allCategories">
  <th>{{category.name}}</th>
  <th>
    <select ng-options="everyCategory.name for everyCategory in allCategories | filter: { name: '!' + category.name }" ng-model="somthing">...</select>
      <option value="">Select parent category</option>
    </select>
  </th>
</tr>
Bookmark answered 23/9, 2015 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.