Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend url query params with perPage and sorting #40

Merged
merged 2 commits into from
Jan 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 43 additions & 13 deletions src/jstable.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ const JSTableDefaultConfig = {
// query params names
queryParams: {
page: 'page',
search: 'search'
search: 'search',
sortColumn: 'sortColumn',
sortDirection: 'sortDirection',
perPage: 'perPage'
},
// append query params on events
addQueryParams: true,
Expand Down Expand Up @@ -567,6 +570,14 @@ class JSTable {
});
}

_setQueryParam(key, value) {
if (!this.config.addQueryParams) return;

const url = new URL(window.location.href);
url.searchParams.set(this.config.queryParams[key], value);
window.history.replaceState(null, null, url);
}

_bindEvents() {
var that = this;

Expand All @@ -579,11 +590,7 @@ class JSTable {
let new_page = parseInt(node.getAttribute("data-page"), 10);
that.paginate(new_page);

if (that.config.addQueryParams) {
const url = new URL(window.location.href);
url.searchParams.set(that.config.queryParams.page, new_page);
window.history.replaceState(null, null, url);
}
that._setQueryParam('page', new_page)
}

if (node.nodeName === "TH" && node.hasAttribute("data-sortable")) {
Expand All @@ -592,7 +599,11 @@ class JSTable {
return false;

event.preventDefault();
that.sort(node.cellIndex, node.classList.contains("asc") ? "desc" : "asc");
let sortDirection = node.classList.contains("asc") ? "desc" : "asc";
that.sort(node.cellIndex, sortDirection);

that._setQueryParam('sortColumn', node.cellIndex)
that._setQueryParam('sortDirection', sortDirection)
}
});

Expand All @@ -605,6 +616,8 @@ class JSTable {
that._emit("perPageChange", that.config.perPage, value);
that.config.perPage = value;
that.update();

that._setQueryParam('perPage', value)
}
});
}
Expand All @@ -615,11 +628,7 @@ class JSTable {
e.preventDefault();
that.search(e.target.value);

if (that.config.addQueryParams) {
const url = new URL(window.location.href);
url.searchParams.set(that.config.queryParams.search, e.target.value);
window.history.replaceState(null, null, url);
}
that._setQueryParam('search', e.target.value)
}
});
}
Expand Down Expand Up @@ -796,7 +805,20 @@ class JSTable {

async _parseQueryParams() {
const urlParams = new URLSearchParams(window.location.search);


let perPage = urlParams.get(this.config.queryParams.perPage);
if (perPage) {
perPage = parseInt(perPage)
this.config.perPage = perPage;
let selectors = this.wrapper.querySelectorAll('.' + this.config.classes.selector);
selectors.forEach(function (selector) {
selector.querySelectorAll('option').forEach(opt => opt.removeAttribute('selected'))
selector.value = perPage;
selector.querySelector(`option[value='${perPage}']`).setAttribute('selected', '')
});
this.update()
}

// parse search param and populate search
let search = urlParams.get(this.config.queryParams.search);
if (search) {
Expand All @@ -812,6 +834,14 @@ class JSTable {
if (page) {
await this.paginate(parseInt(page));
}

let sortColumn = urlParams.get(this.config.queryParams.sortColumn);
if (sortColumn) {
sortColumn = parseInt(sortColumn)
let sortDirection = urlParams.get(this.config.queryParams.sortDirection);
sortDirection = (sortDirection == undefined) ? "asc" : sortDirection;
this.sort(sortColumn, sortDirection);
}
}
}

Expand Down