This repository was archived by the owner on Dec 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathangular-table.js
More file actions
153 lines (131 loc) · 5.34 KB
/
angular-table.js
File metadata and controls
153 lines (131 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
var app = angular.module('myapp', []);
// AngularJS Table service
app.service('angularTable', function () {
var configureClient = function ($scope, data, itemsPerPage) {
init($scope, itemsPerPage);
$scope.load = function () {
if (_.isEmpty($scope.filterText)) {
$scope.filteredItems = data;
} else {
var filter = $scope.filterText.toLowerCase();
$scope.currentPage = 0;
$scope.filteredItems = data.filter(function (item) {
return JSON.stringify(item).toLowerCase().indexOf(filter) != -1;
});
}
$scope.groupToPages();
};
// Calculates page records
$scope.groupToPages = function () {
$scope.pagedItems = [];
for (var i = 0; i < $scope.filteredItems.length; i++) {
if (i % $scope.itemsPerPage === 0) {
$scope.pagedItems[Math.floor(i / $scope.itemsPerPage)] = [$scope.filteredItems[i]];
} else {
$scope.pagedItems[Math.floor(i / $scope.itemsPerPage)].push($scope.filteredItems[i]);
}
}
};
$scope.sortBy = function (newSortingOrder) {
data = _.sortBy(data, newSortingOrder);
if ($scope.sort.order === 'desc') {
data = data.reverse();
}
$scope.sort.order = $scope.sort.order === 'asc' ? 'desc' : 'asc';
$scope.sort.column = newSortingOrder;
$scope.load();
};
};
function init($scope, itemsPerPage) {
$scope.sort = {
column: 'Id',
order: 'asc'
};
$scope.gap = 5;
$scope.filteredItems = [];
$scope.itemsPerPage = itemsPerPage;
$scope.pagedItems = [];
$scope.currentPage = 0;
$scope.filterText = '';
$scope.totalItems = 0;
$scope.totalPages = 0;
$scope.prevPage = function () {
if ($scope.currentPage > 0) {
$scope.currentPage--;
}
};
$scope.nextPage = function (pageSize) {
if ($scope.currentPage < pageSize) {
$scope.currentPage++;
}
};
$scope.setPage = function (n) {
$scope.currentPage = n;
};
$scope.range = function (size, start, end) {
var ret = [];
if (size < end) {
end = size;
start = size - $scope.gap;
}
for (var i = start; i < end; i++) {
if (i >= 0) ret.push(i);
}
return ret;
};
$scope.selectedCls = function (column) {
if (column == $scope.sort.column) {
return ('glyphicon glyphicon-sort-by-attributes' + ($scope.sort.order === 'asc' ? '-alt' : ''));
} else {
return 'glyphicon glyphicon-sort';
}
};
$scope.search = function (searchText) {
if (_.isEmpty(searchText)) {
$scope.filterText = '';
}
$scope.load();
};
}
return {
createClient: function ($scope, data, itemsPerPage) {
configureClient($scope, data, itemsPerPage);
return $scope;
}
};
});
// AngularJS Table paging directive
app.directive('tablePaging', function () {
return {
scope: {
tablePaging: '='
},
template:
'<div class="row" ng-show="tablePaging.filteredItems.length">' +
'<div class="col-xs-4"><div class="pagination"><strong>Total:</strong> {{tablePaging.filteredItems.length}} items</div></div>' +
'<div class="col-xs-8">' +
'<div class="pagination pull-right">' +
'<ul class="pagination">' +
'<li ng-class="{disabled: tablePaging.currentPage == 0}">' +
'<a href ng-click="tablePaging.setPage(0)">First</a>' +
'</li>' +
'<li ng-class="{disabled: tablePaging.currentPage == 0}">' +
'<a href ng-click="tablePaging.prevPage()">« Prev</a>' +
'</li>' +
'<li ng-repeat="n in tablePaging.range(tablePaging.pagedItems.length, tablePaging.currentPage, tablePaging.currentPage + tablePaging.gap)"' +
'ng-class="{active: n == tablePaging.currentPage}" ng-click="tablePaging.setPage(n)">' +
'<a href ng-bind="n + 1">1</a>' +
'</li>' +
'<li ng-class="{disabled: tablePaging.currentPage == tablePaging.pagedItems.length - 1}">' +
'<a href ng-click="tablePaging.nextPage(tablePaging.pagedItems.length - 1)">Next »</a>' +
'</li>' +
'<li ng-class="{disabled: tablePaging.currentPage == tablePaging.pagedItems.length - 1}">' +
'<a href ng-click="tablePaging.setPage(tablePaging.pagedItems.length - 1)">Last</a>' +
'</li>' +
'</ul>' +
'</div>' +
'</div>' +
'</div>'
};
});