2

I have a form where I want the user to be able to enter in search terms into textboxes, and then click a button to search the list. Is it possible to do something like this with ng-repeat and a button click? I'm really new to Angular, and I'm unsure if I can use it for this type of functionality. I have a little bit of code:

HTML

<div>    
<input ng-model="filterSearch.address" />
<input ng-model="filterSearch.city" />
<button ng-click="">Search</button>    
<table>
    <thead>
        <tr>
            <th>
                Address 
            </th>
            <th>
                City
            </th>
        </tr>            
    </thead>
    <tbody>            
        <tr ng-repeat="search in vm.searches | filter: filterSearch">
            <td>
                {{ search.address }}
            </td>
            <td>
                {{ search.city }}
            </td>
        </tr>
    </tbody>
</table>

Controller:

(function () {   
'use strict'
angular
    .module('crm.ma')
    .controller('AdvancedSearchCtrl', AdvancedSearchCtrl);

function AdvancedSearchCtrl() {
    var vm = this;
    vm.searches = [
               {
                   "address": "202 This St",
                   "city": "Columbus"
               },
               {
                   "address": "205 That St",
                   "city": "Dayton"
               }

    ];


}
})();

If anyone could point me in the right direction that would be great. Thanks.

Updated code:

<div>    
<input ng-model="searchModel.address" />
<input ng-model="searchModel.city" />
<button ng-click="filterSearch = searchModel">Search</button>    
<table>
    <thead>
        <tr>
            <th>
                Address 
            </th>
            <th>
                City
            </th>
        </tr>            
    </thead>
    <tbody>            
        <tr ng-repeat="search in vm.searches | filter:{'address': filterSearch.address, 'city': filterSearch.city}">
            <td>
                {{ search.address }}
            </td>
            <td>
                {{ search.city }}
            </td>
        </tr>
    </tbody>
</table>

1 Answer 1

2

You can specify fields to search on like:

    <tr ng-repeat="search in vm.searches | filter:{'address': filterSearch.address, 'city': filterSearch.city">
        <td>
            {{ search.address }}
        </td>
        <td>
            {{ search.city }}
        </td>
    </tr>

And if you need the on-click to trigger the search, you can just use something like:

ng-click="filterSearch = searchModel"

And change your inputs to use the searchModel variable.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok. I updated my code in my question to reflect the changes you have suggested. I'm doing something wrong though, because it's still searching the list when I type it into the textboxes instead of only searching and displaying when I click the button. Thanks for your help.
Can you show me some code? I'm not sure how to do this?
If it's only happening after the first click, it's because the filterSearch object is set by reference to the searchModel object, rather than the values being copied. Just do filterSearch = angular.copy(searchModel)
where do I put that at? I've got this? <input ng-model="searchModel.address" /> <input ng-model="searchModel.city" /> <button ng-click="ilterSearch = angular.copy(searchModel)">Search</button>
I'm sorry I'm really bad with Angular. :)

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.