Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 929
Viswa Avatar asked Oct 30 '13 04:10

Viswa


People also ask

How do you filter a list in Excel based on another list?

Step 1: Select data you want to do filter, in this case we select A2:C11, select Data->Advanced. Step 2: On Advanced Filter dialog, check on 'Filter the list, in-place', in List range select $A$2:$A$11, in Criteria range, select $F$2:$F$6. Then click OK.

How do I filter rows instead of columns in Excel?

Ctrl+A, ctrl+C, then in paste special use transpose. Transpose means the cells which you have copied changes from columns to rows and rows to columns. Then you can apply filter.

How do I filter rows in power query?

You can right-click a particular cell in a column to open the shortcut menu for that value. Point to the small filter icon, and then select the filter option you want to use. Power Query displays a type-specific filter based on the data type of the column.


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

Remaining default ngtable configuration.

like image 171
Viswa Avatar answered Sep 19 '22 12:09

Viswa


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 });
}
like image 45
Justin Avatar answered Sep 22 '22 12:09

Justin