Solved

Expand & Collapse Table Jquery

Posted on 2013-01-16
6
1,615 Views
Last Modified: 2013-01-22
Team,
I want to perform 2 separate actions.
1. expand n collapse a  table rows
2. expand a row in a table

I have the below script that works perfectly for expanding all rows at a time.
click on the div tag:
<div class="txtr mb">
 <a href="javascript:void(0)" class="jq-expand-all-new addLink" onclick="onExpandAll();">
  <span class="icon"></span>
  <span>Expand All</span>
</a>
</div>


table:
<table>
<th>
</th>
<tbody>
<tr>
<td>
<div id="plan${count.count}" class="hd">
<a class="collapsed rel" href="javascript:void(0);"><span class="icon"> </span>
${plan.description}&#9;  
</a>
</div>
</td>
</tr>
<tr>
<td>
&#9;<table>
&#9;<tbody>
&#9;<tr>
&#9;&#9;<td>
&#9;&#9;</td>
&#9;</tr>
&#9;<tr>
&#9;&#9;<td>
&#9;&#9;</td>
&#9;</tr>
&#9;<tr>
&#9;&#9;<td>
&#9;&#9;</td>
&#9;</tr>
&#9;</tbody>
&#9;</table>
</td>
</tr>
</tbody>
</table>



function onExpandAll()
{
&#9;var $ele = $( '.jq-expand-all-new' );
&#9;// var $anccollapsed = $('#activelist').find('tbody tr td:first div.hd a');
&#9;if( $ele.hasClass( 'addLink' ) )
&#9;{
&#9;&#9;$( "#completeHistoryTable tr" ).each( function()
&#9;&#9;{
&#9;&#9;&#9;$( this ).find( 'td:first div.hd a' ).removeClass( 'collapsed' ).addClass( "expanded" );&#9;
&#9;&#9;&#9;$( this ).find( 'td:first div.hd a' ).closest( "tr" ).next().find( "td" ).removeClass( 'closed' );
&#9;&#9;} );
&#9;&#9;$ele.removeClass( 'addLink' ).addClass( 'minusLink' );
&#9;&#9;$ele.children( ':nth-child(2)' ).text( 'Collapse All' );
&#9;}
&#9;else if( $ele.hasClass( 'minusLink' ) )
&#9;{
&#9;&#9;$( "#completeHistoryTable tr" ).each( function()
&#9;&#9;{
&#9;&#9;&#9;$( this ).find( 'td:first div.hd a' ).removeClass( 'expanded' ).addClass( "collapsed" );
&#9;&#9;&#9;$( this ).find( 'td:first div.hd a' ).closest( "tr" ).next().find( "td" ).addClass( 'closed' );
&#9;&#9;} );
&#9;&#9;$ele.removeClass( 'minusLink' ).addClass( 'addLink' );
&#9;&#9;$ele.children( ':nth-child(2)' ).text( 'Expand All' );
&#9;}
}

However when i try to expand the individual rows , unable to achieve the expand functionality. not sure where am I missing .

please help.

Thanks
0
Comment
Question by:Easwaran Paramasivam
  • 2
6 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38785418
I have the below script that works perfectly for expanding all rows at a time.

If you say so. Kind of difficult to test without the css definitions for expanded, collapsed, addLink, minusLink, hd, closed, etc. The table has no content or classes so it has no height to expand into.

Assuming all this is in place, what will trigger the expanding of individual rows?
0
 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 38787454
Adding the sample table template for clear understanding
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 38795574
First you need to start with a proper table.  If you are generating this from php/asp then watch your code for building the table.  http://www.w3schools.com/html/html_tables.asp

Get all your tables correct.  Next use firebug or chrome/sarfari console to check for errors in your js.  Also paste your rendered html into http://validator.w3.org/#validate_by_input.   If after you find no errors in your js and good validation  and you still get errors, please give us a link to your page or post the rendered html, css and js and let's take a look.  

<table width="500">
  <tr>
    <th>This is a heading </th>
</tr>
  <tbody>
    <tr>
      <td><div id="plan${count.count}" class="hd"> <a class="collapsed rel" href="javascript:void(0);"><span class="icon"> </span> ${plan.description} </a> </div></td>
    </tr>
    <tr>
      <td><table>
          <tbody>
            <tr>
              <td>stuff</td>
            </tr>
            <tr>
              <td>stuff</td>
            </tr>
            <tr>
              <td>stuff</td>
            </tr>
          </tbody>
        </table></td>
    </tr>
  </tbody>
</table>

Open in new window

0
 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 38805875
Hi there,
Thanks for your tip on validator check. my table structure got passed on W3c standards.
However there was a JS file import was missing as part of the check. The import was working fine with my local and dev stream was not working and later i fixed the relative path.

Thanks for your help.

Thanks Experts admin team for your support

Regards,
Easwar
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

895 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

14 Experts available now in Live!

Get 1:1 Help Now