Solved

Expand & Collapse Table Jquery

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

735 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