Opened 5 years ago

Closed 7 weeks ago

Last modified 7 days ago

#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 (16)

comment:1 by Carlton Gibson, 5 years ago

Resolution: needsinfo
Status: newclosed

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!)

comment:2 by Harm Geerts, 4 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 Harm Geerts, 4 months ago

Resolution: needsinfo
Status: closednew
Version: 3.0dev

comment:4 by Harm Geerts, 4 months ago

Version: dev3.0

The issue was observed in 3.0, but I tested on dev.

comment:5 by Sarah Boyce, 4 months ago

Triage Stage: UnreviewedAccepted

Thank you for the test project Harm!

comment:6 by Kairat Makym, 4 months ago

Owner: changed from nobody to Kairat Makym
Status: newassigned

comment:7 by Kairat Makym, 4 months ago

Has patch: set
Triage Stage: AcceptedUnreviewed

comment:8 by Kairat Makym, 3 months ago

Triage Stage: UnreviewedReady for checkin

comment:9 by Jacob Walls, 3 months ago

Triage Stage: Ready for checkinAccepted

comment:10 by Sarah Boyce, 7 weeks ago

Triage Stage: AcceptedReady for checkin

comment:11 by Sarah Boyce <42296566+sarahboyce@…>, 7 weeks ago

In 91bebf1a:

Refs #31170 -- Added JavaScript tests for RelatedObjectLookups.js.

comment:12 by Sarah Boyce <42296566+sarahboyce@…>, 7 weeks ago

Resolution: fixed
Status: assignedclosed

In 51398f8b:

Fixed #31170 -- Added change event trigger to dismissRelatedLookupPopup.

comment:13 by Sarah Boyce <42296566+sarahboyce@…>, 7 weeks ago

In 7cf6a34:

[5.2.x] Refs #31170 -- Added JavaScript tests for RelatedObjectLookups.js.

Backport of 91bebf1adb43561b54bac18e76224759dc70acb3 from main.

comment:14 by Sarah Boyce <42296566+sarahboyce@…>, 7 weeks ago

In b0d497a:

[5.2.x] Fixed #31170 -- Added change event trigger to dismissRelatedLookupPopup.

Backport of 51398f8bd568a6324a8cafe20c068d0974913ad5 from main.

comment:15 by GitHub <noreply@…>, 7 days ago

In a245604:

Fixed #36284, Refs #31170 -- Ensured related lookup popups are closed properly.

In the admin, when selecting related objects via the helpers defined in
RelatedObjectLookups.js, the dismissRelatedLookupPopup function was
attempting to access window.relatedWindows, which does not exist in
real execution, causing related lookup popups to remain open.

This change ensures that this code correctly accesses the module-local
relatedWindows by explicitly assigning it to window.relatedWindows.

Regression in 91bebf1adb43561b54bac18e76224759dc70acb3.

Thanks Matthias Kestenholz for the report, the fix ideas, and testing.

Co-authored-by: Matthias Kestenholz <mk@…>

comment:16 by Natalia <124304+nessita@…>, 7 days ago

In 614be949:

[5.2.x] Fixed #36284, Refs #31170 -- Ensured related lookup popups are closed properly.

In the admin, when selecting related objects via the helpers defined in
RelatedObjectLookups.js, the dismissRelatedLookupPopup function was
attempting to access window.relatedWindows, which does not exist in
real execution, causing related lookup popups to remain open.

This change ensures that this code correctly accesses the module-local
relatedWindows by explicitly assigning it to window.relatedWindows.

Regression in 91bebf1adb43561b54bac18e76224759dc70acb3.

Thanks Matthias Kestenholz for the report, the fix ideas, and testing.

Co-authored-by: Matthias Kestenholz <mk@…>

Backport of a245604277eb9edeba234dacf199890766462709 from main.

Note: See TracTickets for help on using tickets.
Back to Top