Tosm
Tosm

Reputation: 35

ng-table Filter via select drop down menu

I am trying to create a Ng-table along with a select drop down menu for filtering the result. This is what i have gotten so far.

1) How do i get rid of the pagination created as a result of the filter menu? I only need one pagination. I understand that i can do it via CSS .ng-table-pager { display: none; }. However, both pagination will be gone.

2) The default filter menu is blank which shows ALL data. This is great. How do i change the blank field to "ALL" text field?

3) Instead of straight away filtering, can i add a submit button before invoking the filter function?

(function() {
    "use strict";

    angular.module("uCloud", ["ngTable"])
        .controller("myController", myController);

    myController.$inject = ["NgTableParams"];

    function myController(NgTableParams) {

        this.nameFilter = [
            {id:"teste1", title:"-> teste1"},
            {id:"teste2", title:"-> teste 2"},
            {id:"teste3", title:"-> teste 3"},
            {id:"teste4", title:"-> teste 4"},
            {id:"teste5", title:"-> teste 5"},
        ];



        this.objectTest = [

            {name: "teste1", description: "testando1"},
            {name: "teste2", description: "testando2"},
            {name: "teste3", description: "testando3"},
            {name: "teste4", description: "testando4"}
        ];

        this.tableParams = new NgTableParams({}, {
            dataset: this.objectTest
        });
    }
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>ng-table - Select Filter</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css'>
</head>

<body>
<div ng-app="uCloud" class="container-fluid">
    <div class="row">
        <div class="col-md-3" ng-controller="myController as demo">
            <h3>ngTable</h3>
            <table ng-table="demo.tableParams" class="table table-condensed table-bordered table-striped">
                <tr ng-repeat="row in $data">

                    <td data-title="'Name'"
                        filter-data="demo.nameFilter">{{row.name}}
                    </td>



                    <td data-title="'Description'"
                        filter-data="demo.descriptionFilter">{{row.description}}
                    </td>


                </tr>
            </table>

            <table ng-table="demo.tableParams" class="">

            <td data-title="'Name'"
                filter="{name: 'select'}"
                filter-data="demo.nameFilter">All{{row.name}}
            </td>            </table>




        </div>
    </div>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.min.js'></script>

<script src='https://unpkg.com/ng-table/bundles/ng-table.min.js'></script>
<script src="index.js"></script>

</body>
</html>

Upvotes: 2

Views: 3313

Answers (1)

Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17486

First things first: a warning, you're loading Angular twice. Get rid of one of the <script> tags pointing to Angular.


1) How do i get rid of the pagination created as a result of the filter menu? I only need one pagination.

You see two pagination controls because you've created two ng-tables. You need to get rid of the table for the filter and use a normal select HTML element for that.

  <select ng-model="demo.tableParams.filter()['name']">
    <option value="">All</option>
    <option value="teste1">->teste1</option>
    <option value="teste2">->teste2</option>
    <option value="teste3">->teste3</option>
    <option value="teste4">->teste4</option>
    <option value="teste5">->teste5</option>
  </select>

2) The default filter menu is blank which shows ALL data. This is great. How do i change the blank field to "ALL" text field?

As shown above, just add an option with value="". This will make all the items in the dataset a match.

3) Instead of straight away filtering, can i add a submit button before invoking the filter function?

Check out NgTable samples - change filter values programmatically.

Working demo here.

    angular.module("uCloud", ["ngTable"])
  .controller("myController", myController);

myController.$inject = ["NgTableParams"];

function myController(NgTableParams) {

  this.tableParams = new NgTableParams({}, {
    dataset: [{
      name: "teste1",
      description: "testando1"
    }, {
      name: "teste2",
      description: "testando2"
    }, {
      name: "teste3",
      description: "testando3"
    }, {
      name: "teste4",
      description: "testando4"
    }],
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src='https://unpkg.com/ng-table/bundles/ng-table.min.js'></script>
<script src="index.js"></script>
<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>ng-table - Select Filter</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css'>
</head>

<body>
<div ng-app="uCloud" class="container-fluid">
  <div class="row">
<div class="col-md-3" ng-controller="myController as demo">
  <h3>ngTable</h3>
  <table ng-table="demo.tableParams" class="table table-condensed table-bordered table-striped">
    <tr ng-repeat="row in $data">
      <td data-title="'Name'" filter-data="demo.nameFilter">{{row.name}}
      </td>
      <td data-title="'Description'" filter-data="demo.descriptionFilter">{{row.description}}
      </td>
    </tr>
  </table>
  <select ng-model="demo.tableParams.filter()['name']">
    <option value="">All</option>
    <option value="teste1">->teste1</option>
    <option value="teste2">->teste2</option>
    <option value="teste3">->teste3</option>
    <option value="teste4">->teste4</option>
    <option value="teste5">->teste5</option>
  </select>
</div>
  </div>
</div>


</body>
</html>

Upvotes: 2

Related Questions