Search code examples
angularjsangularjs-directiveangular-uingtable

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


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.


Solution

  • 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.