Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Filter ng-options not having specific values

I want to filter on a select like so :

<select ng-model="test" ng-options="c as c.label group by c.type for c in columns | filter:{c.type:'!field'} | filter:{c.type:'!map'}"></select> 

EDIT : Adding the column model :

Columns = [ {     name: "name",     label: "Label",     info: "Information displayed in help",     type: "type",     view: "html template",     style: "min-width: 10em;",     show: true }, {  ... } ]; 

Columns is used for several things and to optimize my code I need it to be also in a Select, but without the entries whose type are 'field' nor 'map'

Yet, I get to choose from everything, even the entries which types are 'field' and 'map'. Is there a clean way to do it ?

like image 429
Romain Avatar asked Nov 25 '13 10:11

Romain


People also ask

How do I set default selected value in ng-options?

In my opinion the correct way to set a default value is to simply pre-fill your ng-model property with the value selected from your ng-options , angular does the rest. Essentially when you define the $scope property your select will bind to assign it the default value from your data array.

What is the correct way to apply filter in AngularJS?

In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.

How do you check if an array contains a value in AngularJS?

The indexOf() method searches the array for the specified item, and returns its position. And return -1 if the item is not found. If you want to search from end to start, use the lastIndexOf() method: var Color = ["blue", "black", "brown", "gold"]; var a = Color.

What is custom filter in AngularJS?

Introduction to AngularJS Custom Filter. In AngularJS filters are used to modify or update the data before rendering the data on view or UI. Filters are clubbed in expression or directives using pipe (|) symbol.


1 Answers

AngularJS NOT Filter

<select ng-model="test" ng-options="c as c.label group by c.type for c in columns     | filter:{ type : '!field' }     | filter:{ type : '!map' }"> </select> 

Fiddle

From the docs:

"...The predicate can be negated by prefixing the string with !."

"A pattern object can be used to filter specific properties on objects contained by array. For example {name:"M", phone:"1"} predicate will return an array of items which have property name containing "M" and property phone containing "1"..."

like image 162
AlwaysALearner Avatar answered Sep 18 '22 04:09

AlwaysALearner