Solved

Expand & Collapse Table Jquery

Posted on 2013-01-16
6
1,597 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

13 Experts available now in Live!

Get 1:1 Help Now