Collapsed stacked inlines

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* collapsed_stacked_inlines.js */
/* Created in May 2009 by Hannes Rydén */
/* Use, distribute and modify freely */

jQuery(function($) {
    // Only for stacked inlines
    $('div.inline-group div.inline-related:not(.tabular)').each(function() {
        fs = $(this).find('fieldset')
        h3 = $(this).find('h3:first')

        // Don't collapse if fieldset contains errors
        if (fs.find('div').hasClass('errors'))
            fs.addClass('stacked_collapse');
        else
            fs.addClass('stacked_collapse collapsed');
        
        // Add toggle link
        h3.prepend('<a class="stacked_collapse-toggle" href="#">(' + gettext('Show') + ')</a> ');
        h3.find('a.stacked_collapse-toggle').bind("click", function(){
            fs = $(this).parent('h3').next('fieldset');
            if (!fs.hasClass('collapsed'))
            {
                fs.addClass('collapsed');
                $(this).html('(' + gettext('Show') + ')');
            }
            else
            {
                fs.removeClass('collapsed');
                $(this).html('(' + gettext('Hide') + ')');
            }
        }).removeAttr('href').css('cursor', 'pointer');
    });
});

More like this

  1. Dynamic tabular inlines with optional drag-n-drop sorting by Aneon 4 years, 11 months ago
  2. Dynamically add inlines by MasonM 4 years, 10 months ago
  3. Javascript constraints in admin app and fieldsets to tabs (jquery) by jpic 5 years, 1 month ago
  4. GeoDjango maps in admin TabularInlines by alanB 3 years, 6 months ago
  5. Initially open collapsable fieldset class in admin by ralfzen 3 years, 7 months ago

Comments

gmandx (on March 3, 2010):

Now I'm using Django 1.2 beta 1 SVN-12660, and the admin interface now includes a button to dynamically add new inlines. So I exclude the empty inline that Django has hidden, with this:

$('div.inline-group div.inline-related:not(.tabular):not(.empty-form)')...

Adding ":not(.empty-form)')" to the selector excludes all dynamic inlines, and they always appear expanded (I like that way, so the user knows what just created)

#

xenolog (on June 6, 2010):

For use with Django 1.2 we need change:

jQuery(function($) {

to

(function($) {
    $(document).ready(function() {

and last

});

to

    });
})(django.jQuery);

In this case call jQuery-XXXXX.js from Media from admin.ModelAdmin not nessesary.

#

shacker (on July 30, 2010):

Thanks Aneon and xenolog - works perfectly.

#

(Forgotten your password?)