#36757 closed New feature (wontfix)
Add CSS class to ignore change event in list view
| Reported by: | Jacob Rief | Owned by: | Ahmed Asar |
|---|---|---|---|
| Component: | contrib.admin | Version: | dev |
| Severity: | Normal | Keywords: | list-view, change-event |
| Cc: | Jacob Rief | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I am the author of the popular 3rd party library https://github.com/jrief/django-admin-sortable2 used in approximately 1.5% of all Django installations. It is used to sort the entries in the list view of the Django-Admin. There however is a problem: Whenever one reorders entries and immediatly afterwards attemps to delete an entry, an alert message pops up, warning the user about unsaved changes.
Please check this issue https://github.com/jrief/django-admin-sortable2/issues/397 for details.
The easiest solution to fix this would be to just ignore that change. By applying this simple patch:
--- a/django/contrib/admin/static/admin/js/actions.js
+++ b/django/contrib/admin/static/admin/js/actions.js
@@ -151,7 +151,7 @@
updateCounter(actionCheckboxes, options);
lastChecked = target;
} else {
- list_editable_changed = true;
+ list_editable_changed = !target.classList.contains('ignore-list-changes');
}
});
});
this issue can be solved without causing any harm to other applications. I then would apply the CSS class ignore-list-changes to the <tbody> before dragging an entry and remove it afterwards. Btw., a few lines above, the CSS class action-select is used to prevent a similar use-case, so this patch follows the same logic.
This patch can also be used by other 3rd party libraries which change fields inside the list view and want to prevent such a warning message.
The class name ignore-list-changes is of course open for debate.
If this patch is accepted, I'll create a pull request.
Change History (6)
comment:1 by , 3 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:2 by , 3 weeks ago
comment:3 by , 3 weeks ago
My question is, if I shall proceed on this, or if it will be closed as unplanned. I can imagine that other 3rd party apps may also profit from this feature.
comment:4 by , 3 weeks ago
| Easy pickings: | unset |
|---|---|
| Resolution: | → wontfix |
| Status: | assigned → closed |
| Summary: | Optionally ignore change event in list view → Add CSS class to ignore change event in list view |
| Type: | Cleanup/optimization → New feature |
Thanks for the ticket and the clear reproduction. As a user of the admin, if I reorder 100 items, I delete one, and that discards my sort order updates to the other 99, I think I'd still want the warning. I'd definitely want the warning if I were about to lose changes to other list_editable fields.
I appreciate you might make a different judgment call for django-admin-sortable2, but I'm unsure about adding workarounds for this in core in the general case. Feel free to seek input django/new-features if you disagree.
comment:5 by , 2 weeks ago
Hi Jacob,
that's not the way how django-admin-sortable2 works. There in the list view, the sorting order is saved immediatly after you drop an item. This can't be implemented otherwise because the list view doesn't have any "Save" button.
So what we currently end with, is a resorted list which confuses Django's JS code, because it assumes that changes have been made to editable fields. But these changes have already been saved to the database.
I wouldn't even consider this a workaround for django-admin-sortable2 but for any 3rd party library wanting to change any value.
My current solution is to monkey [patch django/contrib/admin/static/admin/js/actions.js](https://github.com/jrief/django-admin-sortable2/blob/master/patches/actions-django-5.2.patch) which is error prone and hard to maintain.
comment:6 by , 2 weeks ago
This can't be implemented otherwise because the list view doesn't have any "Save" button.
There is a save button if you have list_editable enabled. I installed django-admin-sortable2 (I understand your point that other packages would benefit, too), I added fields to list_editable including the sort order field and other text fields, and I noticed with your patch my changes to the text fields were discarded without a warning.
Feel free to take this to a wider audience if you disagree.
Hi Jacob,
Sorry for the confusion — I think the ticket was assigned to me by mistake during triage. I didn’t mean to take it over without checking with you first, especially since you’re the one who proposed the solution and offered to submit a PR.