-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination.js
102 lines (70 loc) · 2.88 KB
/
pagination.js
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
function paginationCtrl(scope){
scope.person = {};
scope.personData = [];
scope.personDataPaginated = [];
scope.editEnabled = false;
scope.pages = [1];
scope.selectedPage = 1;
let offset = 0,
limit = 5;
scope.submit = function(){
if(scope.personData.length!=0 &&scope.personData.length%5==0){
scope.pages.push(scope.pages[scope.pages.length-1]+1);
}
scope.personData.push(angular.copy(scope.person));
scope.personDataPaginated = scope.personData.slice(offset,offset+limit);
let jsonString = JSON.stringify(scope.personData);
window.localStorage["person"] = jsonString;
scope.person = {};
}
scope.delete = function(index){
scope.personData.splice(index+offset,1);
let jsonString = JSON.stringify(scope.personData);
window.localStorage["person"] = jsonString;
if(scope.personData.length%5==0){
if(scope.pages>1){
scope.selectedPage -= 1;
scope.pages.splice(scope.pages.length-1,1);
}
if(offset>=5)
offset = offset -5;
}
scope.personDataPaginated = scope.personData.slice(offset,offset+limit);
}
scope.edit = function(){
scope.editEnabled = true;
}
scope.makeChanges = function(idx){
//scope.personData[idx] = scope.person;
//console.log(scope.person)
scope.personDataPaginated = scope.personData.slice(offset,offset+limit);
let jsonString = JSON.stringify(scope.personData);
window.localStorage["person"] = jsonString;
scope.editEnabled = false;
}
scope.discardChanges = function(idx){
scope.editEnabled = false;
scope.personDataPaginated[idx+offset] = angular.copy(scope.personData[idx+offset]);
}
scope.navigate = function(page){
offset = 5*(page-1);
scope.personDataPaginated = scope.personData.slice(offset,offset+limit);
scope.selectedPage = parseInt(page);
}
function initialisePersonData(){
var data = window.localStorage["person"];
if(data){
scope.personData = JSON.parse(data);
scope.personDataPaginated = scope.personData.slice(offset,offset+limit);
let pages = 0;
if(scope.personData.length>5)
pages = (scope.personData.length/5)%1==0?scope.personData.length/5: parseInt(scope.personData.length/5) + 1
for(let i= 1;i<pages;i++){
scope.pages.push(i+1);
}
}
}
initialisePersonData();
}
angular.module("pageApp",[])
.controller("paginationCtrl",["$scope",paginationCtrl]);