-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
129 lines (106 loc) · 3.78 KB
/
script.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
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
let container = document.createElement("div")
container.classList.add("container","table-striped")
let table = document.createElement("table")
table.classList.add("table")
let thead = document.createElement("thead")
thead.classList.add("bg-dark","text-light")
let trHead = document.createElement("tr")
let tdHead1 = document.createElement("td")
tdHead1.innerHTML = "Id"
let tdHead2 = document.createElement("td")
tdHead2.innerHTML = "Name"
let tdHead3 = document.createElement("td")
tdHead3.innerHTML = "Email"
let tbody = document.createElement("tbody")
tbody.setAttribute("id","table-body")
let span = document.createElement("span")
span.innerHTML = ""
let pagination = document.createElement("div")
pagination.setAttribute("id","pagination-wrapper")
pagination.classList.add("container","buttons")
trHead.append(tdHead1)
trHead.append(tdHead2)
trHead.append(tdHead3)
thead.append(trHead)
table.append(thead)
table.append(tbody)
container.append(span)
container.append(table)
document.body.append(container)
document.body.append(pagination)
function createTableRow(id,name,email){
let tr = document.createElement("tr")
let td1 = document.createElement("td")
let td2 = document.createElement("td")
let td3 = document.createElement("td")
td1.innerHTML = id
td2.innerHTML = name
td3.innerHTML = email
tr.append(td1)
tr.append(td2)
tr.append(td3)
tbody.append(tr)
}
let request = new XMLHttpRequest();
request.open("GET","https://raw.githubusercontent.com/Rajavasanthan/jsondata/master/pagenation.json",true);
request.send();
request.onload = function(){
let tabledata = JSON.parse(this.response);
let state = {
"queryset" : tabledata,
"page" : 1 ,
"rows" : 5,
"window" : 5
}
buildTable()
function pagination(queryset,page,rows){
let trimStart = (page-1) * rows;
let trimEnd = trimStart + rows;
let trimedData = queryset.slice(trimStart,trimEnd)
let pages = Math.ceil(tabledata.length/rows);
return {
"queryset":trimedData,
"pages" : pages
}
}
function pageButtons(pages){
let wrapper = document.getElementById("pagination-wrapper")
wrapper.innerHTML = ""
let maxLeft = (state.page - Math.floor(state.window /2));
let maxRight = (state.page + Math.floor(state.window /2));
if(maxLeft<1){
maxLeft = 1
maxRight = state.window
}
if(maxRight > pages){
maxLeft = pages-(state.window-1)
maxRight = pages
if(maxLeft < 1){
maxLeft = 1;
}
}
for(let page = maxLeft; page <= maxRight ; page ++){
wrapper.innerHTML = wrapper.innerHTML + `<button value="${page}" class="page">${page}</button>`
}
if(state.page!==1){
wrapper.innerHTML = `<button value=${1} class="page">« First</button>` + wrapper.innerHTML
}
if (state.page != pages) {
wrapper.innerHTML += `<button value=${pages} class="page">Last »</button>`
}
let dynamic = document.getElementById("pagination-wrapper")
dynamic.addEventListener("click",function(e){
document.getElementById("table-body").innerHTML = ""
state.page = Number(e.target.value)
buildTable()
})
}
function buildTable(){
let data = pagination(state.queryset,state.page,state.rows)
let array = data.queryset
for(let i=0;i<array.length;i++){
createTableRow(array[i].id,array[i].name,array[i].email);
}
pageButtons(data.pages)
}
}