ng-table插件(四-排序基础)
2016-06-20 本文已影响637人
MakingChoice
基本排序
ng-table提供一个表头来增加基本的排序功能。
(1)指定具体哪一列需要排序
(2)可以通过<code>NgTableParams</code>可选择的提供初始列排序
(3)在这个例子中需要为<code>NgTableParams.sorting()</code>设置值,然后展示出来。
下面是代码:
<div ng-app="myApp" class="container-fluid">
<div class="row">
<div class="col-xs-12">
<h2 class="page-header">Sorting - basic example</h2>
<div class="row">
<div class="col-md-6">
<div class="bs-callout bs-callout-info">
<h4>Overview</h4>
<p><code>ngTable</code> provides a header template that adds sorting to a html table</p>
<ul>
<li>Specify which column should be sortable</li>
<li>Optionally supply the initial columns to sort to the <code>NgTableParams</code> constructor call</li>
</ul>
</div>
</div>
<div class="col-md-6">
<div class="bs-callout bs-callout-warning">
<h4>Got a <code>getData</code> function?</h4>
<p>In this case you'll need to apply the values from <code>NgTableParams.sorting()</code> to the data you want to display in your table. This is typically the case when the data is being fetched from the server.</p>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6" ng-controller="demoController as demo">
<h3>ngTable directive</h3>
<table ng-table="demo.tableParams" class="table table-condensed table-bordered table-striped">
<tr ng-repeat="row in $data">
<td data-title="'Name'" sortable="'name'">{{row.name}}</td>
<td data-title="'Age'" sortable="'age'">{{row.age}}</td>
<td data-title="'Money'">{{row.money}}</td>
</tr>
</table>
</div>
<div class="col-md-6" ng-controller="dynamicDemoController as demo">
<h3>ngTableDynamic directive</h3>
<table ng-table-dynamic="demo.tableParams with demo.cols" class="table table-condensed table-bordered table-striped">
<tr ng-repeat="row in $data">
<td ng-repeat="col in $columns">{{row[col.field]}}</td>
</tr>
</table>
</div>
</div>
</div>
(function() {
"use strict";
var app = angular.module("myApp", ["ngTable", "ngTableDemos"]);
app.controller("demoController", demoController);
demoController.$inject = ["NgTableParams", "ngTableSimpleList"];
//定义controller依赖注入
function demoController(NgTableParams, simpleList) {//默认控制器,传入数据产生表格
this.tableParams = new NgTableParams({
// initial sort order
sorting: { name: "asc" } //asc表示升序
}, {
dataset: simpleList
});
}
app.controller("dynamicDemoController", dynamicDemoController);
dynamicDemoController.$inject = ["NgTableParams", "ngTableSimpleList"];
//定义controller依赖注入
function dynamicDemoController(NgTableParams, simpleList) {
this.cols = [//设置哪些可以排序,哪些不可以
{ field: "name", title: "Name", sortable: "name", show: true },
{ field: "age", title: "Age", sortable: "age", show: true },
{ field: "money", title: "Money", show: true }
];
this.tableParams = new NgTableParams({
// initial sort order
sorting: { age: "desc" } //降序
}, {
dataset: simpleList
});
}
})();
(function() {
"use strict";
angular.module("myApp").run(configureDefaults);
configureDefaults.$inject = ["ngTableDefaults"];
function configureDefaults(ngTableDefaults) {
ngTableDefaults.params.count = 5;//通过provider服务,调用config设置基本参数
ngTableDefaults.settings.counts = [];
}
})();
sorting.png