3

I am trying to filter out items which are objects & not null in angular 1.5.6.

I tried all options from this post.

Still cannot make it to work.

Only 'Sally' should be shown in the select

Here is the fiddle

HTML :

<div ng-app="myApp" ng-controller="myCtrl">
   <br>

     <select ng-model="a" ng-options="detail.name for detail in details | filter:{shortDescription:'!null'} track by detail.name">
  </select>

    <br>
    New 

     <select ng-model="b" ng-options="detail.name for detail in details | filter:{shortDescription: ''} track by detail.name">
  </select>

    <br>
    All
    <select  ng-model="c" ng-options="detail.name for detail in details | filter:{shortDescription: '!!'} track by detail.name">
  </select>

  <br>
  Without any filter
  <select ng-model="d" ng-options="detail.name for detail in details track by detail.name">
  </select>
  <!-- -->
</div>

Script :

function myCtrl($scope) {
    $scope.details = [{
        name: 'Bill',
        shortDescription: null
    }, {
        name: 'Sally',
        shortDescription: {'MyName':'A girl'}
    }];
}

angular.module("myApp",[]).controller("myCtrl", myCtrl);

5 Answers 5

2

The problem is that filter:{shortDescription: ''} matches only primitives but not complex objects and shortDescription in your case is a nested object.
Therefore instead use filter:{shortDescription:{$:''}} like so:

  <select ng-model="a" 
          ng-options="detail.name for detail in details | filter:{shortDescription:{$:''}} track by detail.name">
  </select>
Sign up to request clarification or add additional context in comments.

Comments

2

Please run this snippet to achieve your goal

function myCtrl($scope) {
    $scope.details = [{
        name: 'Bill',
        shortDescription: null
    }, {
        name: 'Sally',
        shortDescription: {'MyName':'A girl'}
    }];

}

angular.module("myApp",[]).controller("myCtrl", myCtrl).filter('descFilter',descFilter);

function descFilter(){
	return function(array,key){
  	var newArr = [];
    for(var i = 0; i < array.length; i++){
    	if (array[i][key] != null){
       newArr.push(array[i]);
      }
    }
    return newArr;
  };
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
   <br>
     With Filter:
     <select ng-model="a" ng-options="detail.name for detail in details | descFilter:'shortDescription'">
  </select>
    
  
  <br>
  Without any filter
  <select ng-model="d" ng-options="detail.name for detail in details track by detail.name">
  </select>
  <!-- -->
</div>

1 Comment

Thank You. Your answer indeed works. but that would mean creating a new array inside & another filter function. I was looking for something basic like what DAXholic provided.
1

In ngOptions the not null check is know to not work. Instead you can create a custom filter and use that.

Here is an example with a custom filter function. http://jsfiddle.net/ivankovachev/bue59Loj/

The custom filter function:

$scope.isDescriptionNull = function(item) {
            if (item.shortDescription === null) return false;

          return true;
        }

And how to use it:

<select ng-model="a" ng-options="detail.name for detail in (details | filter:isDescriptionNull) track by detail.name">

Comments

0

You have a little bug in your markup.

The filter expects to get a object property, expression, or comparator.

Official documentation:

{{ filter_expression | filter : expression : comparator : anyPropertyKey}}

So, what you are doing isn't a valid filter

Comments

0
<select ng-model="a" 
          ng-options="detail.name for detail in details  | filter:{shortDescription:'!!'}} track by detail.name">
  </select>

Comments

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.