1

So I need to implement a search filter, just by name. I am not good with angularjs so I could use some help. This is what I did for now, but it needs a bit of tweaking:

<input type="text" ng-model="searchText" placeholder="Search for a pupil..." />

<tr ng-repeat="student in studentData | filter : searchText">

just this filter. But in studentData, i should search through studentData.FullName only. What is the easiest way to do this?

3 Answers 3

1

I believe you can specify the property you want to filter by like this:

<tr ng-repeat="student in studentData | filter : {fullName: searchText}">

However, keep in mind that this will be an exact text match, including casing "Joe" is not equal to "joe".

The above will work, but IMO it's not the best solution. Below is my recommendation

Change the HTML to point at a new array of data:

<input type="text" ng-model="searchText" placeholder="Search for a pupil..." />
<tr ng-repeat="student in filteredStudentData">

And in your controller:

$scope.searchText = ''; //start as an empty string
$scope.studentData = [ ... ]; //your real data goes in here
$scope.filteredStudentData = []; // start as an empty array

//Watch the `searchText` property for changes
$scope.$watch('searchText ', function(newValue, oldValue){
  //remove whitespace, and make it all lowercase - note the `.trim()` only works on IE 9+
  var normalizedSearchText = (newValue || '').toLowerCase().trim();

  //Now only include the relevant results in the `filteredStudentData` array
  //note that the `.filter()` method is only available on IE9+
  $scope.filteredStudentData = $scope.studentData.filter(function (student) {
    return 
       normalizedSearchText === student.fullName.toLowerCase().trim() ||
       normalizedSearchText === student.someOtherProperty.toLowerCase().trim() ||
       normalizedSearchText === student.anotherExampleProperty.toLowerCase().trim();
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the edit, thats what I tried to do from the start, trim/lowercase/new array, but this works perfectly, the simpler one, no problems with caps. But cool response, i will keep it in mind for the future
1

Check the below code..

<input type="text" ng-model="searchText" placeholder="Search for a pupil..." />

<tr ng-repeat="student in studentData | filter :{name:searchText}:true">

Comments

1

Try this

<input type="text" ng-model="searchText" placeholder="Search for a pupil..." />

<tr ng-repeat="student in studentData | filter:{student.Fullname:searchText}">

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.