Skip to content

Commit 1bbf8d9

Browse files
committed
Implemented pagination, refactored links function and added comments.
1 parent 6be9ab1 commit 1bbf8d9

File tree

1 file changed

+24
-12
lines changed
  • django_responsive_tables2/static/django_responsive_tables2

1 file changed

+24
-12
lines changed
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
function updateHeaders(tableId, url, tableElement) {
2-
// Get the column header elements.
3-
headerElements = tableElement.getElementsByTagName('TH');
4-
for (let x = 0; x < headerElements.length; x++) {
5-
const headerLinks = headerElements[x].getElementsByTagName('A');
6-
for (let y = 0; y < headerLinks.length; y++) {
7-
headerLinks[y].onclick = function(e) {
1+
/** Updates the specified tables related links. */
2+
function updateLinks(tableId, url, elements) {
3+
for (let x = 0; x < elements.length; x++) {
4+
const links = elements[x].getElementsByTagName('A');
5+
for (let y = 0; y < links.length; y++) {
6+
links[y].onclick = function(e) {
87
e.preventDefault();
9-
url = buildUrl(url, decodeParameters(headerLinks[y].getAttribute('href')));
8+
url = buildUrl(url, decodeParameters(links[y].getAttribute('href')));
109
updateTable(tableId, url);
1110
};
1211
}
1312
}
1413
}
1514

15+
/** Updates the specified table and submits search queries. */
1616
function updateTable(tableId, url, search=null) {
1717
const xhr = new XMLHttpRequest();
1818
xhr.onreadystatechange = function() {
@@ -21,10 +21,19 @@ function updateTable(tableId, url, search=null) {
2121
// Replace the existing table with the table fetched from the request.
2222
var tableElement = document.getElementById(tableId);
2323
tableElement.innerHTML = xhr.responseText;
24-
updateHeaders(tableId, url, tableElement);
24+
25+
// Update header column sort links.
26+
const headerElements = tableElement.getElementsByTagName('TH');
27+
updateLinks(tableId, url, headerElements);
28+
29+
// Update page links.
30+
const paginationElements = tableElement.getElementsByTagName('pagination');
31+
updateLinks(tableId, url, paginationElements);
2532
}
2633
}
2734
};
35+
36+
// If there is a search, add search parameters to our AJAX call.
2837
if (search) {
2938
let searchParam = {"search": search};
3039
url = buildUrl(url, searchParam);
@@ -33,6 +42,7 @@ function updateTable(tableId, url, search=null) {
3342
xhr.send();
3443
}
3544

45+
/** Encodes a dictionary to a URL parameter string. */
3646
function encodeParameters(params) {
3747
let result = '?';
3848
for (let i = 0; i < Object.keys(params).length; i++) {
@@ -45,13 +55,15 @@ function encodeParameters(params) {
4555
return result;
4656
}
4757

58+
/** Gets a dictionary of parameters from a given URL. */
4859
function decodeParameters(url) {
4960
let result = {};
5061
let parameters = url.split("?");
51-
// If no question mark was found, there were no parameters.
62+
// No question mark, no parameters.
5263
if (parameters.length <= 1) return { };
5364
else parameters = parameters[1];
5465
parameters = parameters.split("&");
66+
// Iterate through each parameter key and value.
5567
for (i in parameters) {
5668
pair = parameters[i].split("=");
5769
if (pair.length == 2) {
@@ -63,19 +75,19 @@ function decodeParameters(url) {
6375
return result;
6476
}
6577

78+
/** Removes all parameters from a given URL. */
6679
function clearParameters(url) {
6780
split = url.split("?");
6881
if (split.length > 1) return split[0];
6982
else return url;
7083
}
7184

85+
//** Appends parameters to a given URL. */
7286
function buildUrl(url, params, keep_params=true) {
7387
let new_params = { };
7488
if (keep_params==true) new_params = decodeParameters(url);
75-
7689
for (const [key, value] of Object.entries(params)) {
7790
new_params[key] = value;
7891
}
79-
8092
return clearParameters(url) + encodeParameters(new_params);
8193
}

0 commit comments

Comments
 (0)