Saturday, September 30, 2017

Re: Django admin: is there an event, or another way to call a Javascript function, when a Django Admin Popup (the green plus icon) completes ?

Opened a new thread on django-developers: https://groups.google.com/d/msg/django-developers/BsudgRRCKb4/airBUyhaAwAJ



On Saturday, September 30, 2017 at 6:32:51 PM UTC+2, Federico Capoano wrote:
Hi James,

I was banging my head on the wall for this issue, you saved me some precious time, thank you!

I wonder why such a mechanism is not included by default into Django, I will bring up this issue on the development mailing list.

Federico



On Thursday, November 26, 2015 at 11:58:22 AM UTC+1, James Murty wrote:
Hi,

Below is a Javascript file with the hackery we use to trigger a change event when the add/change popup window is dismissed, which works for Django 1.7 at least.

Hopefully it will help, or at least point you in the right direction.

Regards,
James

/*
 * Trigger change events when Django admin's popup window is dismissed
 */
(function($) {
    $(document).ready(function() {

        // HACK to override `dismissRelatedLookupPopup()` and
        // `dismissAddAnotherPopup()` in Django's RelatedObjectLookups.js to
        // trigger change event when an ID is selected or added via popup.
        function triggerChangeOnField(win, chosenId) {
            var name = windowname_to_id(win.name);
            var elem = document.getElementById(name);
            $(elem).change();
        }
        window.ORIGINAL_dismissRelatedLookupPopup = window.dismissRelatedLookupPopup
        window.dismissRelatedLookupPopup = function(win, chosenId) {
            ORIGINAL_dismissRelatedLookupPopup(win, chosenId);
            triggerChangeOnField(win, chosenId);
        }
        window.ORIGINAL_dismissAddAnotherPopup = window.dismissAddAnotherPopup
        window.dismissAddAnotherPopup = function(win, chosenId) {
            ORIGINAL_dismissAddAnotherPopup(win, chosenId);
            triggerChangeOnField(win, chosenId);
        }

    });
})(jQuery);



On Wednesday, 25 November 2015 21:39:06 UTC+11, boito...@gmail.com wrote:
Hi,

  I see this question is not receiving a lot of love. Perhaps we can try to approach it differently then:
 What is the mechanism that populates back the parentform select once the pop-up form is completed and saved ? (As I have very limited experience with front-end web technologies, I would not know where to look for the code implementing that functionnality).

From here, perhaps we can try to hook some custom Javascript.

Thank you for reading,
  Ad

Le vendredi 20 novembre 2015 09:55:10 UTC+1, boito...@gmail.com a écrit :
Hi,

  We are developing an application that relies on dynamic admin pages (based on some custom ajax). The high level idea is that, when the user changes the value in a select html element that is mapped to a ForeignKey on another model, we call a JS function (which triggers an asynchronous request that changes the current admin form on completion).


To achieve that, we listen to the change event of said select (to call the JS function when it is fired), which works fine when the user clicks a value directly in the drop-down menu proposed by the select.

Sadly, when this select is populated through the Admin Popup functionality (the process that starts by clicking the green plus icon to open a popup window, and that completes when the user click the save button on the popup window), it seems that the change event is not fired for the select, as our callback is not executed, even though the selected value is actually changed.

Is there another event we can listen to to get the same behaviour than when the user clicks a value directly from the list ? Or any other method to call our JS function when the select value is set through the popup completion ?

Thank you for reading,
  Ad

(Full disclosure: I first tried my luck with a question on SO, without great success)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6172c177-174d-46f5-9018-e016ff58d092%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment