0

In this code, when I type the record name it is searching in text box, I need to search the record when I type and click the enter button it should search the record name.

<input type="text" class="form-control" name="search" placeholder="Enter keyword to search" ng-model="Google" style="background-color:#5b2c2c;color:white;">
<table  class="table" border="1" style="margin:0;margin-left:90px;background-color:white;width:80%;border:5px solid green">
   <thead>
      <tr>
         <th><a>Google</a></th>
         <th><a>Facebook</a></th>
         <th><a>Twitter</a></th>
         <th><a>Yahoo</a></th>
      </tr>
   </thead>
   <tbody>
      <tr ng-repeat="record in collection | filter:Google" ng-class-even="'stripped'">
         <td>{{record.Google}}</td>
         <td>{{record.Facebook}}</td>
         <td>{{record.Twitter}}</td>
         <td>{{record.Yahoo}}</td>
      </tr>
   </tbody>
</table>

<script>
 var app = angular.module('myapp', []);
 app.controller('myctrl', function($scope) {
     $scope.collection = [{
             Google: 'Dhoni',
             Facebook: 'Simla',
             Twitter: '5000'
         },
         {
             Google: 'Kohli',
             Facebook: 'Manali',
             Twitter: '15000'
         },
         {
             Google: 'Virat',
             Facebook: 'Rajasthan',
             Twitter: '35000'
         },
         {
             Google: 'Yuvraj',
             Facebook: 'Kerala',
             Twitter: '35000'
         },
         {
             Google: 'Singh',
             Facebook: 'Mysore',
             Twitter: '35000'
         },
         {
             Google: 'Murali',
             Facebook: 'OOTY',
             Twitter: '20000'
         },
         {
             Google: 'Vijay',
             Facebook: 'Goa',
             Twitter: '20000'
         }
     ];
 });
</script>

4 Answers 4

1
<input type="text" class="form-control" name="search" placeholder="Enter keyword to search" ng-model="searchText" style="background-color:#5b2c2c;color:white;">
<button ng-click="Google=searchText">Search</button>
Sign up to request clarification or add additional context in comments.

Comments

1

You can easily do it by using filter inside controller and on click of button call function. Here $scope.Search is text by which filter is done. and item is set of item on which filter is perform !

    $scope.searchMe = function(){
       $scope.items = $filter('filter')($scope.items, $scope.search);
   }

Do not forget to inject $filter in controller.

Comments

1
<input type="text" class="form-control" name="search" placeholder="Enter keyword to search" ng-model="searchText" style="background-color:#5b2c2c;color:white;">

<tr ng-repeat="record in collection | filter:Google" ng-class-even="'stripped'">
<button ng-click="Google.Facebook=searchText">Search</button> // filter facebook column only
<button ng-click="Google=searchText">Search</button> // can filter any column

Comments

0
<input type="text" ng-model="searchText" ng-keyPress="filterbyKeyword($event)" placeholder="Enter keyword to search" />

$scope.filterbyKeyword=function(event)
{
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode=='13')
    {
        //your code 
    }
}

Comments

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.