I have the following array of json objects in my angularjs application.
$scope.clients = [ {
"code": "FREG",
"name": "Feranget",
"role": "employee"
},
{
"code": "CRTE",
"name": "Crenart",
"role": "admin"
},
{
"code": "DERT",
"name": "Derta",
"role": "admin"
} ]
I have search form where I can enter code in the put text box, to search in this clients based on code. So I have applied a filter in ng repeat on the property code, which is working fine.
<tbody ng-if="clientSearch.clientCode">
<tr ng-repeat="client in clients | filter:{ code: clientSearch.clientCode }">
</tbody>
Now I need to support the search form with two more input fields, name and role having models as clientSearch.clientName and clientSearch.clieRole. clientSearch.clientName should be filtered on name property of the object and clientSearch.clieRoleshould be filtered on role property of the object with AND operation based on the input fileds, i.e., if code and role are entered in the respective input boxes then the clients should be filter out based on code and role but name filter should be ignored because it is empty. If name is also entered in the input box then the clients should be filtered based on code and role and name. Essentially empty fields should be ignored for filter and all non empty fields should be applied in filtering clients list with AND operation.
Here I have used the condition <tbody ng-if="clientSearch.clientCode"> so that by default clients list won't appear when input filed clientCode is empty and appear only when the entered text matches the search criteria. This feature needs to be maintained across all the three input fields clientSearch.clientName, clientSearch.clieRole and clientSearch.clientCode. When I replaced it with <tbody ng-if="clientSearch">, it is not working by default all the client list is appearing
In my controller I have input models are initialized as shown below:
$scope.clientSearch = { clientCode :'', clientName : '', clientRole : '' }
Can any one help me to solve this filtering problem in simple and genralized manner.