Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a table row expand and close? [closed]

Is it possible to make a table row expand and collapse? Can anyone refer me to a script or an example?

I prefer jQuery if possible. I have a drawing concept I would like to achieve:

alt text

like image 361
Erik Avatar asked Oct 09 '10 18:10

Erik


People also ask

How do you make an expandable table in HTML?

The expandable table can be achieved by using JavaScript with HTML. By Clicking on a row of the table, it expands and a sub-table pops up. When the user again clicks on that row the content will hide. This can be very useful when the data is complex but it is inter-related.

How do you fix a row at the top of the table?

To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.


2 Answers

Yes, a table row can slide up and down, but it's ugly since it changes the shape of the table and makes everything jump. Instead, put an element in each td, something that makes sense like a p or h2 etc.

As for implementing a table slide toggle...

It's probably simplest to put the click handler on the entire table, .stopPropagation() and check what was clicked.

If a td in a row with a colspan is clicked, close the p in it. If it's not a td in a row with a colspan, then close then toggle the following row's p.

It is essential to wrap all your written content in an element inside the tds, since you never want to slideUp a td or tr or table shape will change!

Something like:

$(function() {          // Initially hide toggleable content     $("td[colspan=3]").find("p").hide();        // Click handler on entire table     $("table").click(function(event) {            // No bubbling up         event.stopPropagation();          var $target = $(event.target);            // Open and close the appropriate thing         if ( $target.closest("td").attr("colspan") > 1 ) {             $target.slideUp();         } else {             $target.closest("tr").next().find("p").slideToggle();         }                         }); });​ 

Try it out with this jsFiddle example.

... and try out this jsFiddle showing implementation of a + - - toggle.

The HTML just has to have alternating rows of several tds and then a row with a td of a colspan greater than 1. You can obviously adjust the specifics quite easily.

The HTML would look something like:

<table>     <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>     <tr><td colspan="3"><p>Blah blah blah blah blah blah blah.</p>     </td></tr>      <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>     <tr><td colspan="3"><p>Blah blah blah blah blah blah blah.</p>     </td></tr>          <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>     <tr><td colspan="3"><p>Blah blah blah blah blah blah blah.</p>     </td></tr>     </table>​ 
like image 64
Peter Ajtai Avatar answered Sep 21 '22 06:09

Peter Ajtai


You could do it like this:

HTML

<table>     <tr>         <td>Cell 1</td>         <td>Cell 2</td>         <td>Cell 3</td>         <td>Cell 4</td>         <td><a href="#" id="show_1">Show Extra</a></td>     </tr>     <tr>         <td colspan="5">             <div id="extra_1" style="display: none;">                 <br>hidden row                 <br>hidden row                 <br>hidden row             </div>         </td>     </tr> </table> 

jQuery

$("a[id^=show_]").click(function(event) {     $("#extra_" + $(this).attr('id').substr(5)).slideToggle("slow");     event.preventDefault(); }); 

See a demo on JSFiddle

like image 29
Valentin Flachsel Avatar answered Sep 20 '22 06:09

Valentin Flachsel