alt
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow jQuery arrow jQuery Table Row Finished Code
Site Search


jQuery Table Row Finished Code

jQuery Table Row Finished Code

The Finished Code

Our second example page has demonstrated table row striping, highlighting, tooltips, collapsing/expanding, and filtering. Taken together, the JavaScript code for this page is:

  1. $(document).ready(function() {
  2.  
  3. var highlighted = "";
  4.  
  5. var column = 3;
  6.  
  7. var positionTooltip = function(event) {
  8.  
  9. var tPosX = event.pageX;
  10.  
  11. var tPosY = event.pageY + 20;
  12.  
  13. $('div.tooltip').css({top: tPosY, left: tPosX});
  14.  
  15. };
  16.  
  17. var showTooltip = function(event) {
  18.  
  19. $('div.tooltip').remove();
  20.  
  21. var $thisAuthor = $(this).text();
  22.  
  23. if ($(this).parent().is('.highlight')) {
  24.  
  25. highlighted = 'un-';
  26.  
  27. } else {
  28.  
  29. highlighted = '';
  30.  
  31. };
  32.  
  33. $('</p>
  34. <div class="tooltip">Click to ' + highlighted +
  35.  
  36. 'highlight all articles written by ' +
  37.  
  38. $thisAuthor + '</div>
  39. ').appendTo('body');
  40.  
  41. positionTooltip(event);
  42.  
  43. };
  44.  
  45. var hideTooltip = function() {
  46.  
  47. $('div.tooltip').remove();
  48.  
  49. };
  50.  
  51. $('table.striped td:nth-child(' + column + ')' )
  52.  
  53. .addClass('clickable')
  54.  
  55. .click(function(event) {
  56.  
  57. var thisClicked = $(this).text();
  58.  
  59. $('table.striped td:nth-child(' + column + ')' )
  60.  
  61. .each(function(index) {
  62.  
  63. if (thisClicked == $(this).text()) {
  64.  
  65. $(this).parent().toggleClass('highlight');
  66.  
  67. } else {
  68.  
  69. $(this).parent().removeClass('highlight');
  70.  
  71. };
  72.  
  73. })
  74.  
  75. showTooltip.call(this, event);
  76.  
  77. })
  78.  
  79. .hover(showTooltip, hideTooltip)
  80.  
  81. .mousemove(positionTooltip);
  82.  
  83. });
  84.  
  85. $(document).ready(function() {
  86.  
  87. $('table.striped').each(function() {
  88.  
  89. $(this).bind('stripe', function() {
  90.  
  91. var rowIndex = 0;
  92.  
  93. $('tbody tr:not(.filtered)', this).each(function(index) {
  94.  
  95. if ($('th',this).length) {
  96.  
  97. $(this).addClass('subhead');
  98.  
  99. rowIndex = -1;
  100.  
  101. } else {
  102.  
  103. if (rowIndex % 6 < 3) {
  104.  
  105. $(this).removeClass('odd').addClass('even');
  106.  
  107. }
  108.  
  109. else {
  110.  
  111. $(this).removeClass('even').addClass('odd');
  112.  
  113. }
  114.  
  115. }
  116.  
  117. rowIndex++;
  118.  
  119. });
  120.  
  121. });
  122.  
  123. $(this).trigger('stripe');
  124.  
  125. });
  126.  
  127. })
  128.  
  129. $(document).ready(function() {
  130.  
  131. $('table.filterable').each(function() {
  132.  
  133. var $table = $(this);
  134.  
  135. $table.find('th').each(function (column) {
  136.  
  137. if ($(this).is('.filter-column')) {
  138.  
  139. var $filters = $('
  140. <div class="filters">
  141. <h3>Filter by ' +
  142.  
  143. $(this).text() + ':</h3>
  144. </div>
  145. ');
  146.  
  147. var keywords = {};
  148.  
  149. $table.find('tbody tr td').filter(':nth-child(' + (column +
  150.  
  151. 1) + ')').each(function() {
  152.  
  153. keywords[$(this).text()] = $(this).text();
  154.  
  155. })
  156.  
  157. $('
  158. <div class="filter">all</div>
  159. ').click(function() {
  160.  
  161. $table.find('tbody tr').removeClass('filtered')
  162.  
  163. .not('.collapsed').show();
  164.  
  165. $(this).addClass('active').siblings().removeClass('active');
  166.  
  167. $table.trigger('stripe');
  168.  
  169. }).addClass('clickable active').appendTo($filters);
  170.  
  171. $.each(keywords, function (index, keyword) {
  172.  
  173. $('
  174. <div class="filter"></div>
  175. ').text(keyword).bind('click',
  176.  
  177. {'keyword': keyword}, function(event) {
  178.  
  179. $table.find('tbody tr').each(function() {
  180.  
  181. if ($('td', this).filter(':nth-child(' + (column + 1)
  182.  
  183. + ')').text() == event.data['keyword']) {
  184.  
  185. $(this).removeClass('filtered').not('.collapsed')
  186.  
  187. .show();
  188.  
  189. }
  190.  
  191. else if ($('th',this).length == 0) {
  192.  
  193. $(this).addClass('filtered').hide();
  194.  
  195. }
  196.  
  197. });
  198.  
  199. $(this).addClass('active').siblings().removeClass(
  200.  
  201. 'active');
  202.  
  203. $table.trigger('stripe');
  204.  
  205. }).addClass('clickable').appendTo($filters);
  206.  
  207. });
  208.  
  209. $filters.insertBefore($table);
  210.  
  211. }
  212.  
  213. });
  214.  
  215. });
  216.  
  217. });
  218.  
  219. $(document).ready(function() {
  220.  
  221. var toggleMinus = '../icons/bullet_toggle_minus.png';
  222.  
  223. var togglePlus = '../icons/bullet_toggle_plus.png';
  224.  
  225. var $subHead = $('tbody th:first-child');
  226.  
  227. $subHead.prepend('<img alt="collapse <br />
  228. this section" src="' + toggleMinus + '" />');
  229.  
  230. $('img', $subHead).addClass('clickable')
  231.  
  232. .click(function() {
  233.  
  234. var toggleSrc = $(this).attr('src');
  235.  
  236. if ( toggleSrc == toggleMinus ) {
  237.  
  238. $(this).attr('src', togglePlus)
  239.  
  240. .parents('tr').siblings().addClass('collapsed').fadeOut('fast');
  241.  
  242. } else {
  243.  
  244. $(this).attr('src', toggleMinus)
  245.  
  246. .parents('tr').siblings().removeClass('collapsed')
  247.  
  248. .not('.filtered').show().fadeIn('fast');
  249.  
  250. };
  251.  
  252. });
  253.  
  254. })
 

 

Summary

In this chapter, we have explored some of the ways to slice and dice the tables on our sites, reconfiguring them into beautiful and functional containers for our data. We have covered sorting data in tables, using different kinds of data (words, numbers, dates) as sort keys along with paginating tables into easily-viewed chunks. We have learned sophisticated row striping techniques and JavaScript-powered tooltips. We have also walked through expanding and collapsing as well as filtering and highlighting of rows that match the given criteria.

We've even touched briefly on some quite advanced topics, such as sorting and paging with server-side code and AJAX techniques, dynamically calculating page coordinates for elements, and writing a jQuery plug-in.

As we have seen, properly semantic HTML tables wrap a great deal of subtlety and complexity in a small package. Fortunately, jQuery can help us easily tame these creatures, allowing the full power of tabular data to come to the surface.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape