Search code examples
htmlangularjsangularjs-filter

angularJS filter using array type(?) ng-model


I wanna make filter working at only one element. To make filter, I searched SO, and find way to make it I want. But I used 'select' tag to choose element applies to filter, I got problem when selected value is NULL.

<div class="input-group">
    <select class="form-control" ng-model="setParam">
        <option value="">All</option>
        <option value="atName">Name</option>
        <option value="atComp">Company</option>
        <option value="atDep">Department</option>
        <option value="atPos">Position</option>
        <option value="atTel">Tel</option>
        <option value="atEmail">Email</option>
    </select>
</div>
<div class="input-group">
    <span>
        <input type="text" class="form-control" ng-model="atParam[setParam]">                               
    </span>

<tr data-ng-repeat="row in attendees | filter : atParam">

The problem is getting nothing to display when selected value is NULL. Everything except NULL works well. What should do if I solve it? Or, are there another ways to search? Thanks!


Solution

  • I think your requirement is when you select ALL all the records should be displayed. But you are getting none.

    For that you need to add a condition to the filter depending on the setParam

    <tr data-ng-repeat="row in attendees | filter : setParam ? atParam : ''">
    

    What this does is,

    • It will search for setParam first.
    • If it is not null then the atParam condition of yours is used.
    • If it is null then, the empty string is filtered thereby displaying all records