Opened 14 years ago

Closed 9 years ago

#15760 closed New feature (fixed)

Feature: JS Hooks for Dynamic Inlines

Reported by: Mark Lavin Owned by: Tim Graham <timograham@…>
Component: contrib.admin Version: dev
Severity: Normal Keywords: inlines jquery callback
Cc: dev@…, Jaap Roes, cmawebsite@…, Giorgos Stratakis, Ramez Issac Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
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 Mark Lavin 14 years ago.
Admin inlines with events

Download all attachments as: .zip

Change History (28)

by Mark Lavin, 14 years ago

Attachment: inlines.diff added

Admin inlines with events

comment:1 by Mark Lavin, 14 years ago

Needs documentation: set

comment:2 by Julien Phalip, 14 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Brillgen Developers, 14 years ago

Cc: dev@… added
Easy pickings: unset

comment:4 by Julien Phalip, 13 years ago

UI/UX: set

comment:5 by Jaap Roes, 13 years ago

Cc: Jaap Roes added

comment:6 by anonymous, 13 years ago

Keywords: callback added
Owner: changed from nobody to Mark Lavin

comment:7 by Mark Lavin, 13 years ago

Owner: Mark Lavin removed

comment:8 by Collin Anderson, 13 years ago

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 13 years ago by Collin Anderson (next)

comment:9 by Mark Lavin, 13 years ago

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 by Collin Anderson, 13 years ago

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

comment:11 by Michał Pasternak, 12 years ago

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 by Per Rosengren, 12 years ago

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 by Julien Phalip, 12 years ago

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 by Giorgos Stratakis, 11 years ago

Cc: Giorgos Stratakis added

comment:15 by Ramez Issac, 9 years ago

How About to extend the current plugin defaults to accept say 'addedCallback' and 'deletedCalback' which can be assigned in to the django.jQuery.fn.formset.defaults . Those options are left for the user to fill.
The Tabular & Stacked inline js can callback those function along with their 'added' and 'deleted' function

And as answer to julien question about what if many inline existed on a page , we can send both the created form and the formset name.

For using this new feature, a change_form template is extended for the model you want to work with, and we add the javascript function of desire inside the HTML template. Mainly in the change_form_document_ready block or maybe a new block (where missing block.super is not gonna mess things up)

I can do this if idea is accepted.

Last edited 9 years ago by Ramez Issac (previous) (diff)

comment:16 by Ramez Issac, 9 years ago

Adding a formset defaults in the document_ready block is too late for the defaults to get picked up.
The plugin is Initiated by the edit_inline/tabular.html (or stacked) , at the end of inline_field_sets block

The other option i see is on the extrahead block

comment:17 by Ramez Issac, 9 years ago

Cc: Ramez Issac added

PR created 4883

comment:18 by Ramez Issac, 9 years ago

Needs documentation: set

Any Idea on where this feature documentation should go in the Django admin documentation.

Thanks.

Last edited 9 years ago by Ramez Issac (previous) (diff)

comment:19 by Ramez Issac, 9 years ago

Patch needs improvement: unset

comment:20 by Tim Graham, 9 years ago

Maybe a new page at docs/ref/contrib/admin/javascript.txt would make sense. Anyway, the content is more important than the location at this point.

comment:21 by Ramez Issac, 9 years ago

Indeed , thank you Tim.
I added an 'initial documentation' , there is a small problem in docs/ref/contrib/admin/javascript line 19 , I couldn't figure out how to make a proper link. :-)
Waiting for feedback regarding this and any other.

Best Regards.

Last edited 9 years ago by Ramez Issac (previous) (diff)

comment:22 by Ramez Issac, 9 years ago

Needs documentation: unset

comment:23 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:24 by Ramez Issac, 9 years ago

Patch needs improvement: unset

Added JavaScript tests along with documentation enhancement.

comment:25 by Tim Graham, 9 years ago

Patch needs improvement: set

Reviewed the updated patch.

comment:26 by Ramez Issac, 9 years ago

Patch needs improvement: unset

Improvements made. Kindly review. Thanks.

comment:27 by Tim Graham <timograham@…>, 9 years ago

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In 1335aa2:

Fixed #15760 -- Added JavaScript events for admin inline forms.

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