Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 - how to collapsing and expanding for complicated table elements

enter image description here

Hi, I need a expanding and collapsing function for a table. The code found on line most are for and then in js define a function for this tr class.

But my case is more complicated as seen as picture. It will expand after clicking "Parameter 1" and show a merged cell and 2 cells.

So in this case, how can I realize this function? Thanks in advance.

Attached is a simple snippet for the table for your test:

https://jsfiddle.net/knspgwkq/

html for test table

<table width="200" border="1">
  <tr>
    <td rowspan="5">Summary 1</td>
    <td colspan="2"><div align="center">1 st level</div></td>
  </tr>
  <tr>
    <td colspan="2"><div class="P1">Parameter 1</div></td>
  </tr>
  <tr>
    <td rowspan="2">Sub level-1 </td>
    <td>Sub parameter (1)</td>
  </tr>
  <tr>
    <td>Sub parameter (2)</td>
  </tr>
  <tr>
    <td colspan="2"><div class="P2">Parameter 2</div></td>
  </tr>
</table>

JS

$('.P1').click(function(){
  $(this).find('span').text(function(_, value){return value=='-'?'+':'-'});
  $(this).nextUntil('.P2').slideToggle(100, function(){
  });
});
like image 785
Héléna Avatar asked Dec 16 '15 08:12

Héléna


People also ask

How do you make a collapsible list in HTML?

Close the head portion of the page, with the ending tag for the head of the page (</head>). Create the body of the HTML code. Type the tag for starting the body (<body>). Create the list content, including some HTML styling information for the users browser to use, for them to expand and collapse the list.

How do I add a collapse Button in HTML?

To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element. Then add the data-target="#id" attribute to connect the button with the collapsible content (<div id="demo">).


2 Answers

How about adding your custom data attribute to the cells that you wish to control? Live example https://jsfiddle.net/knspgwkq/7/

Your parent will hold data-collapsable-parent with some key value. And your children that you wish to hide/show with above parent will hold data-collapsable-child with the same key value as parent.

And if you need additional collapsing expanding elements check this example By clicking Parameter 1 then Sub parameter (2) you will open additional element.

https://jsfiddle.net/knspgwkq/9/

$('[data-collapsable-parent]').click(function(){
  var child = $(this).attr("data-collapsable-parent");
  $('[data-collapsable-child="'+ child + '"]').toggle('slow');
});
 [data-collapsable-child] {
   display: none;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="200" border="1">
  <tr>
    <td rowspan="5">Summary 1</td>
    <td colspan="2"><div align="center">1 st level</div></td>
  </tr>
  <tr>
    <td colspan="2" data-collapsable-parent="1"><div class="P1">Parameter 1</div></td>
  </tr>
  <tr>
    <td data-collapsable-child="1" rowspan="2">Sub level-1 </td>
    <td data-collapsable-child="1" >Sub parameter (1)</td>
  </tr>
  <tr>
    <td data-collapsable-child="1" >Sub parameter (2)</td>
  </tr>
  <tr>
    <td colspan="2"><div class="P2">Parameter 2</div></td>
  </tr>
</table>
like image 174
Paran0a Avatar answered Oct 12 '22 05:10

Paran0a


Here's an idea: use two toggles to get the upward collapse effect, and the downward expand effect (without modifying your table structure).

// Basic toggle without animation
//$('.P1').on("click", function() {
//  $(this).closest("tr").nextUntil('.P2').toggle();
//});

// Better sliding toggle
$('.P1').on("click", function() {
  $trs = $(this).closest("tr").nextUntil('.P2');
  $trs.eq(1).find("td").eq(0).slideToggle(100, function() {
    $trs.eq(0).slideToggle(100);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="200" border="1">
  <tr>
    <td rowspan="5">Summary 1</td>
    <td colspan="2">
      <div align="center">1 st level</div>
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <div class="P1">Parameter 1</div>
    </td>
  </tr>
  <tr>
    <td rowspan="2">Sub level-1</td>
    <td>Sub parameter (1)</td>
  </tr>
  <tr>
    <td>Sub parameter (2)</td>
  </tr>
  <tr>
    <td colspan="2">
      <div class="P2">Parameter 2</div>
    </td>
  </tr>
</table>
like image 34
Drakes Avatar answered Oct 12 '22 04:10

Drakes