20. July 2009 by Janko in Tutorials | tags:

jExpand is ultra lightweight jQuery plugin that will make your tables expandable. Typical for line of business applications, this feature can help you organize tables better. This way, tables can hold more information such as images, lists, diagrams and other elements.

Download jExpand with samples View demo

How does it work?

To have such expandable table we have to reserve two rows for each entity, one that will be master row and another that will show details. Details rows are toggled by clicking on master row.

Master row can contain as many columns as it is needed. The important thing is that details row have to span (using colspan attribute) all the columns that master row contains (if you want to have full details row area, of course). In the example below, table contains 4 columns and details rows spans exactly 4 columns.

<table id="report">
    <tr>
        <th>Lorem ipsum</th>
        <th>Lorem ipsum</th>
        <th>Lorem ipsum</th>
        <th>Lorem ipsum</th>
    </tr>
    <tr>
        <td>Lorem ipsum</td>
        <td>Lorem ipsum</td>
        <td>Lorem ipsum</td>
        <td>Lorem ipsum</td>
    </tr>
    <tr>
        <td colspan="4">
            <!-- Details here -->  
        </td>
    </tr>
    <tr>
        <td>Lorem ipsum</td>
        <td>Lorem ipsum</td>
        <td>Lorem ipsum</td>
        <td>Lorem ipsum</td>
    </tr>
    <tr>
        <td colspan="4">
            <!-- Details here -->  
        </td>
    </tr>
</table>

The code that turns this into expandable table is quite simple. First, it will add .master class to each odd row in the table and hide all rows that don't have .master class (those are details rows). It will then "unhide" table header that is first in the array of table rows. The second step is to add click handler to all rows that have .master class. By clicking on any master row, jQuery will toggle the visibility of related details row. Couldn't be simpler, I think.

$("#report tr:odd").addClass("master");
$("#report tr:not(.master)").hide();
$("#report tr:first-child").show();
$("#report tr.master").click(function(){
    $(this).next("tr").toggle();
    $(this).find(".arrow").toggleClass("up");
}); 

There is one more line of code here which adds "odd" CSS class to each odd table row. The code is fully functional even without this line, but I added it to make table styling simpler. This way it is easy to style each master row.

At first I tried with $("td[colspan]") and $("td:not([colspan])") selectors to do the same, but that didn't work in IE. The code was simpler and additional classes wasn't needed. Shame. I tested this version in FF, IE (all versions), Chrome, Safari and Opera and it works fine in all browsers. Except IE8 - details rows aren't shown. It seems as if IE8 have some problems with .toggle() function. To make it work in this browser as well you'll have to check for visibility and then use show() or hide() functions. Shame, again.

At the end, to use plugin that is located in source files just do this:

