最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Collapse HTML table rows by default - Stack Overflow

programmeradmin3浏览0评论

I'm using slight modification from this answer to make header rows collapsible in an HTML table.

I want to modify the JS snippet so that rows are collapsed rather than expanded by default (on page load/document ready). How can I do this?

Basically what I'm trying to achieve is for:

$(this).find('span').text(function(_, value){return value=='-'?'+':'-'});
$(this).nextUntil('tr.header').slideToggle(100, function(){

to be run once at page load/refresh, in addition to when header rows are clicked and toggled.

I know that I'll need to change the two tags two <span>+</span> in HTML, but am stuck on how to trigger this behavior on page load.

$(document).ready(function() {
  $('tr.header').click(function() {
    $(this).find('span').text(function(_, value) {
      return value == '-' ? '+' : '-'
    });
    $(this).nextUntil('tr.header').slideToggle(100, function() {});
  });
});
table, tr, td, th {
  border: 1px solid black;
  border-collapse: collapse;
}

tr.header {
  cursor: pointer;
}
<script src=".7.1/jquery.min.js"></script>
<table border="0">
  <tr class="header">
    <th colspan="2">Header <span>-</span></th>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr class="header">
    <th colspan="2">Header <span>-</span></th>
  </tr>
  <tr>
    <td>date</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
</table>

I'm using slight modification from this answer to make header rows collapsible in an HTML table.

I want to modify the JS snippet so that rows are collapsed rather than expanded by default (on page load/document ready). How can I do this?

Basically what I'm trying to achieve is for:

$(this).find('span').text(function(_, value){return value=='-'?'+':'-'});
$(this).nextUntil('tr.header').slideToggle(100, function(){

to be run once at page load/refresh, in addition to when header rows are clicked and toggled.

I know that I'll need to change the two tags two <span>+</span> in HTML, but am stuck on how to trigger this behavior on page load.

$(document).ready(function() {
  $('tr.header').click(function() {
    $(this).find('span').text(function(_, value) {
      return value == '-' ? '+' : '-'
    });
    $(this).nextUntil('tr.header').slideToggle(100, function() {});
  });
});
table, tr, td, th {
  border: 1px solid black;
  border-collapse: collapse;
}

tr.header {
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<table border="0">
  <tr class="header">
    <th colspan="2">Header <span>-</span></th>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr class="header">
    <th colspan="2">Header <span>-</span></th>
  </tr>
  <tr>
    <td>date</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
</table>

Share Improve this question edited May 17, 2024 at 14:18 Mr. Polywhirl 48.7k12 gold badges93 silver badges144 bronze badges asked Mar 29, 2019 at 13:51 Brad SolomonBrad Solomon 40.9k39 gold badges161 silver badges256 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I just update your code with few JS changes, I hope it'll help you out. Thanks

Add below JS code before your click method

$('tr.header').nextUntil('tr.header').slideToggle(100);

$(document).ready(function(){
    $('tr.header').nextUntil('tr.header').slideToggle(100);
    $('tr.header').click(function(){
        $(this).find('span').text(function(_, value){return value=='-'?'+':'-'});
        $(this).nextUntil('tr.header').slideToggle(100, function(){
        });
    });
});
table, tr, td, th {
    border: 1px solid black;
    border-collapse:collapse;
}
tr.header {
    cursor:pointer;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="0">
  <tr  class="header">
      <th colspan="2">Header <span>-</span></th>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr  class="header">
    <th colspan="2">Header <span>-</span></th>
  </tr>
  <tr>
    <td>date</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
</table>

Another way you can achieve this functionality by using CSS

$(document).ready(function(){
    $('tr.header').click(function(){
        $(this).find('span').text(function(_, value){return value=='-'?'+':'-'});
        $(this).nextUntil('tr.header').slideToggle(100, function(){
        });
    });
});
table {
  border-collapse: collapse;
}

tr {
  border: 1px solid #000;
  display: none;
}

td + td {
  border-left: 1px solid #000;
}

tr + tr {
  border-top: 0;
}

tr.header {
  cursor: pointer;
  display: block;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="0">
  <tr  class="header">
      <th colspan="2">Header <span>-</span></th>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr  class="header">
    <th colspan="2">Header <span>-</span></th>
  </tr>
  <tr>
    <td>date</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
</table>

Seems like you'd just do this in document.ready:

$('tr:not(.header)').hide();

Demo

Also, I'd probably toggle visibility of your plus and minus characters with a class on the row and CSS rather than cluttering up your script with that.

发布评论

评论列表(0)

  1. 暂无评论