Solved

Trying to get jquery style accordian effect to work inside an html table

Posted on 2013-06-06
4
743 Views
Last Modified: 2013-06-10
I'm trying to get the jquery style accordian option to work inside an html table.  I have 2 header rows that won't change and then I have a dynamic html table that gets built. I have included some html code and javascript code I have as an example:

This is the output example
I would like to be able to hide/show first 2 rows after 'This is the header' is clicked and show/hide 3 rows after 'This is another header' is clicked

Main Header                              // static first header
header 1      header 2      header 3    // static second header
This is the header                      // first accordian header              
Research      Description      Partner
Research      Description      Partner
This is the another header         // second accordian header
Research_2      Description_2      Partner_2
Research_2      Description_2      Partner_2
Research_2      Description_2      Partner_2

<table class="research">
    <tbody>
        
        <tr><td colspan="3">Main Header</td></tr>
        <tr><td>header 1</td><td>header 2</td><td>header 3</td></tr>               
        
        <tr class="accordion">
            <td colspan="3">This is the header</td>
        </tr>
        <tr>
            <td>Research</td>
            <td>Description</td>
            <td>Partner</td>
        </tr>
        <tr>
            <td>Research</td>
            <td>Description</td>
            <td>Partner</td>
        </tr>  
        
        
        <tr class="accordion">
            <td colspan="3">This is another header</td>
        </tr>
        <tr>
            <td>Research_2</td>
            <td>Description_2</td>
            <td>Partner_2</td>
        </tr>
        <tr>
            <td>Research_2</td>
            <td>Description_2</td>
            <td>Partner_2</td>
        </tr>        
        <tr>
            <td>Research_2</td>
            <td>Description_2</td>
            <td>Partner_2</td>
        </tr>        

    </tbody>
</table>

$(function() {
    var $research = $('.research');
    $research.find("tr").not('.accordion').hide();
    $research.find("tr").eq(0).show();
    
    $research.find(".accordion").click(function(){
        $(this).siblings("tr").fadeToggle(500);
    }).eq(0).trigger('click');
});

Open in new window

0
Comment
Question by:dmalovich
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
ID: 39228117
Here is your solution:

http://jsfiddle.net/PCpe4/
0
 

Author Closing Comment

by:dmalovich
ID: 39229019
Perfect. Thanks....
0
 

Author Comment

by:dmalovich
ID: 39229212
Is there any way to default the tabs to close when the page opens and have the user choose to open up a tab.  Sorry for asking after I closed the question.....
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 39233978
Yes this is possible:

just add below line in document.ready event

$('#table1 tr.accordion:eq(1)').nextUntil('tr.accordion').hide();

This will hide second content panel on page load.
you can change this index from 1 to 0 (zero).

Hope this helps you.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now