Monojit Sarkar
Monojit Sarkar

Reputation: 2451

Angular: How to perform search on button click

this is a sample code where user write some data in textbox and result filter accordingly but how can i filter data by clicking button?

<div ng-app="myApp" ng-controller="MyController">
 <label>Field: 
   <select ng-model="selectedFieldName">
      <option value="">--Select Account--</option>
          <option ng-repeat="(fieldname,fieldvalue) in customer[0]" ng-value="fieldname | uppercase">{{fieldname | uppercase}}</option>
   </select>  
</label>

<label>data: <input ng-model="searchText"></label>
    <table class="table table-striped table-bordered">
        <tr>
          <td>ID</td>
          <td>First Name</td>
          <td>Last Name</td>
          <td>Salary</td>
          <td>Date of Birth</td>
          <td>City</td>
          <td>Phone</td>
        </tr>
        <tr ng-repeat="item in customer | filter:SearchList ">
        <!-- orderBy:'$index':false -->
            <td>{{ item.id }}</td>
            <td>{{ item.firstname }}</td>
            <td>{{ item.lastname }}</td>
            <td>{{ item.salary }}</td>
            <td>{{ item.dob }}</td>
            <td>{{ item.city }}</td>
            <td>{{ item.phone }}</td>
        </tr>
    </table>
</div>

;var myApp = angular.module('myApp', []);

myApp.controller('MyController', function MyController($scope) {

    $scope.selectedFieldName='';
    $scope.searchText='';

        $scope.SearchList = function(row) {

    if ($scope.selectedFieldName && $scope.searchText) {
      var propVal = row[$scope.selectedFieldName.toLowerCase()]+ '';
      if (propVal) {
        return propVal.toUpperCase().indexOf($scope.searchText.toUpperCase()) > -1;
      } else {
        return false;
      }
    }
    return true;    
  };

    $scope.customer = [
      {
          'id': 1,
          'firstname': 'Tridip',
          'lastname': 'Bhattacharjee',
          'salary' : 15000,
          'dob': '05/09/2013',
          'city': 'kolkata',
          'phone': '033 2589 7415'
      },
      {
          'id': 2,
          'firstname': 'Arijit',
          'lastname': 'Banerjee',
          'salary' : 25000,
          'dob': '01/09/2010',
          'city': 'Bihar',
          'phone': '033 2589 9999'
      },
      {
          'id': 3,
          'firstname': 'Dibyendu',
          'lastname': 'Saha',
          'salary' : 20000,
          'dob': '06/09/2011',
          'city': 'Rachi',
          'phone': '033 2589 3333'
      },
      {
          'id': 4,
          'firstname': 'Bisu',
          'lastname': 'Das',
          'salary' : 5000,
          'dob': '05/01/2009',
          'city': 'Silchar',
          'phone': '033 2589 2222'
      },
      {
          'id': 5,
          'firstname': 'Soumyajit',
          'lastname': 'Kar',
          'salary' : 12000,
          'dob': '09/08/2011',
          'city': 'kanpur',
          'phone': '033 3333 1894'
      }
    ];
})

please see my code and tell if i add one button then how could i trigger custom filter when user click on button after write data in textbox for search. looking for guide line. thanks

Upvotes: 0

Views: 10877

Answers (1)

Jeffrey Coleman
Jeffrey Coleman

Reputation: 188

You could simply create the filter object upon click.

<input ng-model="searchText"></label>
<button ng-click="submitFilter()" class="search-button">Submit</button>

and then in your controller:

$scope.submitFilter(){
    $scope.SearchList = $scope.searchText;
}

Upvotes: 4

Related Questions