Solved

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

Posted on 2013-06-06
4
759 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

770 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