Skip to content

Commit 15d8e71

Browse files
Merge pull request #62 from torabian/adding-md-data-table
Adding md data table
2 parents 327fcd3 + 63eadbb commit 15d8e71

File tree

7 files changed

+144
-3
lines changed

7 files changed

+144
-3
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"angular": "^1.5.0",
1515
"angular-nvd3": "1.0.0-rc.2",
1616
"angular-material": "^1.0.6",
17-
"angular-mocks": "^1.5.0"
17+
"angular-mocks": "^1.5.0",
18+
"angular-material-data-table": "^0.10.10"
1819
},
1920
"devDependencies": {}
2021
}

src/app/components/services/NavService.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
name: 'Table',
2424
icon: 'view_module',
2525
sref: '.table'
26+
},
27+
{
28+
name: 'Data Table',
29+
icon: 'view_module',
30+
sref: '.data-table'
2631
}
2732
];
2833

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
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
(function(){
2+
3+
angular
4+
.module('app')
5+
.controller('DataTableController', [
6+
'tableService',
7+
'$scope',
8+
TableController
9+
10+
]);
11+
12+
function TableController(tableService , $scope) {
13+
var vm = this;
14+
15+
vm.tableData = [];
16+
vm.totalItems = 0;
17+
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+
52+
});
53+
54+
}
55+
56+
GetItemsData($scope.query);
57+
58+
59+
60+
61+
}
62+
63+
})();

src/app/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('angularMaterialAdmin', ['ngAnimate', 'ngCookies',
4-
'ngSanitize', 'ui.router', 'ngMaterial', 'nvd3', 'app'])
4+
'ngSanitize', 'ui.router', 'ngMaterial', 'nvd3', 'app' , 'md.data.table'])
55

66
.config(function ($stateProvider, $urlRouterProvider, $mdThemingProvider,
77
$mdIconProvider) {
@@ -37,6 +37,15 @@ angular.module('angularMaterialAdmin', ['ngAnimate', 'ngCookies',
3737
data: {
3838
title: 'Table'
3939
}
40+
})
41+
.state('home.data-table', {
42+
url: '/data-table',
43+
controller: 'DataTableController',
44+
controllerAs: 'vm',
45+
templateUrl: 'app/views/data-table.html',
46+
data: {
47+
title: 'Table'
48+
}
4049
});
4150

4251
$urlRouterProvider.otherwise('/dashboard');

src/app/views/data-table.html

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

src/app/views/partials/checkboxes.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ <h4 flex="82">{{vm.remaining()}} of {{vm.todos.length}} remaining</h4>
2323
Remove completed
2424
</md-button>
2525
</form>
26-
</md-content>
26+
</md-content>

0 commit comments

Comments
 (0)