#31170 closed Bug (fixed)
dismissRelatedLookupPopup doesn't trigger change event
Reported by: | J. David Ibáñez | Owned by: | Kairat Makym |
---|---|---|---|
Component: | contrib.admin | Version: | 3.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | 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 (14)
comment:1 by , 5 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:2 by , 2 months 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 , 2 months ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
Version: | 3.0 → dev |
comment:5 by , 2 months ago
Triage Stage: | Unreviewed → Accepted |
---|
Thank you for the test project Harm!
comment:6 by , 2 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:7 by , 2 months ago
Has patch: | set |
---|---|
Triage Stage: | Accepted → Unreviewed |
comment:8 by , 2 months ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
comment:9 by , 2 months ago
Triage Stage: | Ready for checkin → Accepted |
---|
comment:10 by , 5 days ago
Triage Stage: | Accepted → Ready for checkin |
---|
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!)