ng-table 通过 select 下拉菜单过滤
ng-table Filter via select drop down menu
我正在尝试创建一个 Ng-table 以及一个 select 下拉菜单来过滤结果。这是我到目前为止得到的。
1) 如何摆脱因过滤器菜单而创建的分页?我只需要一个分页。我知道我可以通过 CSS .ng-table-pager {
显示:none;
}.但是,这两个分页都会消失。
2) 默认过滤器菜单为空白,显示所有数据。这很棒。如何将空白字段更改为 "ALL" 文本字段?
3) 除了直接过滤,我可以在调用过滤功能之前添加一个提交按钮吗?
(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>
首先要注意的是:警告,您正在加载 Angular 两次。删除指向 Angular.
的 <script>
标签之一
1) How do i get rid of the pagination created as a result of the filter menu? I only need one pagination.
您看到两个分页控件,因为您创建了两个 ng-table
。您需要摆脱过滤器的 table 并为此使用普通的 select
HTML 元素。
<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?
如上所示,只需添加一个option
和value=""
即可。这将使数据集中的所有项目都匹配。
3) Instead of straight away filtering, can i add a submit button before invoking the filter function?
查看 NgTable samples - change filter values programmatically。
工作演示 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>
我正在尝试创建一个 Ng-table 以及一个 select 下拉菜单来过滤结果。这是我到目前为止得到的。
1) 如何摆脱因过滤器菜单而创建的分页?我只需要一个分页。我知道我可以通过 CSS .ng-table-pager { 显示:none; }.但是,这两个分页都会消失。
2) 默认过滤器菜单为空白,显示所有数据。这很棒。如何将空白字段更改为 "ALL" 文本字段?
3) 除了直接过滤,我可以在调用过滤功能之前添加一个提交按钮吗?
(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>
首先要注意的是:警告,您正在加载 Angular 两次。删除指向 Angular.
的<script>
标签之一
1) How do i get rid of the pagination created as a result of the filter menu? I only need one pagination.
您看到两个分页控件,因为您创建了两个 ng-table
。您需要摆脱过滤器的 table 并为此使用普通的 select
HTML 元素。
<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?
如上所示,只需添加一个option
和value=""
即可。这将使数据集中的所有项目都匹配。
3) Instead of straight away filtering, can i add a submit button before invoking the filter function?
查看 NgTable samples - change filter values programmatically。
工作演示 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>