Skip to content

Commit 052f05c

Browse files
authored
Add QueryParameters for controlling the BigTable behavior (#392)
* Add QueryParameters for controlling the BigTable behavior * Fix param behavior [skip-netlify] --------- Co-authored-by: Khemarato Bhikkhu <theopenbuddhistuniversity@gmail.com>
1 parent 5f425e6 commit 052f05c

1 file changed

Lines changed: 68 additions & 37 deletions

File tree

‎_layouts/content-category.html‎

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,22 @@ <h1 class="post-title">{{ page.title | escape }}</h1>
117117
"order": [], // Don't sort until asked to
118118
"layout": {
119119
"topStart": {
120-
"buttons": ["colvis"]
120+
"buttons": [
121+
{
122+
"extend": "colvis",
123+
"columns": ":not(.unhideable)",
124+
"popoverTitle": "<div style='color:gray;font-size:17px;display:flex;justify-content:space-between;width:100%'><span>Name</span><span>✓</span></div>"
125+
},
126+
]
121127
},
122128
"bottomStart": {
123129
"pageLength": {
124-
"menu": [10, 25, 50, 100, 250, 500]
130+
"menu": [10, 25, 50, 100, 250, 500, 1000]
125131
}
126132
}
127133
},
128134
"columnDefs": [
135+
{ "targets": [0], "visible": true, "className": "unhideable"},
129136
{ "targets": [1], "visible": initialwindowwidth > 550},
130137
{ "targets": [2], "width": "70px", "visible": initialwindowwidth > 475},
131138
{ "targets": [4], "visible": initialwindowwidth > 700},
@@ -134,43 +141,67 @@ <h1 class="post-title">{{ page.title | escape }}</h1>
134141
{ "targets": [7], "visible": initialwindowwidth > 820}
135142
]
136143
});
137-
const requestedOrder = getQueryVariable('order'); // e.g. ?order=1.asc
138-
const requestedCols = getQueryVariable('cols'); // e.g. ?cols=0.1.10.11
139-
const requestedPageSize = getQueryVariable('n'); // e.g. ?n=50
140-
const requestedPage = getQueryVariable('p'); // e.g. ?n=50&p=2
141-
if (requestedCols || requestedOrder || requestedPage || requestedPageSize) {
142-
setTimeout(() => {
143-
let needsDraw = false;
144-
if (requestedOrder) {
145-
const order = requestedOrder.split('.');
146-
if (order.length == 2) {
147-
order[0] = parseInt(order[0]);
148-
mainContentList.order(order);
149-
needsDraw = true;
150-
}
144+
145+
function updateTableToReflectURLParams() {
146+
window.tableDrawLock = true;
147+
const requestedOrder = getQueryVariable('order'); // e.g. ?order=1.asc
148+
const requestedCols = getQueryVariable('cols'); // e.g. ?cols=0.1.10.11
149+
const requestedPageSize = getQueryVariable('n'); // e.g. ?n=50
150+
const requestedPage = getQueryVariable('p'); // e.g. ?n=50&p=2
151+
const requestedSearch = getQueryVariable('q'); // e.g. q=citta
152+
153+
if (requestedOrder !== undefined) {
154+
const order = requestedOrder.split('.');
155+
if (order.length <= 2) {
156+
order[0] = parseInt(order[0]);
157+
mainContentList.order(order);
151158
}
152-
if (requestedCols) {
153-
const cols = requestedCols.split('.');
154-
mainContentList.columns().every((i) => {
155-
mainContentList.columns(i).visible(cols.includes(i.toString()));
156-
});
159+
}
160+
if (requestedSearch !== undefined) {
161+
document.getElementById('dt-search-0').value = requestedSearch;
162+
mainContentList.search(requestedSearch);
163+
}
164+
if (requestedCols !== undefined) {
165+
const cols = requestedCols.split('.');
166+
mainContentList.columns().every((i) => {
167+
mainContentList.columns(i).visible(cols.includes(i.toString()));
168+
});
169+
}
170+
if (requestedPageSize !== undefined) {
171+
const ps = parseInt(requestedPageSize);
172+
if (ps > 0) {
173+
mainContentList.page.len(ps);
157174
}
158-
if (requestedPageSize) {
159-
const ps = parseInt(requestedPageSize);
160-
if (ps > 0) {
161-
mainContentList.page.len(ps);
162-
needsDraw = true;
163-
}
175+
}
176+
mainContentList.draw();
177+
if (requestedPage !== undefined) {
178+
const p = parseInt(requestedPage);
179+
if (p >= 0) {
180+
mainContentList.page(p).draw(false);
164181
}
165-
if (needsDraw) {
166-
mainContentList.draw();
167-
}
168-
if (requestedPage) {
169-
const p = parseInt(requestedPage);
170-
if (p >= 0) {
171-
mainContentList.page(p).draw(false);
172-
}
173-
}
174-
}, 5);
182+
}
183+
setTimeout(() => window.tableDrawLock = undefined, 5);
184+
}
185+
updateTableToReflectURLParams();
186+
addEventListener('popstate', updateTableToReflectURLParams);
187+
188+
function getURLForCurrentTable() {
189+
const url = new URL(location);
190+
url.searchParams.set('n', mainContentList.page.len());
191+
url.searchParams.set('p', mainContentList.page());
192+
url.searchParams.set('order', mainContentList.order().flat().join('.'));
193+
const vises = mainContentList.columns().visible();
194+
url.searchParams.set('cols', vises.reduce((r,c,i)=>c?[...r,i]:r,[]).join('.'));
195+
url.searchParams.set('q', mainContentList.search());
196+
return url;
197+
}
198+
function pushNewTableState() {
199+
if (window.tableDrawLock) return;
200+
history.replaceState({},'',getURLForCurrentTable());
175201
}
202+
setTimeout(() => {
203+
history.replaceState({},'',getURLForCurrentTable());
204+
mainContentList.on('draw', pushNewTableState);
205+
mainContentList.on('column-visibility.dt', pushNewTableState);
206+
}, 200);
176207
</script>

0 commit comments

Comments
 (0)