4

I'm using a search box(input) for filtering the table result. Currently it filters regarding StudentName column, but it needs to be dynamic.

Lets say:

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

Program in short:

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

<table>
    <tr ng-repeat="x in names |filter :{'StudentName' :test}"></tr>
</table>

1 Answer 1

1

Follow below method - To Search different things you need to use different input fields otherwise you got to create your custom filter with your type of 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 custom 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">
Sign up to request clarification or add additional context in comments.

5 Comments

All in one Input is my problem .I have dropdown to choose from
what is there all things in your dropdown. How do you find what are you searching for. i.e. name, rollno ?
User can able to select one of the list("name","RollNo") in dropdown then I will collect the user clicked value from dropdown and then I need to put the value in angularjs filter, exactly in StudentName place in code (<tr ng-repeat="x in names |filter :{'StudentName' :test}"></tr>)
confusing. Are you saying you have one dropdown for type of fields you want to search like name, roll and other input field for real value for them?
in that case see EDIT

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.