Subversion Repositories Applications.referentiel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
58 jpm 1
/*
2
 * File:        chromatable.js
3
 * Version:     1.3.0
4
 * CVS:         $Id$
5
 * Description: Make a "sticky" header at the top of the table, so it stays put while the table scrolls
6
 * Author:      Zachary Siswick
7
 * Created:     Thursday 19 November 2009 8:53pm
8
 * Language:    Javascript
9
 *
10
 */
11
(function($){
12
 
13
    $.chromatable = {
14
        // Default options
15
        defaults: {
16
						//specify a pixel dimension, auto, or 100%
17
            	width: "900px",
18
				height: "300px",
19
				scrolling: "yes"
20
        }
21
 
22
		};
23
 
24
		$.fn.chromatable = function(options){
25
 
26
		// Extend default options
27
		var options = $.extend({}, $.chromatable.defaults, options);
28
 
29
		return this.each(function(){
30
 
31
				// Add jQuery methods to the element
32
				var $this = $(this);
33
				var $uniqueID = $(this).attr("ID") + ("wrapper");
34
 
35
 
36
				//Add dimentsions from user or default parameters to the DOM elements
37
				$(this).css('width', options.width).addClass("_scrolling");
38
 
39
				$(this).wrap('<div class="scrolling_outer"><div id="'+$uniqueID+'" class="scrolling_inner"></div></div>');
40
 
41
				$(".scrolling_outer").css({'position':'relative'});
42
				$("#"+$uniqueID).css({'overflow-x':'hidden', 'overflow-y':'auto','padding-right':'17px'});
43
				$("#"+$uniqueID).css('height', options.height);
44
				$("#"+$uniqueID).css('width', options.width);
45
 
46
				// clone an exact copy of the scrolling table and add to DOM before the original table
47
				// replace old class with new to differentiate between the two
48
				$(this).before($(this).clone().attr("id", "").addClass("_thead").css(
49
 
50
						{'width' : 'auto',
51
						 'display' : 'block',
52
						 'position':'absolute',
53
						 'border':'none',
54
						 'border-bottom':'1px solid #CCC',
55
						 'top':'0'
56
							}));
57
 
58
 
59
				// remove all children within the cloned table after the thead element
60
				$('._thead').children('tbody').remove();
61
 
62
 
63
				$(this).each(function( $this ){
64
 
65
					// if the width is auto, we need to remove padding-right on scrolling container
66
 
67
					if (options.width == "100%" || options.width == "auto") {
68
 
69
						$("#"+$uniqueID).css({'padding-right':'0px'});
70
					}
71
 
72
 
73
					if (options.scrolling == "no") {
74
 
75
						$("#"+$uniqueID).before('<a href="#" class="expander" style="width:100%;">Expand table</a>');
76
 
77
						$("#"+$uniqueID).css({'padding-right':'0px'});
78
 
79
						$(".expander").each(
80
 
81
 
82
							function(int){
83
 
84
								$(this).attr("ID", int);
85
 
86
								$( this ).bind ("click",function(){
87
 
88
										$("#"+$uniqueID).css({'height':'auto'});
89
 
90
										$("#"+$uniqueID+" ._thead").remove();
91
 
92
										$(this).remove();
93
 
94
									});
95
								});
96
 
97
 
98
						//this is dependant on the jQuery resizable UI plugin
99
						$("#"+$uniqueID).resizable({ handles: 's' }).css("overflow-y", "hidden");
100
 
101
					}
102
 
103
				});
104
 
105
 
106
				// Get a relative reference to the "sticky header"
107
				$curr = $this.prev();
108
 
109
				// Copy the cell widths across from the original table
110
				$("thead:eq(0)>tr th",this).each( function (i) {
111
 
112
					$("thead:eq(0)>tr th:eq("+i+")", $curr).width( $(this).width());
113
 
114
				});
115
 
116
 
117
				//check to see if the width is set to auto, if not, we don't need to call the resizer function
118
				if (options.width == "100%" || "auto"){
119
 
120
 
121
						// call the resizer function whenever the table width has been adjusted
122
						$(window).resize(function(){
123
 
124
									resizer($this);
125
						});
126
					}
127
				});
128
 
129
    };
130
 
131
		// private function to temporarily hide the header when the browser is resized
132
 
133
		function resizer($this) {
134
 
135
				// Need a relative reference to the "sticky header"
136
				$curr = $this.prev();
137
 
138
				$("thead:eq(0)>tr th", $this).each( function (i) {
139
 
140
					$("thead:eq(0)>tr th:eq("+i+")", $curr).width( $(this).width());
141
 
142
				});
143
 
144
  	};
145
 
146
})(jQuery);