$(#sometable").jExpand(); 
How do you deal with cases like this? Do you use tables or you have some other approach?  

If you liked this article why don't you share it:

Expand table rows with jQuery - jExpand plugin (via @jankowarpspeed) Share this on StumbleUpon Share this on delicious Share this on Digg Share this on Dzone Share this on DesignBump Send this to friend

Comments

Pingbacks and trackbacks

  1. Trackback from progg.ru Расширяемые таблицы jQuery
  2. Pingback from designm.ag Expand table rows with jQuery – jExpand plugin
  3. Pingback from tripwiremagazine.com 60+ Must Have jQuery Toolbox | tripwire magazine
  4. Pingback from kygeek.com Daily Links for Tuesday, July 21th, 2009
  5. Pingback from yloz.eu jquery expand plugin | Yloz féle zacc
  6. Trackback from Sanjeev Agarwal Daily tech links for .net and related technologies - July 22-24, 2009
  7. Trackback from geff zhang jQuery和asp.net mvc相关资源链接
  8. Pingback from wcs.wayne.edu Wayne State Web Communications Blog » Blog Archive » [Friday Links] The eduWeb Edition
  9. Pingback from tripwiremagazine.com 95+ Exceptionally Useful jQuery Plugins, Tutorials and Cheat Sheets | tripwire magazine
  10. Trackback from PimpThisBlog.com Expand table rows with jQuery - jExpand plugin
  11. Pingback from noupe.com 35 More Useful jQuery Plugins and Techniques « Noupe
  12. Pingback from denbagus.net Amazing JQuery tables | denbagus blog
  13. Pingback from maestrosdelweb.com Comparación de Frameworks en Javascript
  14. Pingback from webdesignledger.com 12 Useful jQuery Plugins for Working with Tables | Web Design Ledger
  15. Pingback from 44crosbyrow.com Expand table rows with jQuery – jExpand plugin
  16. Pingback from meshdiary.co.cc 12 Useful jQuery Plugins for Working with Tables | meshdairy
  17. Pingback from nethobo.com 12个实用的jQuery表格插件 - Hobo's Blog
  18. Pingback from 9tricks.com 12 Useful jQuery Plugins for Working with Tables | 9Tricks.Com - Tips - Tricks - Tutorials
  19. Pingback from huibit05.com 12 Useful jQuery Plugins for Working with Tables | Master Design
  20. Pingback from webdesignledger.com The Best jQuery Plugins of 2009 | Web Design Ledger
  21. Pingback from neurosoftware.ro The Best jQuery Plugins of 2009 | Programming Blog
  22. Pingback from chamli.com Chamli Tennakoon :: Art Director | Graphic Designer | Web Designer | Branding | Corporate ID | Package Design | in Orange County » The Best jQuery Plugins of 2009
  23. Pingback from arthacker.org 2009年最佳jQuery插件 | arthacker
  24. Pingback from fatkun.com 2009 年度最佳 jQuery 插件 | Fatkun's Blog
  25. Pingback from wsria.cn 推荐:2009 年度最佳 jQuery 插件 | what is the RIA? Java + Javascript + jQuery = 3J
  26. Pingback from coral.wordpress.com.cn The roses blooming » Blog Archive » 2009 年度最佳 jQuery 插件
  27. Pingback from juhang.net.cn 2009 年度最佳 jQuery 插件 - 聚航工作室
  28. Pingback from learningjquery.org 2009 年度最佳 jQuery 插件 | Learning jQuery
  29. Pingback from iestew.com 2009 年度最佳 jQuery 插件 « 持家 治国 平天下
  30. Pingback from kiya.cn 2009 年度最佳 jQuery 插件 - 奇亚 Kiya's Blog
  31. Pingback from jssay.com JSSAY » 2009 年度最佳 jQuery 插件(转)
  32. Pingback from fangleo.cn 年度最佳 jQuery 插件 - 关注Web前端开发的技术博客
  33. Pingback from neery.org 2009 年度最佳 jQuery 插件 « it is the Future !
  34. Pingback from yeepal.com 2009 年度最佳 jQuery 插件 | 易贝
  35. Pingback from warezrar.com The Best jQuery Plugins of 2009 | Warez Planet
  36. Pingback from dw10.com 2009 年度最佳 jQuery 插件 » 网页设计室
  37. Pingback from genepeng.cn 2009最好的jQuery Plugins[插件] « 狂奔的蜗牛和疯狂的兔子
  38. Pingback from warezrdx.com The Best jQuery Plugins of 2009 | Software Country
  39. Pingback from cq-cser.cn The Best jQuery Plugins of 2009 | CQ-CSER
  40. Pingback from helloyi.cn 2009 年度最佳 jQuery 插件 | 好易阁|Web技术|杀毒软件|免费资源
  41. Pingback from jennal.cn 2009 年度最佳 jQuery 插件 - Jennal’s Blog
  42. Pingback from haicharlie143.wordpress.com my fav links « Haicharlie143's Blog
  43. Pingback from vinaphoto.wordpress.com 25+ Highly Useful jQuery Plugins Bringing Life back to HTML Tables | tripwire magazine « Photoshop.vn – your photoshop resources
  44. Pingback from afiffattouh.com 25+ Highly Useful jQuery Plugins Bringing Life back to HTML Tables | Afif Fattouh - Web Specialist
  45. Pingback from cnet.ro Tabele interactive cu jQuery | CNET.ro
  46. Pingback from vinayindxb.wordpress.com 12 Useful jQuery Plugins for Working with Tables « Vinayindxb
  47. Pingback from blog.almacenplantillasweb.es Plugins jQuery para manejo de tablas HTML | Diseño web, accesibilidad, usabilidad, posicionamiento y optimización web - AlmacenPlantillasWeb Blog
  48. Pingback from bjnahan.net 2009 年度最佳 jQuery 插件 » 呐喊分享 » 呐喊网络部落格
  49. Pingback from yybean.com 2009年度最佳 jQuery 插件 » yybean.com
  50. Pingback from blog.gaoso.com.cn 2009 年度最佳 jQuery 插件 | 高手网
  51. Pingback from yousilin.wordpress.com 2009 年度最佳 jQuery 插件 « Scott McKnight's Blog
  52. Pingback from doremc.com 熊仔阳光站 » 2009 年度最佳 jQuery 插件
  53. Pingback from wphocasi.com 2009 yılının En iyi Jquery Eklentileri » WordPress Hocası
  54. Pingback from blog.54cto.net 2009 年度最佳 jQuery 插件 - 54cto's blog
  55. Pingback from webdesigners.lk Useful jQuery Tables | Web Designers Sri Lanka
  56. Pingback from ttxs.info 2009 年度最佳 jQuery 插件 | 好好学习,天天向上
  57. Pingback from designshack.co.uk 15 Tips for Designing Terrific Tables | Design Shack
  58. Pingback from sitozcan.com 2009 yılının En iyi Jquery Eklentileri | SITOZCAN.COM
  59. Pingback from creativosonline.org jExpand, plugin de jQuery para expandir tablas | Recursos para Diseñadores Gráficos y Web | Creativos Online

Add comment

   
        Country flag
biuquote
Loading