Solved

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

Posted on 2013-06-06
4
774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

733 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