Variable Name in Angularjs Filter

I use the search box (input) to filter the result of the table. It currently filters StudentName columns, but it should be dynamic.

Let's say:

filter :{'RollNo' :test}
filter :{'Dept' :test} 

Shortly speaking:

<input type="text" ng-model="test">

<table>
    <tr ng-repeat="x in names |filter :{'StudentName' :test}"></tr>
</table>
+4
source share
1 answer

Follow the method below - To search for different things, you need to use different input fields, otherwise you must create your own custom filter with your requirements.

<label>Any: <input ng-model="search.$"></label> <br>
<label>Name only <input ng-model="search.name"></label><br>
<label>RollNo only <input ng-model="search.RollNo"></label><br>

<table>
  <tr ng-repeat="x in names | filter:search">
    <td>{{x.name}}</td>
    <td>{{x.RollNo}}</td>
  </tr>
</table>

EDIT

Here is a special filter for your case:

.filter('TableFilter', function(){
    return function(dataArray, type, filtervalue) {
        if (!dataArray) {
            return;
        }else{
            if(type === 'Name'){
                return dataArray.filter(function(item){
                    var term = item.name === filtervalue;
                    return term;
                });
            }else if(status === 'RollNo'){
                return dataArray.filter(function(item){
                    var term = item.RollNo === filtervalue;
                    return term;
                });
            }
        }
    }
});

<tr ng-repeat="x in names | TableFilter : dropDownvalue : test">
+1
source

Source: https://habr.com/ru/post/1677254/


All Articles