Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

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:



Sample Code
  1. $(document).ready(function() {
  2. var highlighted = ""
  3. var column = 3
  4. var positionTooltip = function(event) {
  5. var tPosX = event.pageX
  6. var tPosY = event.pageY + 20
  7. $('div.tooltip').css({top: tPosY, left: tPosX})
  8. }
  9. var showTooltip = function(event) {
  10. $('div.tooltip').remove()
  11. var $thisAuthor = $(this).text()
  12. if ($(this).parent().is('.highlight')) {
  13. highlighted = 'un-'
  14. } else {
  15. highlighted = ''
  16. }
  17. $('</p><br>
  18. <div class="tooltip">Click to ' + highlighted +
  19. 'highlight all articles written by ' +
  20. $thisAuthor + '</div>
  21. ').appendTo('body')
  22. positionTooltip(event)
  23. }
  24. var hideTooltip = function() {
  25. $('div.tooltip').remove()
  26. }
  27. $('table.striped td:nth-child(' + column + ')' )
  28. .addClass('clickable')
  29. .click(function(event) {
  30. var thisClicked = $(this).text()
  31. $('table.striped td:nth-child(' + column + ')' )
  32. .each(function(index) {
  33. if (thisClicked == $(this).text()) {
  34. $(this).parent().toggleClass('highlight')
  35. } else {
  36. $(this).parent().removeClass('highlight')
  37. }
  38. })
  39. showTooltip.call(this, event)
  40. })
  41. .hover(showTooltip, hideTooltip)
  42. .mousemove(positionTooltip)
  43. })
  44. $(document).ready(function() {
  45. $('table.striped').each(function() {
  46. $(this).bind('stripe', function() {
  47. var rowIndex = 0
  48. $('tbody tr:not(.filtered)', this).each(function(index) {
  49. if ($('th',this).length) {
  50. $(this).addClass('subhead')
  51. rowIndex = -1
  52. } else {
  53. if (rowIndex % 6 < 3) {
  54. $(this).removeClass('odd').addClass('even')
  55. }
  56. else {
  57. $(this).removeClass('even').addClass('odd')
  58. }
  59. }
  60. rowIndex++
  61. })
  62. })
  63. $(this).trigger('stripe')
  64. })
  65. })
  66. $(document).ready(function() {
  67. $('table.filterable').each(function() {
  68. var $table = $(this)
  69. $table.find('th').each(function (column) {
  70. if ($(this).is('.filter-column')) {
  71. var $filters = $('
  72. <div class="filters">
  73. <h3>Filter by ' +
  74. $(this).text() + ':</h3>
  75. </div>
  76. ')
  77. var keywords = {}
  78. $table.find('tbody tr td').filter(':nth-child(' + (column +
  79. 1) + ')').each(function() {
  80. keywords[$(this).text()] = $(this).text()
  81. })
  82. $('
  83. <div class="filter">all</div>
  84. ').click(function() {
  85. $table.find('tbody tr').removeClass('filtered')
  86. .not('.collapsed').show()
  87. $(this).addClass('active').siblings().removeClass('active')
  88. $table.trigger('stripe')
  89. }).addClass('clickable active').appendTo($filters)
  90. $.each(keywords, function (index, keyword) {
  91. $('
  92. <div class="filter"></div>
  93. ').text(keyword).bind('click',
  94. {'keyword': keyword}, function(event) {
  95. $table.find('tbody tr').each(function() {
  96. if ($('td', this).filter(':nth-child(' + (column + 1)
  97. + ')').text() == event.data['keyword']) {
  98. $(this).removeClass('filtered').not('.collapsed')
  99. .show()
  100. }
  101. else if ($('th',this).length == 0) {
  102. $(this).addClass('filtered').hide()
  103. }
  104. })
  105. $(this).addClass('active').siblings().removeClass(
  106. 'active')
  107. $table.trigger('stripe')
  108. }).addClass('clickable').appendTo($filters)
  109. })
  110. $filters.insertBefore($table)
  111. }
  112. })
  113. })
  114. })
  115. $(document).ready(function() {
  116. var toggleMinus = '../icons/bullet_toggle_minus.png'
  117. var togglePlus = '../icons/bullet_toggle_plus.png'
  118. var $subHead = $('tbody th:first-child')
  119. $subHead.prepend('<img alt="collapse
  120. this section" src="' + toggleMinus + '" />')
  121. $('img', $subHead).addClass('clickable')
  122. .click(function() {
  123. var toggleSrc = $(this).attr('src')
  124. if ( toggleSrc == toggleMinus ) {
  125. $(this).attr('src', togglePlus)
  126. .parents('tr').siblings().addClass('collapsed').fadeOut('fast')
  127. } else {
  128. $(this).attr('src', toggleMinus)
  129. .parents('tr').siblings().removeClass('collapsed')
  130. .not('.filtered').show().fadeIn('fast')
  131. }
  132. })
  133. })
Copyright exforsys.com


 


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.



Read Next: jQuery Reference



 

 

Comments


macera perest said:

  how does this code work?
I can not achieve.
Can you send me the code which includes html too?
January 3, 2009, 8:38 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape