Login

Initially open collapsable fieldset class in admin

Author:
ralfzen
Posted:
September 17, 2010
Language:
JavaScript
Version:
1.2
Tags:
fieldset collapse collapsed open show
Score:
1 (after 1 ratings)

It's just an extension for the admin. Replace the collapse.js (collapse.min.js) and use it like this in the admin fieldset option: ´´'classes': ['collapse', 'open']´´. Without the 'open'-class, it will work as usual. Needs possibly fixing for IE <= 8 because IE doesn't support the ´´:not´´ selector.

 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
34
35
36
37
38
39
40
41
42
43
44
45
46
(function($) {
	$(document).ready(function() {
		// Add anchor tag for Show/Hide link
		$("fieldset.collapse").each(function(i, elem) {
			// Don't hide if fields in this fieldset have errors
			if ( $(elem).find("div.errors").length == 0 ) {
				// if extra-class open do not collapse initially
				if ( !$(elem).hasClass('open') ) {
		        	$(elem).addClass("collapsed");
	  				$(elem).find("h2").first().append(' (<a id="fieldsetcollapser' +
	  					i +'" class="collapse-toggle" href="#">' + gettext("Show") +
	  					'</a>)');
		        } else {
		        	$(elem).find("h2").first().append(' (<a id="fieldsetcollapser' +
		        		i +'" class="collapse-toggle" href="#">' + gettext("Hide") +
		  				'</a>)');
		        }
			}
		});
		
		hide = function() {
			$(this).text(gettext("Show"));
			$(this).closest("fieldset").addClass("collapsed");
			return false;
		};
		
		show = function() {
			$(this).text(gettext("Hide"));
			$(this).closest("fieldset").removeClass("collapsed");
			return false;
		};
		
		// Add close-toggle to anchor tag
		$("fieldset.collapse:not(.open) a.collapse-toggle").toggle(
			show,
			hide
		);
		
		// Add open-toggle to anchor tag
		$("fieldset.collapse.open a.collapse-toggle").toggle(
			hide,
			show
		);
	
	});
})(django.jQuery);

More like this

  1. Collapsed stacked inlines by Aneon 6 years, 2 months ago
  2. FieldsetForm by Ciantic 8 years, 2 months ago
  3. DRY Fieldsets by DrMeers 6 years ago
  4. Forms splitted in fieldsets by gustavo80br 7 years, 3 months ago
  5. Remove named field from fieldsets by dokterbob 5 years, 7 months ago

Comments

Please login first before commenting.