OneMoreQuestion
OneMoreQuestion

Reputation: 1753

searchKeyword not working in AngularJS filter

I am writing an app using AngularJS on the front end. I want to search through a table by any word/field; one search box for everything. According to this article here: http://hello-angularjs.appspot.com/searchtable I can use filter: searchKeyword to do just this.

This is my code, and it is not working as expected.

<label>Search: <input ng-model="searchKeyword"></label>

<table ng-repeat="post in posts | orderBy: sort | filter: searchKeyword">
    <tr>
       <td> {{index(post)}} </td>

       <td> {{post.title}} </td>

       <td> {{post.author}} </td>

       <td> {{post.content}} </td>

       <td> {{post.date | date: "d/M/yyyy"}} </td>
    </tr>
</table>

What should I do? Thank you

Upvotes: 0

Views: 1239

Answers (2)

Teliren
Teliren

Reputation: 342

I ran into a similar issue myself.

I did something like this

<input class="form-control" type="text" ng-model="searchFilter.itemDesc" 
    placeholder="Search...." />

<tr ng-repeat="item in items | filter: searchFilter">

each item has a property of itemDesc

the downside to this approach is that it only lets you search one property of the object being repeated

If you are wanting it to be searched by the different properties of the object

you can do something similar to

Filtering by Multiple Specific Model Properties in AngularJS (in OR relationship)

Upvotes: 0

Sofre Garevski
Sofre Garevski

Reputation: 64

Try this:

Controller

    $scope.posts = [
      {title:'Title one', author:'Author one', content: 'Content one'},
      {title:'Title two', author:'Author two', content: 'Content two'},
      {title:'Title three', author:'Author three', content: 'Content three'}
    ];

    $scope.searchKeyword = '';
    $scope.sort = 'title';

View

<label>Search:
  <input ng-model="searchKeyword">
</label>

<table ng-repeat="post in posts | orderBy: sort | filter: searchKeyword">
  <tr>
    <td> {{post.title}} </td>
    <td> {{post.author}} </td>
    <td> {{post.content}} </td>
  </tr>
</table>

Upvotes: 1

Related Questions