Code

Opened 3 years ago

Last modified 9 months ago

#15760 new New feature

Feature: JS Hooks for Dynamic Inlines

Reported by: mlavin Owned by:
Component: contrib.admin Version: master
Severity: Normal Keywords: inlines jquery callback
Cc: dev@…, jaap3, cmawebsite@…, gstrat Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: yes

Description

It is difficult to access the newly added row when working with dynamic inlines. This makes it difficult to use widgets which require javascript bindings (such as auto-complete widgets) in the inlines. I believe this is the issue that someone was trying to raise in #15693. While the formset plugin does allow for passing method to be called when a new row is added, this is always already populated by admin/edit_inline/stacked.html and admin/edit_inline/tabular.html. If you want to include another method to be run you need to override these templates completely.

I've attached a patch which adds two events to the formset plugin: formsetadd and formsetdelete which are fired by the add and delete rows respectively. The example usage would be

django.jQuery('.add-row a').live('formsetadd', function(e, row) {
    console.log('Formset add!');
    console.log($(row));
});

Attachments (1)

inlines.diff (639 bytes) - added by mlavin 3 years ago.
Admin inlines with events

Download all attachments as: .zip

Change History (15)

Changed 3 years ago by mlavin

Admin inlines with events

comment:1 Changed 3 years ago by mlavin

  • Needs documentation set
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by julien

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 3 years ago by brillgen

  • Cc dev@… added
  • Easy pickings unset

comment:4 Changed 3 years ago by julien

  • UI/UX set

comment:5 Changed 3 years ago by jaap3

  • Cc jaap3 added

comment:6 Changed 2 years ago by anonymous

  • Keywords callback added
  • Owner changed from nobody to mlavin

comment:7 Changed 2 years ago by mlavin

  • Owner mlavin deleted

comment:8 Changed 2 years ago by CollinAnderson

  • Cc cmawebsite@… added

I ran into this issue two years ago, and I am running into it again today. I ended up having to clone django's add link, hide the original, add my own click handler, then call django's click handler on the original element.

It looks like a few others have ran into this issue too.

I believe the posted patch would solve the problem for me, maybe put a django namespace prefix on it just to be save.

Version 0, edited 2 years ago by CollinAnderson (next)

comment:9 Changed 2 years ago by mlavin

I ended up working around this issue in my own project by monkey patching django.jQuery.fn.formset. I still think that adding these events would be helpful but I think that https://github.com/django/django/pull/24 largely reduces the need for them if it is accepted.

comment:10 Changed 2 years ago by CollinAnderson

Ahh, yes, #18241 would also solve this for me.

comment:11 Changed 2 years ago by mpasternak

I like the inlines.diff patch, because it is more explicit this way. I don't think there's anything wrong with having clearly named events on the javascript side, is there?

comment:12 Changed 20 months ago by per856

  • Needs documentation unset

Updated this patch to current master, with some alterations. Added documentation.

Patch: https://github.com/django/django/pull/542

This fix also fixes accepted duplicate #19314

Example usage:

django.jQuery(document).bind('formset_add.admin.my_widget', function(event, row_element) {
    my_widget_setup($('.my_widget', row_element));
});

comment:13 Changed 20 months ago by julien

  • Patch needs improvement set

Thanks for your work on the patch.

In the example usage above, you bind the event 'formset_add.admin.my_widget'. Is that a typo (i.e. the 'my_widget' part)?

Also, what would happen when the page would contain multiple types of inlines? How would you discriminate between the various inlines? If you could please elaborate on the approach a developer would have to follow, that would be helpful — and in fact, this could also be added to the documentation.

Finally, to include this patch in Django core, some tests would have to be written. Would you like to have a go at writing some Selenium tests demonstrating the use of this new feature?

Thanks!

comment:14 Changed 9 months ago by gstrat

  • Cc gstrat added

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from (none) to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.