How do i filter a row based on any column data with single textbox
Asked Answered
E

2

6

I am using ng-table.

I tried to use filter given in the example, but for filtering each column i need to have separate textbox.

But what i tying to achieve is, One textbox to search any row based on any column data.

How can i achieve this ?

Just like jquery datatable search box.

Esurient answered 30/10, 2013 at 4:51 Comment(4)
did you try working with custom filters example?Fifth
@Fifth ya i tried, custom filters also filtering based on single columnEsurient
I don't know this table system...but looking at example 14 External data control. Looks fairly straightforward to create a watch on data set and change it yourself. Keep a copy of original data and use it to reset when user is done filteringFifth
@Fifth Thanks. As you suggested, using example 14.. i done it.Esurient
E
10

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;
}

Remaining default ngtable configuration.

Esurient answered 30/10, 2013 at 7:40 Comment(1)
You might consider prevent reload of table on first load by wrapping the reload inside this this.last !== undefined , a complete example : $scope.$watch("searchUser", function () { if (this.last !== undefined) { $scope.tableParams.reload(); } });Bioluminescence
Q
1

Based on the original question, if you are loading up all your data initially, then it's pretty easy. I used this http://ng-table.com/#/filtering/demo-api for reference and then added typeahead 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 you load up the data in your table):

$scope.applyGlobalSearch = function(term) {
    $scope.tableParams.filter({ $: term });
}
Quillet answered 20/11, 2015 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.