beginner1417
beginner1417

Reputation: 67

using ng-repeat angular with filter to exclude blank value

I'm trying to get non-blank elements in my select using filter, but this doesn't work properly.

<select ng-model="selectedSubject.id">
    <option value="0">
        <%= res.getString("portlet.form.subjectField.default")  %>
    </option>  
    <option ng-repeat="sujet in subjects.allSubjects | filter:{name_fr:'!!'}" value="{{sujet.id}}">
        {{sujet.name_fr}}
    </option>
</select>

Upvotes: 4

Views: 2350

Answers (3)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

filter:{my_field:'!!'} will filter out only null values.

If you want to filter out empty values, or both null and empty, you should use a custom filter

<option ng-repeat="sujet in subjects.allSubjects | filter:notEmptyOrNull" value="{{sujet.id}}">{{sujet.name_fr}}</option>

Controller

$scope.notEmptyOrNull = function(item){
  return !(item.name_fr === null || item.name_fr.trim().length === 0)
}

Upvotes: 5

Moni Bhattacharya
Moni Bhattacharya

Reputation: 133

The filter that you are using will only filter the data that contains null values not blank.

To filter out blank data you have to create filter.This question has already been answered on stack overflow. Visit this link.

Angular - Filter to remove blank strings from array

Upvotes: 1

Raulucco
Raulucco

Reputation: 3426

You have to set the model so that angular selects the right option base on the select ngModel

<select ng-model="selectedSubject.id" ng-options="sujet.id as sujet.name_fr in subjects.allSubjects track by sujet.id"></select>

And set the default id on the controller.

$scope.allSubjects = subjects;
$scope.selectedSubject.id = subjects[0].id;

Upvotes: 0

Related Questions