jquery.tablehover.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * jQuery tableHover plugin
  3. * Version: 0.1.3
  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.1.3 - 2007-09-04
  15. * - fix: highlight did not work when the hovered table cell had child elements inside
  16. * v 0.1.2 - 2007-08-13
  17. * - fix/change: changed event binding routine, as is got really slow with jquery 1.1.3.1
  18. * -change: added new option "ignoreCols", through which columns can be excluded from the highlighting process
  19. * v 0.1.1 - 2007-06-05
  20. * - fix: errors when using the plugin on a table not having a theader or tfoot
  21. * v 0.1.0 - 2007-05-31
  22. */
  23. (function($)
  24. {
  25. /**
  26. * Calculates the actual cellIndex value of all cells in the table and stores it in the realCell property of each cell.
  27. * Thats done because the cellIndex value isn't correct when colspans or rowspans are used.
  28. * Originally created by Matt Kruse for his table library - Big Thanks! (see http://www.javascripttoolbox.com/)
  29. * @param {element} table The table element.
  30. */
  31. var fixCellIndexes = function(table)
  32. {
  33. var rows = table.rows;
  34. var len = rows.length;
  35. var matrix = [];
  36. for ( var i = 0; i < len; i++ )
  37. {
  38. var cells = rows[i].cells;
  39. var clen = cells.length;
  40. for ( var j = 0; j < clen; j++ )
  41. {
  42. var c = cells[j];
  43. var rowSpan = c.rowSpan || 1;
  44. var colSpan = c.colSpan || 1;
  45. var firstAvailCol = -1;
  46. if ( !matrix[i] )
  47. {
  48. matrix[i] = [];
  49. }
  50. var m = matrix[i];
  51. // Find first available column in the first row
  52. while ( m[++firstAvailCol] ) {}
  53. c.realIndex = firstAvailCol;
  54. for ( var k = i; k < i + rowSpan; k++ )
  55. {
  56. if ( !matrix[k] )
  57. {
  58. matrix[k] = [];
  59. }
  60. var matrixrow = matrix[k];
  61. for ( var l = firstAvailCol; l < firstAvailCol + colSpan; l++ )
  62. {
  63. matrixrow[l] = 1;
  64. }
  65. }
  66. }
  67. }
  68. };
  69. /**
  70. * Sets the rowIndex of each row in the table.
  71. * Opera seems to get that wrong using document order instead of logical order on the tfoot-tbody part.
  72. * @param {element} table The table element.
  73. */
  74. var fixRowIndexes = function(tbl)
  75. {
  76. var v = 0, i, k, r = ( tbl.tHead ) ? tbl.tHead.rows : 0;
  77. if ( r )
  78. {
  79. for ( i = 0; i < r.length; i++ )
  80. {
  81. r[i].realRIndex = v++;
  82. }
  83. }
  84. for ( k = 0; k < tbl.tBodies.length; k++ )
  85. {
  86. r = tbl.tBodies[k].rows;
  87. if ( r )
  88. {
  89. for ( i = 0; i < r.length; i++ )
  90. {
  91. r[i].realRIndex = v++;
  92. }
  93. }
  94. }
  95. r = ( tbl.tFoot ) ? tbl.tFoot.rows : 0;
  96. if ( r )
  97. {
  98. for ( i = 0; i < r.length; i++ )
  99. {
  100. r[i].realRIndex = v++;
  101. }
  102. }
  103. };
  104. /**
  105. * Highlights table rows and/or columns on mouse over.
  106. * Fixes the highlight of the currently highlighted rows/columns on click.
  107. * Works on tables with rowspans and colspans.
  108. *
  109. * @param {map} options An object for optional settings (options described below).
  110. *
  111. * @option {boolean} allowHead Allow highlighting when hovering over the table header.
  112. * Default value: true
  113. * @option {boolean} allowBody Allow highlighting when hovering over the table body.
  114. * Default value: true
  115. * @option {boolean} allowFoot Allow highlighting when hovering over the table footer.
  116. * Default value: true
  117. *
  118. * @option {boolean} headRows If true the rows in the table header will be highlighted when hovering over them.
  119. * Default value: false
  120. * @option {boolean} bodyRows If true the rows in the table body will be highlighted when hovering over them.
  121. * Default value: true
  122. * @option {boolean} footRows If true the rows in the table footer will be highlighted when hovering over them.
  123. * Default value: false
  124. * @option {boolean} spanRows When hovering over a cell spanning over more than one row, highlight all spanned rows.
  125. * Default value: true
  126. *
  127. * @option {boolean} headCols If true the cells in the table header (matching the currently hovered column) will be highlighted.
  128. * Default value: false
  129. * @option {boolean} bodyCols If true the cells in the table body (matching the currently hovered column) will be highlighted.
  130. * Default value: true
  131. * @option {boolean} footCols If true the cells in the table footer (matching the currently hovered column) will be highlighted.
  132. * Default value: false
  133. * @option {boolean} spanCols When hovering over a cell spanning over more than one column, highlight all spanned columns.
  134. * Default value: true
  135. * @option {array} ignoreCols An array of numbers. Each column with the matching column index won't be included in the highlighting process.
  136. * Index starting at 1!
  137. * Default value: [] (empty array)
  138. *
  139. * @option {boolean} headCells Set a special highlight class to the cell the mouse pointer is currently pointing at (inside the table header only).
  140. * Default value: false
  141. * @option {boolean} bodyCells Set a special highlight class to the cell the mouse pointer is currently pointing at (inside the table body only).
  142. * Default value: true
  143. * @option {boolean} footCells Set a special highlight class to the cell the mouse pointer is currently pointing at (inside the table footer only).
  144. * Default value: false
  145. *
  146. * @option {string} rowClass The css class set to the currently highlighted row.
  147. * Default value: 'hover'
  148. * @option {string} colClass The css class set to the currently highlighted column.
  149. * Default value: '' (empty string)
  150. * @option {string} cellClass The css class set to the currently highlighted cell.
  151. * Default value: '' (empty string)
  152. * @option {string} clickClass The css class set to the currently highlighted row and column on mouse click.
  153. * Default value: '' (empty string)
  154. *
  155. * @example $('#table').tableHover({});
  156. * @desc Add simple row highlighting to #table with default settings.
  157. *
  158. * @example $('#table').tableHover({rowClass: "someclass", colClass: "someotherclass"});
  159. * @desc Add row and columnhighlighting to #table and set the specified css classes to the highlighted cells.
  160. *
  161. * @example $('#table').tableHover({clickClass: "someclickclass"});
  162. * @desc Add simple row highlighting to #table and set the specified css class on the cells when clicked.
  163. *
  164. * @example $('#table').tableHover({allowBody: false, allowFoot: false, allowHead: true, colClass: "someclass"});
  165. * @desc Add column highlighting on #table only highlighting the cells when hovering over the table header.
  166. *
  167. * @example $('#table').tableHover({bodyCols: false, footCols: false, headCols: true, colClass: "someclass"});
  168. * @desc Add column highlighting on #table only for the cells in the header.
  169. *
  170. * @type jQuery
  171. *
  172. * @name tableHover
  173. * @cat Plugins/tableHover
  174. * @author Roman Weich (http://p.sohei.org)
  175. */
  176. $.fn.tableHover = function(options)
  177. {
  178. var settings = $.extend({
  179. allowHead : true,
  180. allowBody : true,
  181. allowFoot : true,
  182. headRows : false,
  183. bodyRows : true,
  184. footRows : false,
  185. spanRows : true,
  186. headCols : false,
  187. bodyCols : true,
  188. footCols : false,
  189. spanCols : true,
  190. ignoreCols : [],
  191. headCells : false,
  192. bodyCells : true,
  193. footCells : false,
  194. //css classes,,
  195. rowClass : 'hover',
  196. colClass : '',
  197. cellClass : '',
  198. clickClass : ''
  199. }, options);
  200. return this.each(function()
  201. {
  202. var colIndex = [], rowIndex = [], tbl = this, r, rCnt = 0, lastClick = [-1, -1];
  203. if ( !tbl.tBodies || !tbl.tBodies.length )
  204. {
  205. return;
  206. }
  207. /**
  208. * Adds all rows and each of their cells to the row and column indexes.
  209. * @param {array} rows An array of table row elements to add.
  210. * @param {string} nodeName Defines whether the rows are in the header, body or footer of the table.
  211. */
  212. var addToIndex = function(rows, nodeName)
  213. {
  214. var c, row, rowI, cI, rI, s;
  215. //loop through the rows
  216. for ( rowI = 0; rowI < rows.length; rowI++, rCnt++ )
  217. {
  218. row = rows[rowI];
  219. //each cell
  220. for ( cI = 0; cI < row.cells.length; cI++ )
  221. {
  222. c = row.cells[cI];
  223. //add to rowindex
  224. if ( (nodeName == 'TBODY' && settings.bodyRows)
  225. || (nodeName == 'TFOOT' && settings.footRows)
  226. || (nodeName == 'THEAD' && settings.headRows) )
  227. {
  228. s = c.rowSpan;
  229. while ( --s >= 0 )
  230. {
  231. rowIndex[rCnt + s].push(c);
  232. }
  233. }
  234. //add do colindex
  235. if ( (nodeName == 'TBODY' && settings.bodyCols)
  236. || (nodeName == 'THEAD' && settings.headCols)
  237. || (nodeName == 'TFOOT' && settings.footCols) )
  238. {
  239. s = c.colSpan;
  240. while ( --s >= 0 )
  241. {
  242. rI = c.realIndex + s;
  243. if ( $.inArray(rI + 1, settings.ignoreCols) > -1 )
  244. {
  245. break;//dont highlight the columns in the ignoreCols array
  246. }
  247. if ( !colIndex[rI] )
  248. {
  249. colIndex[rI] = [];
  250. }
  251. colIndex[rI].push(c);
  252. }
  253. }
  254. //allow hover for the cell?
  255. if ( (nodeName == 'TBODY' && settings.allowBody)
  256. || (nodeName == 'THEAD' && settings.allowHead)
  257. || (nodeName == 'TFOOT' && settings.allowFoot) )
  258. {
  259. c.thover = true;
  260. }
  261. }
  262. }
  263. };
  264. /**
  265. * Mouseover event handling. Set the highlight to the rows/cells.
  266. */
  267. var over = function(e)
  268. {
  269. var p = e.target;
  270. while ( p != this && p.thover !== true )
  271. {
  272. p = p.parentNode;
  273. }
  274. if ( p.thover === true )
  275. {
  276. highlight(p, true);
  277. }
  278. };
  279. /**
  280. * Mouseout event handling. Remove the highlight from the rows/cells.
  281. */
  282. var out = function(e)
  283. {
  284. var p = e.target;
  285. while ( p != this && p.thover !== true )
  286. {
  287. p = p.parentNode;
  288. }
  289. if ( p.thover === true )
  290. {
  291. highlight(p, false);
  292. }
  293. };
  294. /**
  295. * Mousedown event handling. Sets or removes the clickClass css style to the currently highlighted rows/cells.
  296. */
  297. var click = function(e)
  298. {
  299. if ( e.target.thover && settings.clickClass != '' )
  300. {
  301. var x = e.target.realIndex, y = e.target.parentNode.realRIndex, s = '';
  302. //unclick
  303. $('td.' + settings.clickClass + ', th.' + settings.clickClass, tbl).removeClass(settings.clickClass);
  304. if ( x != lastClick[0] || y != lastClick[1] )
  305. {
  306. //click..
  307. if ( settings.rowClass != '' )
  308. {
  309. s += ',.' + settings.rowClass;
  310. }
  311. if ( settings.colClass != '' )
  312. {
  313. s += ',.' + settings.colClass;
  314. }
  315. if ( settings.cellClass != '' )
  316. {
  317. s += ',.' + settings.cellClass;
  318. }
  319. if ( s != '' )
  320. {
  321. $('td, th', tbl).filter(s.substring(1)).addClass(settings.clickClass);
  322. }
  323. lastClick = [x, y];
  324. }
  325. else
  326. {
  327. lastClick = [-1, -1];
  328. }
  329. }
  330. };
  331. /**
  332. * Adds or removes the highlight to/from the columns and rows.
  333. * @param {element} cell The cell with the mouseover/mouseout event.
  334. * @param {boolean} on Defines whether the style will be set or removed.
  335. */
  336. var highlight = function(cell, on)
  337. {
  338. if ( on ) //create dummy funcs - dont want to test for on==true all the time
  339. {
  340. $.fn.tableHoverHover = $.fn.addClass;
  341. }
  342. else
  343. {
  344. $.fn.tableHoverHover = $.fn.removeClass;
  345. }
  346. //highlight columns
  347. var h = colIndex[cell.realIndex] || [], rH = [], i = 0, rI, nn;
  348. if ( settings.colClass != '' )
  349. {
  350. while ( settings.spanCols && ++i < cell.colSpan && colIndex[cell.realIndex + i] )
  351. {
  352. h = h.concat(colIndex[cell.realIndex + i]);
  353. }
  354. $(h).tableHoverHover(settings.colClass);
  355. }
  356. //highlight rows
  357. if ( settings.rowClass != '' )
  358. {
  359. rI = cell.parentNode.realRIndex;
  360. if ( rowIndex[rI] )
  361. {
  362. rH = rH.concat(rowIndex[rI]);
  363. }
  364. i = 0;
  365. while ( settings.spanRows && ++i < cell.rowSpan )
  366. {
  367. if ( rowIndex[rI + i] )
  368. {
  369. rH = rH.concat(rowIndex[rI + i]);
  370. }
  371. }
  372. $(rH).tableHoverHover(settings.rowClass);
  373. }
  374. //highlight cell
  375. if ( settings.cellClass != '' )
  376. {
  377. nn = cell.parentNode.parentNode.nodeName.toUpperCase();
  378. if ( (nn == 'TBODY' && settings.bodyCells)
  379. || (nn == 'THEAD' && settings.headCells)
  380. || (nn == 'TFOOT' && settings.footCells) )
  381. {
  382. $(cell).tableHoverHover(settings.cellClass);
  383. }
  384. }
  385. };
  386. fixCellIndexes(tbl);
  387. fixRowIndexes(tbl);
  388. //init rowIndex
  389. for ( r = 0; r < tbl.rows.length; r++ )
  390. {
  391. rowIndex[r] = [];
  392. }
  393. //add header cells to index
  394. if ( tbl.tHead )
  395. {
  396. addToIndex(tbl.tHead.rows, 'THEAD');
  397. }
  398. //create index - loop through the bodies
  399. for ( r = 0; r < tbl.tBodies.length; r++ )
  400. {
  401. addToIndex(tbl.tBodies[r].rows, 'TBODY');
  402. }
  403. //add footer cells to index
  404. if ( tbl.tFoot )
  405. {
  406. addToIndex(tbl.tFoot.rows, 'TFOOT');
  407. }
  408. $(this).bind('mouseover', over).bind('mouseout', out).click(click);
  409. });
  410. };
  411. })(jQuery);