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: | |
---|---|---|---|
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)
Change History (28)
by , 14 years ago
Attachment: | inlines.diff added |
---|
comment:1 by , 14 years ago
Needs documentation: | set |
---|
comment:2 by , 14 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 14 years ago
Cc: | added |
---|---|
Easy pickings: | unset |
comment:4 by , 14 years ago
UI/UX: | set |
---|
comment:5 by , 13 years ago
Cc: | added |
---|
comment:6 by , 13 years ago
Keywords: | callback added |
---|---|
Owner: | changed from | to
comment:7 by , 13 years ago
Owner: | removed |
---|
comment:8 by , 13 years ago
Cc: | 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.
- http://code.google.com/p/django-tinymce/issues/detail?id=89
- http://stackoverflow.com/questions/7133189/django-stackedinline-tinymce-problem
I believe the posted patch would solve the problem for me, maybe put a django namespace prefix on it just to be safe.
Another option would be to globally add a callback or override options.added and options.removed.
comment:9 by , 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:11 by , 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 , 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 , 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 , 11 years ago
Cc: | added |
---|
comment:15 by , 10 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.
comment:16 by , 10 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:18 by , 9 years ago
Needs documentation: | set |
---|
Any Idea on where this feature documentation should go in the Django admin documentation.
Thanks.
comment:19 by , 9 years ago
Patch needs improvement: | unset |
---|
comment:20 by , 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 , 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.
comment:22 by , 9 years ago
Needs documentation: | unset |
---|
comment:23 by , 9 years ago
Patch needs improvement: | set |
---|
comment:24 by , 9 years ago
Patch needs improvement: | unset |
---|
Added JavaScript tests along with documentation enhancement.
comment:26 by , 9 years ago
Patch needs improvement: | unset |
---|
Improvements made. Kindly review. Thanks.
Admin inlines with events