Solved

Expand & Collapse Table Jquery

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

837 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