How to filter a row based on any column data with a single text field

I am using ng-table .

I tried using the filter specified in example , but to filter each column I need to have a separate text field.

But what I require is a single text field to search for any row based on any column data.

How can i achieve this?

As well as the jquery datatable search jquery datatable .

+6
source share
2 answers

This is how i did

Html

  <input type="text" ng-model="searchUser"> <table ng-table="tableParams"> <tr ng-repeat="user in $data"> ... </tr> </table> 

Script

  var usersData = []; // initial data $scope.tableParams = new ngTableParams({ page: 1, count: 7 }, { counts : [7,14,21,28], getData: function($defer, params) { var searchedData = searchData(); params.total(searchedData.length); $scope.users = searchedData.slice((params.page() - 1) * params.count(), params.page() * params.count()); $defer.resolve($scope.users); }, $scope: { $data: {} } }); $scope.$watch("searchUser", function () { $scope.tableParams.reload(); }); var searchData = function(){ if($scope.searchUser) return $filter('filter')(usersData,$scope.searchUser); return usersData; } 

The remaining default configuration is ngtable .

+10
source

Based on the original question, if you download all your data first, then it's pretty easy. I used this http://ng-table.com/#/filtering/demo-api for reference, and then added type filtering with ng-change.

View:

 <form name="awesomeForm" novalidate> <div class="input-group"> <input type="text" class="form-control" placeholder="Search term" name="searchTerm" ng-model="globalSearchTerm" ng-change="applyGlobalSearch(globalSearchTerm)" required /> <span class="input-group-btn"> <button class="btn btn-default" type="submit" > <span class="glyphicon glyphicon-search"></span> </button> </span> </div> </form> 

In your controller (after loading the data into the table):

 $scope.applyGlobalSearch = function(term) { $scope.tableParams.filter({ $: term }); } 
+1
source

Source: https://habr.com/ru/post/957003/


All Articles