Skip to content

Instantly share code, notes, and snippets.

@deniskrumko
Created March 29, 2018 11:40
Show Gist options
  • Save deniskrumko/7308061b73d73c290975a152c7d91853 to your computer and use it in GitHub Desktop.
Save deniskrumko/7308061b73d73c290975a152c7d91853 to your computer and use it in GitHub Desktop.
Django Suit collapsed (folded) inlines
// Attach this script to your "ModelAdmin" class by "Media"
jQuery(function($) {
$(function () {
Suit.after_inline.register('my_unique_func_name', function(inline_prefix, row){
var $group = $(row).parent();
if ($group.hasClass('collapsed')) {
$group.removeClass('collapsed');
$group.find('.inline-related:not(.empty-form)').each(function (index, element) {
$(element).show();
});
$group.find('.stacked_collapse-toggle').html('(' + gettext('Hide') + ')');
}
});
});
$('div.inline-group').each(function() {
var h2 = $(this).find('h2:first');
if (!$(this).find('div').hasClass('errors')) {
$(this).find('.inline-related:not(.empty-form)').each(function (index, element) {
$(element).hide();
});
$(this).addClass('collapsed');
}
if ($(this).hasClass('collapsed')) {
h2.append(' <a class="stacked_collapse-toggle" href="#">(' + gettext('Show') + ')</a> ');
} else {
h2.append(' <a class="stacked_collapse-toggle" href="#">(' + gettext('Hide') + ')</a> ');
}
h2.find('a.stacked_collapse-toggle').bind("click", function(){
var $group = $(this).parent().parent();
if ($group.hasClass('collapsed')) {
$group.removeClass('collapsed');
$group.find('.inline-related:not(.empty-form)').each(function (index, element) {
$(element).show();
});
$(this).html('(' + gettext('Hide') + ')');
} else {
$group.addClass('collapsed');
$group.find('.inline-related:not(.empty-form)').each(function (index, element) {
$(element).hide();
});
$(this).html('(' + gettext('Show') + ')');
}
}).removeAttr('href').css('cursor', 'pointer');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment