Resources/public/js/jquery.columnmanager.js
changeset 54 ec51b9f4695f
parent 53 22377c9e2eae
child 55 80a400379dd3
equal deleted inserted replaced
53:22377c9e2eae 54:ec51b9f4695f
     1 /*
       
     2  * jQuery columnManager plugin
       
     3  * Version: 0.2.5
       
     4  *
       
     5  * Copyright (c) 2007 Roman Weich
       
     6  * http://p.sohei.org
       
     7  *
       
     8  * Dual licensed under the MIT and GPL licenses 
       
     9  * (This means that you can choose the license that best suits your project, and use it accordingly):
       
    10  *   http://www.opensource.org/licenses/mit-license.php
       
    11  *   http://www.gnu.org/licenses/gpl.html
       
    12  *
       
    13  * Changelog: 
       
    14  * v 0.2.5 - 2008-01-17
       
    15  *	-change: added options "show" and "hide". with these functions the user can control the way to show or hide the cells
       
    16  *	-change: added $.fn.showColumns() and $.fn.hideColumns which allows to explicitely show or hide any given number of columns
       
    17  * v 0.2.4 - 2007-12-02
       
    18  *	-fix: a problem with the on/off css classes when manually toggling columns which were not in the column header list
       
    19  *	-fix: an error in the createColumnHeaderList function incorectly resetting the visibility state of the columns
       
    20  *	-change: restructured some of the code
       
    21  * v 0.2.3 - 2007-12-02
       
    22  *	-change: when a column header has no text but some html markup as content, the markup is used in the column header list instead of "undefined"
       
    23  * v 0.2.2 - 2007-11-27
       
    24  *	-change: added the ablity to change the on and off CSS classes in the column header list through $().toggleColumns()
       
    25  *	-change: to avoid conflicts with other plugins, the table-referencing data in the column header list is now stored as an expando and not in the class name as before
       
    26  * v 0.2.1 - 2007-08-14
       
    27  *	-fix: handling of colspans didn't work properly for the very first spanning column
       
    28  *	-change: altered the cookie handling routines for easier management
       
    29  * v 0.2.0 - 2007-04-14
       
    30  *	-change: supports tables with colspanned and rowspanned cells now
       
    31  * v 0.1.4 - 2007-04-11
       
    32  *	-change: added onToggle option to specify a custom callback function for the toggling over the column header list
       
    33  * v 0.1.3 - 2007-04-05
       
    34  *	-fix: bug when saving the value in a cookie
       
    35  *	-change: toggleColumns takes a number or an array of numbers as argument now
       
    36  * v 0.1.2 - 2007-04-02
       
    37  * 	-change: added jsDoc style documentation and examples
       
    38  * 	-change: the column index passed to toggleColumns() starts at 1 now (conforming to the values passed in the hideInList and colsHidden options)
       
    39  * v 0.1.1 - 2007-03-30
       
    40  * 	-change: changed hideInList and colsHidden options to hold integer values for the column indexes to be affected
       
    41  *	-change: made the toggleColumns function accessible through the jquery object, to toggle the state without the need for the column header list
       
    42  *	-fix: error when not finding the passed listTargetID in the dom
       
    43  * v 0.1.0 - 2007-03-27
       
    44  */
       
    45 
       
    46 (function($) 
       
    47 {
       
    48 	var defaults = {
       
    49 		listTargetID : null,
       
    50 		onClass : '',
       
    51 		offClass : '',
       
    52 		hideInList: [],
       
    53 		colsHidden: [],
       
    54 		saveState: false,
       
    55 		onToggle: null,
       
    56 		show: function(cell){
       
    57 			showCell(cell);
       
    58 		},
       
    59 		hide: function(cell){
       
    60 			hideCell(cell);
       
    61 		}
       
    62 	};
       
    63 	
       
    64 	var idCount = 0;
       
    65 	var cookieName = 'columnManagerC';
       
    66 
       
    67 	/**
       
    68 	 * Saves the current state for the table in a cookie.
       
    69 	 * @param {element} table	The table for which to save the current state.
       
    70 	 */
       
    71 	var saveCurrentValue = function(table)
       
    72 	{
       
    73 		var val = '', i = 0, colsVisible = table.cMColsVisible;
       
    74 		if ( table.cMSaveState && table.id && colsVisible && $.cookie )
       
    75 		{
       
    76 			for ( ; i < colsVisible.length; i++ )
       
    77 			{
       
    78 				val += ( colsVisible[i] == false ) ? 0 : 1;
       
    79 			}
       
    80 			$.cookie(cookieName + table.id, val, {expires: 9999});
       
    81 		}
       
    82 	};
       
    83 	
       
    84 	/**
       
    85 	 * Hides a cell.
       
    86 	 * It rewrites itself after the browsercheck!
       
    87 	 * @param {element} cell	The cell to hide.
       
    88 	 */
       
    89 	var hideCell = function(cell)
       
    90 	{
       
    91 		if ( jQuery.browser.msie )
       
    92 		{
       
    93 			(hideCell = function(c)
       
    94 			{
       
    95 				c.style.setAttribute('display', 'none');
       
    96 			})(cell);
       
    97 		}
       
    98 		else
       
    99 		{
       
   100 			(hideCell = function(c)
       
   101 			{
       
   102 				c.style.display = 'none';
       
   103 			})(cell);
       
   104 		}
       
   105 	};
       
   106 
       
   107 	/**
       
   108 	 * Makes a cell visible again.
       
   109 	 * It rewrites itself after the browsercheck!
       
   110 	 * @param {element} cell	The cell to show.
       
   111 	 */
       
   112 	var showCell = function(cell)
       
   113 	{
       
   114 		if ( jQuery.browser.msie )
       
   115 		{
       
   116 			(showCell = function(c)
       
   117 			{
       
   118 				c.style.setAttribute('display', 'block');
       
   119 			})(cell);
       
   120 		}
       
   121 		else
       
   122 		{
       
   123 			(showCell = function(c)
       
   124 			{
       
   125 				c.style.display = 'table-cell';
       
   126 			})(cell);
       
   127 		}
       
   128 	};
       
   129 
       
   130 	/**
       
   131 	 * Returns the visible state of a cell.
       
   132 	 * It rewrites itself after the browsercheck!
       
   133 	 * @param {element} cell	The cell to test.
       
   134 	 */
       
   135 	var cellVisible = function(cell)
       
   136 	{
       
   137 		if ( jQuery.browser.msie )
       
   138 		{
       
   139 			return (cellVisible = function(c)
       
   140 			{
       
   141 				return c.style.getAttribute('display') != 'none';
       
   142 			})(cell);
       
   143 		}
       
   144 		else
       
   145 		{
       
   146 			return (cellVisible = function(c)
       
   147 			{
       
   148 				return c.style.display != 'none';
       
   149 			})(cell);
       
   150 		}
       
   151 	};
       
   152 
       
   153 	/**
       
   154 	 * Returns the cell element which has the passed column index value.
       
   155 	 * @param {element} table	The table element.
       
   156 	 * @param {array} cells		The cells to loop through.
       
   157 	 * @param {integer} col	The column index to look for.
       
   158 	 */
       
   159 	var getCell = function(table, cells, col)
       
   160 	{
       
   161 		for ( var i = 0; i < cells.length; i++ )
       
   162 		{
       
   163 			if ( cells[i].realIndex === undefined ) //the test is here, because rows/cells could get added after the first run
       
   164 			{
       
   165 				fixCellIndexes(table);
       
   166 			}
       
   167 			if ( cells[i].realIndex == col )
       
   168 			{
       
   169 				return cells[i];
       
   170 			}
       
   171 		}
       
   172 		return null;
       
   173 	};
       
   174 
       
   175 	/**
       
   176 	 * Calculates the actual cellIndex value of all cells in the table and stores it in the realCell property of each cell.
       
   177 	 * Thats done because the cellIndex value isn't correct when colspans or rowspans are used.
       
   178 	 * Originally created by Matt Kruse for his table library - Big Thanks! (see http://www.javascripttoolbox.com/)
       
   179 	 * @param {element} table	The table element.
       
   180 	 */
       
   181 	var fixCellIndexes = function(table) 
       
   182 	{
       
   183 		var rows = table.rows;
       
   184 		var len = rows.length;
       
   185 		var matrix = [];
       
   186 		for ( var i = 0; i < len; i++ )
       
   187 		{
       
   188 			var cells = rows[i].cells;
       
   189 			var clen = cells.length;
       
   190 			for ( var j = 0; j < clen; j++ )
       
   191 			{
       
   192 				var c = cells[j];
       
   193 				var rowSpan = c.rowSpan || 1;
       
   194 				var colSpan = c.colSpan || 1;
       
   195 				var firstAvailCol = -1;
       
   196 				if ( !matrix[i] )
       
   197 				{ 
       
   198 					matrix[i] = []; 
       
   199 				}
       
   200 				var m = matrix[i];
       
   201 				// Find first available column in the first row
       
   202 				while ( m[++firstAvailCol] ) {}
       
   203 				c.realIndex = firstAvailCol;
       
   204 				for ( var k = i; k < i + rowSpan; k++ )
       
   205 				{
       
   206 					if ( !matrix[k] )
       
   207 					{ 
       
   208 						matrix[k] = []; 
       
   209 					}
       
   210 					var matrixrow = matrix[k];
       
   211 					for ( var l = firstAvailCol; l < firstAvailCol + colSpan; l++ )
       
   212 					{
       
   213 						matrixrow[l] = 1;
       
   214 					}
       
   215 				}
       
   216 			}
       
   217 		}
       
   218 	};
       
   219 	
       
   220 	/**
       
   221 	 * Manages the column display state for a table.
       
   222 	 *
       
   223 	 * Features:
       
   224 	 * Saves the state and recreates it on the next visit of the site (requires cookie-plugin).
       
   225 	 * Extracts all headers and builds an unordered(<UL>) list out of them, where clicking an list element will show/hide the matching column.
       
   226 	 *
       
   227 	 * @param {map} options		An object for optional settings (options described below).
       
   228 	 *
       
   229 	 * @option {string} listTargetID	The ID attribute of the element the column header list will be added to.
       
   230 	 *						Default value: null
       
   231 	 * @option {string} onClass		A CSS class that is used on the items in the column header list, for which the column state is visible 
       
   232 	 *						Works only with listTargetID set!
       
   233 	 *						Default value: ''
       
   234 	 * @option {string} offClass		A CSS class that is used on the items in the column header list, for which the column state is hidden.
       
   235 	 *						Works only with listTargetID set!
       
   236 	 *						Default value: ''
       
   237 	 * @option {array} hideInList	An array of numbers. Each column with the matching column index won't be displayed in the column header list.
       
   238 	 *						Index starting at 1!
       
   239 	 *						Default value: [] (all columns will be included in the list)
       
   240 	 * @option {array} colsHidden	An array of numbers. Each column with the matching column index will get hidden by default.
       
   241 	 *						The value is overwritten when saveState is true and a cookie is set for this table.
       
   242 	 *						Index starting at 1!
       
   243 	 *						Default value: []
       
   244 	 * @option {boolean} saveState	Save a cookie with the sate information of each column.
       
   245 	 *						Requires jQuery cookie plugin.
       
   246 	 *						Default value: false
       
   247 	 * @option {function} onToggle	Callback function which is triggered when the visibility state of a column was toggled through the column header list.
       
   248 	 *						The passed parameters are: the column index(integer) and the visibility state(boolean).
       
   249 	 *						Default value: null
       
   250 	 *
       
   251 	 * @option {function} show		Function which is called to show a table cell.
       
   252 	 *						The passed parameters are: the table cell (DOM-element).
       
   253 	 *						Default value: a functions which simply sets the display-style to block (visible)
       
   254 	 *
       
   255 	 * @option {function} hide		Function which is called to hide a table cell.
       
   256 	 *						The passed parameters are: the table cell (DOM-element).
       
   257 	 *						Default value: a functions which simply sets the display-style to none (invisible)
       
   258 	 *
       
   259 	 * @example $('#table').columnManager([listTargetID: "target", onClass: "on", offClass: "off"]);
       
   260 	 * @desc Creates the column header list in the element with the ID attribute "target" and sets the CSS classes for the visible("on") and hidden("off") states.
       
   261 	 *
       
   262 	 * @example $('#table').columnManager([listTargetID: "target", hideInList: [1, 4]]);
       
   263 	 * @desc Creates the column header list in the element with the ID attribute "target" but without the first and fourth column.
       
   264 	 *
       
   265 	 * @example $('#table').columnManager([listTargetID: "target", colsHidden: [1, 4]]);
       
   266 	 * @desc Creates the column header list in the element with the ID attribute "target" and hides the first and fourth column by default.
       
   267 	 *
       
   268 	 * @example $('#table').columnManager([saveState: true]);
       
   269 	 * @desc Enables the saving of visibility informations for the columns. Does not create a column header list! Toggle the columns visiblity through $('selector').toggleColumns().
       
   270 	 *
       
   271 	 * @type jQuery
       
   272 	 *
       
   273 	 * @name columnManager
       
   274 	 * @cat Plugins/columnManager
       
   275 	 * @author Roman Weich (http://p.sohei.org)
       
   276 	 */
       
   277 	$.fn.columnManager = function(options)
       
   278 	{
       
   279 		var settings = $.extend({}, defaults, options);
       
   280 
       
   281 		/**
       
   282 		 * Creates the column header list.
       
   283 		 * @param {element} table	The table element for which to create the list.
       
   284 		 */
       
   285 		var createColumnHeaderList = function(table)
       
   286 		{
       
   287 			if ( !settings.listTargetID )
       
   288 			{
       
   289 				return;
       
   290 			}
       
   291 			var $target = $('#' + settings.listTargetID);
       
   292 			if ( !$target.length )
       
   293 			{
       
   294 				return;
       
   295 			}
       
   296 			//select headrow - when there is no thead-element, use the first row in the table
       
   297 			var headRow = null;
       
   298 			if ( table.tHead && table.tHead.length )
       
   299 			{
       
   300 				headRow = table.tHead.rows[0];
       
   301 			}
       
   302 			else if ( table.rows.length )
       
   303 			{
       
   304 				headRow = table.rows[0];
       
   305 			}
       
   306 			else
       
   307 			{
       
   308 				return; //no header - nothing to do
       
   309 			}
       
   310 			var cells = headRow.cells;
       
   311 			if ( !cells.length )
       
   312 			{
       
   313 				return; //no header - nothing to do
       
   314 			}
       
   315 			//create list in target element
       
   316 			var $list = null;
       
   317 			if ( $target.get(0).nodeName.toUpperCase() == 'UL' )
       
   318 			{
       
   319 				$list = $target;
       
   320 			}
       
   321 			else
       
   322 			{
       
   323 				$list = $('<ul></ul>');
       
   324 				$target.append($list);
       
   325 			}
       
   326 			var colsVisible = table.cMColsVisible;
       
   327 			//create list elements from headers
       
   328 			for ( var i = 0; i < cells.length; i++ )
       
   329 			{
       
   330 				if ( $.inArray(i + 1, settings.hideInList) >= 0 )
       
   331 				{
       
   332 					continue;
       
   333 				}
       
   334 				colsVisible[i] = ( colsVisible[i] !== undefined ) ? colsVisible[i] : true;
       
   335 				var text = $(cells[i]).text(), 
       
   336 					addClass;
       
   337 				if ( !text.length )
       
   338 				{
       
   339 					text = $(cells[i]).html();
       
   340 					if ( !text.length ) //still nothing?
       
   341 					{
       
   342 						text = 'undefined';
       
   343 					}
       
   344 				}
       
   345 				if ( colsVisible[i] && settings.onClass )
       
   346 				{
       
   347 					addClass = settings.onClass;
       
   348 				}
       
   349 				else if ( !colsVisible[i] && settings.offClass )
       
   350 				{
       
   351 					addClass = settings.offClass;
       
   352 				}
       
   353 				var $li = $('<li class="' + addClass + '">' + text + '</li>').click(toggleClick);
       
   354 				$li[0].cmData = {id: table.id, col: i};
       
   355 				$list.append($li);
       
   356 			}
       
   357 			table.cMColsVisible = colsVisible;
       
   358 		};
       
   359 
       
   360 		/**
       
   361 		 * called when an item in the column header list is clicked
       
   362 		 */
       
   363 		var toggleClick = function()
       
   364 		{
       
   365 			//get table id and column name
       
   366 			var data = this.cmData;
       
   367 			if ( data && data.id && data.col >= 0 )
       
   368 			{
       
   369 				var colNum = data.col, 
       
   370 					$table = $('#' + data.id);
       
   371 				if ( $table.length )
       
   372 				{
       
   373 					$table.toggleColumns([colNum + 1], settings);
       
   374 					//set the appropriate classes to the column header list
       
   375 					var colsVisible = $table.get(0).cMColsVisible;
       
   376 					if ( settings.onToggle )
       
   377 					{
       
   378 						settings.onToggle.apply($table.get(0), [colNum + 1, colsVisible[colNum]]);
       
   379 					}
       
   380 				}
       
   381 			}
       
   382 		};
       
   383 
       
   384 		/**
       
   385 		 * Reads the saved state from the cookie.
       
   386 		 * @param {string} tableID	The ID attribute from the table.
       
   387 		 */
       
   388 		var getSavedValue = function(tableID)
       
   389 		{
       
   390 			var val = $.cookie(cookieName + tableID);
       
   391 			if ( val )
       
   392 			{
       
   393 				var ar = val.split('');
       
   394 				for ( var i = 0; i < ar.length; i++ )
       
   395 				{
       
   396 					ar[i] &= 1;
       
   397 				}
       
   398 				return ar;
       
   399 			}
       
   400 			return false;
       
   401 		};
       
   402 
       
   403         return this.each(function()
       
   404         {
       
   405 			this.id = this.id || 'jQcM0O' + idCount++; //we need an id for the column header list stuff
       
   406 			var i, 
       
   407 				colsHide = [], 
       
   408 				colsVisible = [];
       
   409 			//fix cellIndex values
       
   410 			fixCellIndexes(this);
       
   411 			//some columns hidden by default?
       
   412 			if ( settings.colsHidden.length )
       
   413 			{
       
   414 				for ( i = 0; i < settings.colsHidden.length; i++ )
       
   415 				{
       
   416 					colsVisible[settings.colsHidden[i] - 1] = true;
       
   417 					colsHide[settings.colsHidden[i] - 1] = true;
       
   418 				}
       
   419 			}
       
   420 			//get saved state - and overwrite defaults
       
   421 			if ( settings.saveState )
       
   422 			{
       
   423 				var colsSaved = getSavedValue(this.id);
       
   424 				if ( colsSaved && colsSaved.length )
       
   425 				{
       
   426 					for ( i = 0; i < colsSaved.length; i++ )
       
   427 					{
       
   428 						colsVisible[i] = true;
       
   429 						colsHide[i] = !colsSaved[i];
       
   430 					}
       
   431 				}
       
   432 				this.cMSaveState = true;
       
   433 			}
       
   434 			//assign initial colsVisible var to the table (needed for toggling and saving the state)
       
   435 			this.cMColsVisible = colsVisible;
       
   436 			//something to hide already?
       
   437 			if ( colsHide.length )
       
   438 			{
       
   439 				var a = [];
       
   440 				for ( i = 0; i < colsHide.length; i++ )
       
   441 				{
       
   442 					if ( colsHide[i] )
       
   443 					{
       
   444 						a[a.length] = i + 1;
       
   445 					}
       
   446 				}
       
   447 				if ( a.length )
       
   448 				{
       
   449 					$(this).toggleColumns(a);
       
   450 				}
       
   451 			}
       
   452 			//create column header list
       
   453 			createColumnHeaderList(this);
       
   454         }); 
       
   455 	};
       
   456 
       
   457 	/**
       
   458 	 * Shows or hides table columns.
       
   459 	 *
       
   460 	 * @param {integer|array} columns		A number or an array of numbers. The display state(visible/hidden) for each column with the matching column index will get toggled.
       
   461 	 *							Column index starts at 1! (see the example)
       
   462 	 *
       
   463 	 * @param {map} options		An object for optional settings to handle the on and off CSS classes in the column header list (options described below).
       
   464 	 * @option {string} listTargetID	The ID attribute of the element with the column header.
       
   465 	 * @option {string} onClass		A CSS class that is used on the items in the column header list, for which the column state is visible 
       
   466 	 * @option {string} offClass		A CSS class that is used on the items in the column header list, for which the column state is hidden.
       
   467 	 * @option {function} show		Function which is called to show a table cell.
       
   468 	 * @option {function} hide		Function which is called to hide a table cell.
       
   469 	 *
       
   470 	 * @example $('#table').toggleColumns([2, 4], {hide: function(cell) { $(cell).fadeOut("slow"); }});
       
   471 	 * @before <table id="table">
       
   472 	 *   			<thead>
       
   473 	 *   				<th>one</th
       
   474 	 *   				<th>two</th
       
   475 	 *   				<th>three</th
       
   476 	 *   				<th>four</th
       
   477 	 *   			</thead>
       
   478 	 * 		   </table>
       
   479 	 * @desc Toggles the visible state for the columns "two" and "four". Use custom function to fade the cell out when hiding it.
       
   480 	 *
       
   481 	 * @example $('#table').toggleColumns(3, {listTargetID: 'theID', onClass: 'vis'});
       
   482 	 * @before <table id="table">
       
   483 	 *   			<thead>
       
   484 	 *   				<th>one</th
       
   485 	 *   				<th>two</th
       
   486 	 *   				<th>three</th
       
   487 	 *   				<th>four</th
       
   488 	 *   			</thead>
       
   489 	 * 		   </table>
       
   490 	 * @desc Toggles the visible state for column "three" and sets or removes the CSS class 'vis' to the appropriate column header according to the visibility of the column.
       
   491 	 *
       
   492 	 * @type jQuery
       
   493 	 *
       
   494 	 * @name toggleColumns
       
   495 	 * @cat Plugins/columnManager
       
   496 	 * @author Roman Weich (http://p.sohei.org)
       
   497 	 */
       
   498 	$.fn.toggleColumns = function(columns, cmo)
       
   499 	{
       
   500         return this.each(function() 
       
   501         {
       
   502 			var i, toggle, di, 
       
   503 				rows = this.rows, 
       
   504 				colsVisible = this.cMColsVisible;
       
   505 
       
   506 			if ( !columns )
       
   507 				return;
       
   508 
       
   509 			if ( columns.constructor == Number )
       
   510 				columns = [columns];
       
   511 
       
   512 			if ( !colsVisible )
       
   513 				colsVisible = this.cMColsVisible = [];
       
   514 
       
   515 			//go through all rows in the table and hide the cells
       
   516 			for ( i = 0; i < rows.length; i++ )
       
   517 			{
       
   518 				var cells = rows[i].cells;
       
   519 				for ( var k = 0; k < columns.length; k++ )
       
   520 				{
       
   521 					var col = columns[k] - 1;
       
   522 					if ( col >= 0 )
       
   523 					{
       
   524 						//find the cell with the correct index
       
   525 						var c = getCell(this, cells, col);
       
   526 						//cell not found - maybe a previous one has a colspan? - search it!
       
   527 						if ( !c )
       
   528 						{
       
   529 							var cco = col;
       
   530 							while ( cco > 0 && !(c = getCell(this, cells, --cco)) ) {} //find the previous cell
       
   531 							if ( !c )
       
   532 							{
       
   533 								continue;
       
   534 							}
       
   535 						}
       
   536 						//set toggle direction
       
   537 						if ( colsVisible[col] == undefined )//not initialized yet
       
   538 						{
       
   539 							colsVisible[col] = true;
       
   540 						}
       
   541 						if ( colsVisible[col] )
       
   542 						{
       
   543 							toggle = cmo && cmo.hide ? cmo.hide : hideCell;
       
   544 							di = -1;
       
   545 						}
       
   546 						else
       
   547 						{
       
   548 							toggle = cmo && cmo.show ? cmo.show : showCell;
       
   549 							di = 1;
       
   550 						}
       
   551 						if ( !c.chSpan )
       
   552 						{
       
   553 							c.chSpan = 0;
       
   554 						}
       
   555 						//the cell has a colspan - so dont show/hide - just change the colspan
       
   556 						if ( c.colSpan > 1 || (di == 1 && c.chSpan && cellVisible(c)) )
       
   557 						{
       
   558 							//is the colspan even reaching this cell? if not we have a rowspan -> nothing to do
       
   559 							if ( c.realIndex + c.colSpan + c.chSpan - 1 < col )
       
   560 							{
       
   561 								continue;
       
   562 							}
       
   563 							c.colSpan += di;
       
   564 							c.chSpan += di * -1;
       
   565 						}
       
   566 						else if ( c.realIndex + c.chSpan < col )//a previous cell was found, but doesn't affect this one (rowspan)
       
   567 						{
       
   568 							continue;
       
   569 						}
       
   570 						else //toggle cell
       
   571 						{
       
   572 							toggle(c);
       
   573 						}
       
   574 					}
       
   575 				}
       
   576 			}
       
   577 			//set the colsVisible var
       
   578 			for ( i = 0; i < columns.length; i++ )
       
   579 			{
       
   580 				this.cMColsVisible[columns[i] - 1] = !colsVisible[columns[i] - 1];
       
   581 				//set the appropriate classes to the column header list, if the options have been passed
       
   582 				if ( cmo && cmo.listTargetID && ( cmo.onClass || cmo.offClass ) )
       
   583 				{
       
   584 					var onC = cmo.onClass, offC = cmo.offClass, $li;
       
   585 					if ( colsVisible[columns[i] - 1] )
       
   586 					{
       
   587 						onC = offC;
       
   588 						offC = cmo.onClass;
       
   589 					}
       
   590 					$li = $("#" + cmo.listTargetID + " li").filter(function(){return this.cmData && this.cmData.col == columns[i] - 1;});
       
   591 					if ( onC )
       
   592 					{
       
   593 						$li.removeClass(onC);
       
   594 					}
       
   595 					if ( offC )
       
   596 					{
       
   597 						$li.addClass(offC);
       
   598 					}
       
   599 				}
       
   600 			}
       
   601 			saveCurrentValue(this);
       
   602 		});
       
   603 	};
       
   604 
       
   605 	/**
       
   606 	 * Shows all table columns.
       
   607 	 * When columns are passed through the parameter only the passed ones become visible.
       
   608 	 *
       
   609 	 * @param {integer|array} columns		A number or an array of numbers. Each column with the matching column index will become visible.
       
   610 	 *							Column index starts at 1!
       
   611 	 *
       
   612 	 * @param {map} options		An object for optional settings which will get passed to $().toggleColumns().
       
   613 	 *
       
   614 	 * @example $('#table').showColumns();
       
   615 	 * @desc Sets the visibility state of all hidden columns to visible.
       
   616 	 *
       
   617 	 * @example $('#table').showColumns(3);
       
   618 	 * @desc Show column number three.
       
   619 	 *
       
   620 	 * @type jQuery
       
   621 	 *
       
   622 	 * @name showColumns
       
   623 	 * @cat Plugins/columnManager
       
   624 	 * @author Roman Weich (http://p.sohei.org)
       
   625 	 */
       
   626 	$.fn.showColumns = function(columns, cmo)
       
   627 	{
       
   628         return this.each(function() 
       
   629         {
       
   630 			var i,
       
   631 				cols = [],
       
   632 				cV = this.cMColsVisible;
       
   633 			if ( cV )
       
   634 			{
       
   635 				if ( columns && columns.constructor == Number ) 
       
   636 					columns = [columns];
       
   637 
       
   638 				for ( i = 0; i < cV.length; i++ )
       
   639 				{
       
   640 					//if there were no columns passed, show all - or else show only the columns the user wants to see
       
   641 					if ( !cV[i] && (!columns || $.inArray(i + 1, columns) > -1) )
       
   642 						cols.push(i + 1);
       
   643 				}
       
   644 				
       
   645 				$(this).toggleColumns(cols, cmo);
       
   646 			}
       
   647 		});
       
   648 	};
       
   649 
       
   650 	/**
       
   651 	 * Hides table columns.
       
   652 	 *
       
   653 	 * @param {integer|array} columns		A number or an array of numbers. Each column with the matching column index will get hidden.
       
   654 	 *							Column index starts at 1!
       
   655 	 *
       
   656 	 * @param {map} options		An object for optional settings which will get passed to $().toggleColumns().
       
   657 	 *
       
   658 	 * @example $('#table').hideColumns(3);
       
   659 	 * @desc Hide column number three.
       
   660 	 *
       
   661 	 * @type jQuery
       
   662 	 *
       
   663 	 * @name hideColumns
       
   664 	 * @cat Plugins/columnManager
       
   665 	 * @author Roman Weich (http://p.sohei.org)
       
   666 	 */
       
   667 	$.fn.hideColumns = function(columns, cmo)
       
   668 	{
       
   669         return this.each(function() 
       
   670         {
       
   671 			var i,
       
   672 				cols = columns,
       
   673 				cV = this.cMColsVisible;
       
   674 			if ( cV )
       
   675 			{
       
   676 				if ( columns.constructor == Number ) 
       
   677 					columns = [columns];
       
   678 				cols = [];
       
   679 
       
   680 				for ( i = 0; i < columns.length; i++ )
       
   681 				{
       
   682 					if ( cV[columns[i] - 1] || cV[columns[i] - 1] == undefined )
       
   683 						cols.push(columns[i]);
       
   684 				}
       
   685 				
       
   686 			}
       
   687 			$(this).toggleColumns(cols, cmo);
       
   688 		});
       
   689 	};
       
   690 })(jQuery);