Persist List State in URL & Preserve Navigation After Edit/Create#772
Open
Alwinator wants to merge 2 commits into
Open
Persist List State in URL & Preserve Navigation After Edit/Create#772Alwinator wants to merge 2 commits into
Alwinator wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes three related UX issues around DataTables list state persistence and post-edit/create navigation:
Column visibility is now persisted in the URL — Hidden columns are saved as a
columnsquery parameter (comma-separated indices of hidden columns). When the page is reloaded or the URL is shared, column visibility state is restored.SearchBuilder filters are now correctly applied on page load — Previously, opening a URL with
searchBuilderquery params would display the filter UI correctly but not actually apply the filter to the data. Now, after DataTables initialization completes,table.searchBuilder.rebuild()is called with the loaded criteria, followed by atable.draw()to trigger the server-side filter.Edit/Create "Save" now returns to the previous list page — When navigating to edit or create from a filtered/paginated list, the current list URL is passed as a
returnToquery parameter. After saving, the user is redirected back to the exact list state (page, filters, search, column visibility) instead of the bare list URL.Changes
JavaScript (
starlette_admin/statics/js/list.js)loadedSearchBuilder,loadedHiddenColumns, andisRestoringmodule-level variables to coordinate deferred state restoration.stateSaveCallback: Serializes hidden column indices into thecolumnsURL param. Suppressed during restoration via theisRestoringflag.stateLoadCallback: Parsescolumnsparam to restore column visibility. Stores loadedsearchBuilderand hidden column indices for deferred application ininitComplete.initComplete: Rebuilds SearchBuilder from URL state, then restores page position and hidden columns in a deferreddrawcallback. AppendsreturnTo(usinglocation.pathname + location.search) to the "New" create button.drawCallback: AppendsreturnTo=<current list path+search>to all.row-actions-container a[href*='/edit/']links.Python (
starlette_admin/base.py)_validate_return_url()method that ensuresreturnTois a relative path starting with the admin'sbase_url(prevents open redirect attacks)._render_edit(): ReadsreturnTofrom query params (GET) or form data (POST). Uses it as the redirect target on "Save"._render_create(): Same behavior as edit.Python (
starlette_admin/contrib/sqla/view.py)searchable_relation_fields: Optional[Dict[str, List[str]]] = Noneclass attribute. This fixes anAttributeError("object has no attribute 'searchable_relation_fields'") caused by the "Add Foreign Key filters" feature accessingself.searchable_relation_fieldsinfind_all/countwithout declaring it on the class.Templates
edit.html/create.html: Added hidden<input name="returnTo">field. Cancel button now usesreturn_urlinstead of hardcoded list URL.list.html: Bumpedlist.jscache version tov=4.Tests (
tests/test_return_url.py)returnToredirects correctly after edit/createreturnTodefaults to list URL_continue_editingignoresreturnToreturnToto template contextSecurity
The
returnToparameter is validated server-side:base_url(e.g./admin/)//(protocol-relative URLs)