0

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

2
  • Show us your controller, please. Commented Jul 13, 2015 at 21:23
  • Please refer to this new post with some modifications. My controller code is in there @isherwood thanks! stackoverflow.com/questions/31394920/… Commented Jul 13, 2015 at 23:02

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

This does not work for me, when I search something nothing changes @Sofre Garevski
This solution doesn't work 100% for me. I don't know why. Sometimes the search works, it pulls results as expected. Sometimes it does not. For example, when my post.title is B, the search can find it. When it is A or C or D, it doesn't find them. Could it be the post.date interfering with it somehow? Btw I am using MongoDB to store the results
0

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)

2 Comments

I did follow the example you provided, and it isn't working for me. Please take a look at my new post with updates @Teliren stackoverflow.com/questions/31394920/…
Update: this approach works fine for a single field. I am really curious to why this is happening. @Teliren

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.