Opened 5 years ago
Last modified 4 hours ago
#31170 new Bug
dismissRelatedLookupPopup doesn't trigger change event
Reported by: | J. David Ibáñez | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 3.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
I've a custom form with a field using ForeignKeyRawIdWidget (field1), and a select box (field2) dependent on field1.
I've some JS listening for change events in field1, when this happens field2 is updated.
The problem is, when field1 is chosen using the popup the change event is not triggered, so field2 is not updated.
This is easy to fix:
--- a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js +++ b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js @@ -49,6 +49,9 @@ } else { document.getElementById(name).value = chosenId; } + var evt = document.createEvent("HTMLEvents"); + evt.initEvent("change", false, true); + elem.dispatchEvent(evt); win.close(); }
Change History (5)
comment:1 by , 5 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:2 by , 21 hours ago
Example https://github.com/Urth/django-ticket-31170
Create a super user, runserver and navigate to http://127.0.0.1:8000/ticket_31170/article/add/
- Select a user with the RelatedLookupPopup looking glass button.
- Observe that nothing happens
- Change the user id yourself
- Observe the javascript alert
The desired behavior is implemented in dismissAddRelatedObjectPopup
which uses the same approach as the patch below.
diff --git a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js index 74d17bfc3e..16699da785 100644 --- a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js +++ b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js @@ -55,8 +55,10 @@ if (elem.classList.contains('vManyToManyRawIdAdminField') && elem.value) { elem.value += ',' + chosenId; } else { - document.getElementById(name).value = chosenId; + elem.value = chosenId; } + // Trigger a change event to update related fields if required. + $(elem).trigger('change'); const index = relatedWindows.indexOf(win); if (index > -1) { relatedWindows.splice(index, 1);
comment:3 by , 21 hours ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
Version: | 3.0 → dev |
Can you put a minimal project/page together showing this, and why you can't attach your own listener and such?
(It's a bit difficult to see exactly what's going on just from that diff...)
Thanks.
(I'm going to close for now as needsinfo. Please re-open when you have an example. Thanks!)