Skip to content

Commit 9ca5483

Browse files
authored
save state in URL params (#700)
* save state in URL params * save state in URL params
1 parent 51a7b1c commit 9ca5483

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

starlette_admin/statics/js/list.js

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ $(function () {
66
var selectedRows = [];
77

88
/*
9-
contains all fields including nested fields inside all CollectionField.
10-
Each nested field name is prefixed by it parent CollectionField name (ex: 'category.name')
11-
*/
9+
contains all fields including nested fields inside all CollectionField.
10+
Each nested field name is prefixed by it parent CollectionField name (ex: 'category.name')
11+
*/
1212
var dt_fields = [];
1313

1414
/* datatables columns generated from model.fields */
@@ -211,9 +211,9 @@ $(function () {
211211
// End Buttons declarations
212212

213213
/*
214-
Convert datatables searchBuilder conditions into custom dict
215-
with custom operators before send it to the backend.
216-
*/
214+
Convert datatables searchBuilder conditions into custom dict
215+
with custom operators before send it to the backend.
216+
*/
217217
function extractCriteria(c) {
218218
var d = {};
219219
if ((c.logic && c.logic == "OR") || c.logic == "AND") {
@@ -277,6 +277,7 @@ $(function () {
277277
}
278278
return d;
279279
}
280+
280281
// End Search builder
281282

282283
// Datatable instance
@@ -412,6 +413,72 @@ $(function () {
412413
drawCallback: function (settings) {
413414
actionManager.initNoConfirmationActions();
414415
},
416+
417+
stateSaveCallback: function (settings, data) {
418+
const params = {
419+
page: data?.page + 1,
420+
page_size: data?.length,
421+
search: data?.search?.search,
422+
order: data?.order
423+
? data.order.map(
424+
([col, dir]) =>
425+
`${dir === "asc" ? "" : "-"}${dt_columns[col - 2].name}`
426+
)
427+
: undefined,
428+
searchBuilder:
429+
data?.searchBuilder && !$.isEmptyObject(data?.searchBuilder)
430+
? JSON.stringify(data?.searchBuilder)
431+
: undefined,
432+
};
433+
434+
const query = Qs.stringify(params, { encode: false, indices: false });
435+
436+
history.replaceState(
437+
null,
438+
"",
439+
location.pathname + (query ? "?" + query : "")
440+
);
441+
},
442+
443+
stateLoadCallback: function (settings, callback) {
444+
const params = Qs.parse(location.search, { ignoreQueryPrefix: true });
445+
if (!Object.keys(params).length) return null;
446+
447+
let length = isNaN(parseInt(params?.page_size))
448+
? model.pageSize
449+
: parseInt(params?.page_size);
450+
let page = isNaN(parseInt(params?.page)) ? 0 : parseInt(params?.page) - 1;
451+
let order = [];
452+
if (params?.order) {
453+
if (typeof params.order === "string") params.order = [params.order];
454+
params.order.forEach((o) => {
455+
const isDesc = o.startsWith("-");
456+
const colName =
457+
o.startsWith("-") || o.startsWith("+") ? o.substring(1) : o;
458+
let idx = dt_columns.findIndex((it) => colName === it.name);
459+
if (idx > -1) order.push([idx + 2, isDesc ? "desc" : "asc"]);
460+
});
461+
}
462+
463+
var state = {
464+
time: Date.now(),
465+
length: length,
466+
order: order.length > 0 ? order : undefined,
467+
search: {
468+
search: params?.search ?? undefined,
469+
smart: true,
470+
regex: false,
471+
caseInsensitive: true,
472+
},
473+
searchBuilder: params?.searchBuilder
474+
? JSON.parse(params?.searchBuilder)
475+
: undefined,
476+
page: page,
477+
start: length * page,
478+
};
479+
if (params?.search) $("#searchInput").val(params.search);
480+
callback(state);
481+
},
415482
...model.datatablesOptions,
416483
});
417484

starlette_admin/statics/js/vendor/qs.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

starlette_admin/templates/list.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ <h1>{{ model.label }}</h1>
140140
src="{{ url_for(__name__ ~ ':statics', path='js/vendor/dt.checkboxes.js') }}"></script>
141141
<script type="text/javascript"
142142
src="{{ url_for(__name__ ~ ':statics', path='js/vendor/dt.searchHighlight.js') }}"></script>
143+
<script type="text/javascript"
144+
src="{{ url_for(__name__ ~ ':statics', path='js/vendor/qs.min.js') }}"></script>
143145
<script type="text/javascript"
144146
src="{{ url_for(__name__ ~ ':statics', path='js/utils.js') }}"></script>
145147
<script>var model = {{__js_model__ | tojson | safe}};</script>

0 commit comments

Comments
 (0)