Skip to content

Commit d0b9fef

Browse files
author
hi
committed
Added a basic pagination table (md-data-table)
Now you have pagination, Page, Rows per page, and ordering.
1 parent 3ddcd8f commit d0b9fef

File tree

3 files changed

+105
-29
lines changed

3 files changed

+105
-29
lines changed

src/app/components/services/TableService.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,29 @@
5959
}
6060
];
6161

62+
function PickRandom() {
63+
return Object.assign({}, tableData[Math.floor(Math.random()*tableData.length)]);
64+
}
65+
6266
return {
6367
loadAllItems : function() {
6468
return $q.when(tableData);
69+
},
70+
/**
71+
* Query expects that `limit`,`page`, and `order` fields be present
72+
*/
73+
loadByPagination: function (query) {
74+
query = query || {limit:10,page:1};
75+
76+
var list = [];
77+
var start = (query.page-1)*query.limit;
78+
var end = start + query.limit;
79+
for (var i = start; i < end; i++) {
80+
var f = PickRandom();
81+
f.id = i+1;
82+
list.push(f);
83+
}
84+
return $q.when({items:list,count:800});
6585
}
6686
};
6787
}

src/app/controllers/DataTableController.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,60 @@
44
.module('app')
55
.controller('DataTableController', [
66
'tableService',
7+
'$scope',
78
TableController
9+
810
]);
911

10-
function TableController(tableService) {
12+
function TableController(tableService , $scope) {
1113
var vm = this;
1214

1315
vm.tableData = [];
16+
vm.totalItems = 0;
1417

15-
tableService
16-
.loadAllItems()
17-
.then(function(tableData) {
18-
vm.tableData = [].concat(tableData);
18+
$scope.selected = [];
19+
20+
$scope.query = {
21+
order: 'name',
22+
limit: 10,
23+
page: 1
24+
};
25+
$scope.selected = [];
26+
27+
$scope.render = function (T) {
28+
return T;
29+
}
30+
var lastQuery = null;
31+
vm.getItems = function () {
32+
/**
33+
* I don't know why this function is being called too many times,
34+
* it supposed to call once per pagination, so the next 3 lines are only to avoid
35+
* multiple requests.
36+
*/
37+
var query = JSON.stringify($scope.query);
38+
if (query == lastQuery) return;
39+
lastQuery = query;
40+
GetItemsData($scope.query);
41+
42+
}
43+
44+
function GetItemsData(query) {
45+
tableService
46+
.loadByPagination(query)
47+
.then(function(tableData) {
48+
vm.tableData = tableData.items;
49+
// Represents the count of database count of records, not items array!
50+
vm.totalItems = tableData.count;
51+
1952
});
53+
54+
}
55+
56+
GetItemsData($scope.query);
57+
58+
59+
60+
2061
}
2162

2263
})();

src/app/views/data-table.html

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
<!-- TODO: Replace table to table component when https://github.com/angular/material/issues/796 will closed -->
2-
<div class="table-responsive-vertical md-whiteframe-z1">
3-
<table id="table" class="table table-hover table-bordered">
4-
<thead>
5-
<tr>
6-
<th>#</th>
7-
<th>Issue</th>
8-
<th>Status</th>
9-
<th>Progress</th>
10-
</tr>
11-
</thead>
12-
<tbody>
13-
<tr ng-repeat="data in vm.tableData track by $index">
14-
<td data-title="ID">{{$index + 1}}</td>
15-
<td data-title="Issue">{{data.issue}}</td>
16-
<td data-title="Status">{{data.status}}</td>
17-
<td data-title="Progress">
18-
<md-progress-linear class="table-progress {{data.class}}"
19-
md-mode="determinate"
20-
value={{data.progress}}>
21-
</md-progress-linear>
22-
</td>
23-
</tr>
24-
</tbody>
25-
</table>
2+
<md-toolbar class="md-table-toolbar md-default">
3+
<div class="md-toolbar-tools">
4+
<span>Nutrition</span>
265
</div>
6+
</md-toolbar>
7+
8+
<!-- exact table from live demo -->
9+
<md-table-container>
10+
<table md-table md-row-select multiple ng-model="selected" >
11+
<thead md-head md-order="query.order" md-on-reorder="vm.getItems()">
12+
<tr md-row>
13+
<th md-column md-numeric>ID</th>
14+
<th md-column md-order-by="nameToLower"><span>Dessert (100g serving)</span></th>
15+
<th md-column md-numeric md-order-by="calories.value"><span>Calories</span></th>
16+
<th md-column md-numeric>Fat (g)</th>
17+
<th md-column md-numeric>Carbs (g)</th>
18+
19+
</tr>
20+
</thead>
21+
<tbody md-body>
22+
<tr md-row md-select="item" md-select-id="name" md-auto-select ng-repeat="item in vm.tableData">
23+
<td md-cell>{{item.id}}</td>
24+
<td md-cell>{{item.issue}}</td>
25+
<td md-cell>{{item.progress}}</td>
26+
<td md-cell>{{item.status}}</td>
27+
<td md-cell>
28+
<md-progress-linear class="table-progress {{item.class}}"
29+
md-mode="determinate"
30+
value={{item.progress}}>
31+
</md-progress-linear>
32+
</td>
33+
34+
35+
36+
</tr>
37+
</tbody>
38+
</table>
39+
</md-table-container>
40+
41+
<md-table-pagination md-limit="query.limit" md-limit-options="[5, 10, 15]" md-page="query.page" md-total="{{vm.totalItems}}" md-on-paginate="vm.getItems()" md-page-select></md-table-pagination>

0 commit comments

Comments
 (0)