Skip to content

Commit e4a6b39

Browse files
authored
Merge pull request #40 from jan-vitek/master
Extend url query params with perPage and sorting
2 parents c7ab569 + d7f95d0 commit e4a6b39

1 file changed

Lines changed: 43 additions & 13 deletions

File tree

src/jstable.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ const JSTableDefaultConfig = {
6565
// query params names
6666
queryParams: {
6767
page: 'page',
68-
search: 'search'
68+
search: 'search',
69+
sortColumn: 'sortColumn',
70+
sortDirection: 'sortDirection',
71+
perPage: 'perPage'
6972
},
7073
// append query params on events
7174
addQueryParams: true,
@@ -566,6 +569,14 @@ class JSTable {
566569
});
567570
}
568571

572+
_setQueryParam(key, value) {
573+
if (!this.config.addQueryParams) return;
574+
575+
const url = new URL(window.location.href);
576+
url.searchParams.set(this.config.queryParams[key], value);
577+
window.history.replaceState(null, null, url);
578+
}
579+
569580
_bindEvents() {
570581
var that = this;
571582

@@ -578,11 +589,7 @@ class JSTable {
578589
let new_page = parseInt(node.getAttribute("data-page"), 10);
579590
that.paginate(new_page);
580591

581-
if (that.config.addQueryParams) {
582-
const url = new URL(window.location.href);
583-
url.searchParams.set(that.config.queryParams.page, new_page);
584-
window.history.replaceState(null, null, url);
585-
}
592+
that._setQueryParam('page', new_page)
586593
}
587594

588595
if (node.nodeName === "TH" && node.hasAttribute("data-sortable")) {
@@ -591,7 +598,11 @@ class JSTable {
591598
return false;
592599

593600
event.preventDefault();
594-
that.sort(node.cellIndex, node.classList.contains("asc") ? "desc" : "asc");
601+
let sortDirection = node.classList.contains("asc") ? "desc" : "asc";
602+
that.sort(node.cellIndex, sortDirection);
603+
604+
that._setQueryParam('sortColumn', node.cellIndex)
605+
that._setQueryParam('sortDirection', sortDirection)
595606
}
596607
});
597608

@@ -604,6 +615,8 @@ class JSTable {
604615
that._emit("perPageChange", that.config.perPage, value);
605616
that.config.perPage = value;
606617
that.update();
618+
619+
that._setQueryParam('perPage', value)
607620
}
608621
});
609622
}
@@ -614,11 +627,7 @@ class JSTable {
614627
e.preventDefault();
615628
that.search(e.target.value);
616629

617-
if (that.config.addQueryParams) {
618-
const url = new URL(window.location.href);
619-
url.searchParams.set(that.config.queryParams.search, e.target.value);
620-
window.history.replaceState(null, null, url);
621-
}
630+
that._setQueryParam('search', e.target.value)
622631
}
623632
});
624633
}
@@ -795,7 +804,20 @@ class JSTable {
795804

796805
async _parseQueryParams() {
797806
const urlParams = new URLSearchParams(window.location.search);
798-
807+
808+
let perPage = urlParams.get(this.config.queryParams.perPage);
809+
if (perPage) {
810+
perPage = parseInt(perPage)
811+
this.config.perPage = perPage;
812+
let selectors = this.wrapper.querySelectorAll('.' + this.config.classes.selector);
813+
selectors.forEach(function (selector) {
814+
selector.querySelectorAll('option').forEach(opt => opt.removeAttribute('selected'))
815+
selector.value = perPage;
816+
selector.querySelector(`option[value='${perPage}']`).setAttribute('selected', '')
817+
});
818+
this.update()
819+
}
820+
799821
// parse search param and populate search
800822
let search = urlParams.get(this.config.queryParams.search);
801823
if (search) {
@@ -811,6 +833,14 @@ class JSTable {
811833
if (page) {
812834
await this.paginate(parseInt(page));
813835
}
836+
837+
let sortColumn = urlParams.get(this.config.queryParams.sortColumn);
838+
if (sortColumn) {
839+
sortColumn = parseInt(sortColumn)
840+
let sortDirection = urlParams.get(this.config.queryParams.sortDirection);
841+
sortDirection = (sortDirection == undefined) ? "asc" : sortDirection;
842+
this.sort(sortColumn, sortDirection);
843+
}
814844
}
815845
}
816846

0 commit comments

Comments
 (0)