Home Web Front-end CSS Tutorial How to Dynamically Expand and Collapse Table Rows with jQuery?

How to Dynamically Expand and Collapse Table Rows with jQuery?

Nov 05, 2024 am 06:09 AM

How to Dynamically Expand and Collapse Table Rows with jQuery?

Expanding and Collapsing Table Rows Dynamically with jQuery

Problem:

Expanding or collapsing table rows when a specific column header is clicked poses a challenge. To simplify this task, a custom CSS class can be used to distinguish rows within each header. However, managing multiple CSS classes for multiple headers can become cumbersome.

Solution:

To avoid the complexity of tracking multiple CSS classes, an alternative approach leverages the nextUntil() method in jQuery. This method retrieves all rows subsequent to the clicked header row, until the next header row is encountered.

Code Snippet:

<code class="javascript">$('.header').click(function(){
    $(this).nextUntil('tr.header').slideToggle(1000);
});</code>
Copy after login

HTML Structure:

<code class="html">&lt;table border=&quot;0&quot;&gt;
  &lt;tr class=&quot;header&quot;&gt;
    &lt;td colspan=&quot;2&quot;&gt;Header&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;data&lt;/td&gt;
    &lt;td&gt;data&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;data&lt;/td&gt;
    &lt;td&gt;data&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;</code>
Copy after login

Example:

In this example, a class "header" is assigned to the header row. When a header row is clicked, the rows immediately below it are toggled between hidden and visible, using the slideToggle() method.

Additional Features:

  • You can use a span element within the header row to display a " " or "-" sign, which toggles after the row is expanded or collapsed:
<code class="javascript">$('.header').click(function(){
    $(this).find('span').text(function(_, value){return value=='-'?'+':'-'});
    $(this).nextUntil('tr.header').slideToggle(100);
});</code>
Copy after login
  • To toggle the span icon/text asynchronously after the toggle is complete, use the promise() of the slideToggle() method:
<code class="javascript">$(this).nextUntil('tr.header').slideToggle(100).promise().done(function () {
    $this.find('span').text(function (_, value) {
        return value == '-' ? '+' : '-'
    });
});</code>
Copy after login
  • Alternatively, use a CSS pseudo element to represent the expansion/collapse sign and toggle a class on the header:
<code class="css">.header .sign:after{
    content:"+";
    display:inline-block;      
}
.header.expand .sign:after{
    content:"-";
}</code>
Copy after login
<code class="javascript">$(this).toggleClass('expand').nextUntil('tr.header').slideToggle(100);</code>
Copy after login

The above is the detailed content of How to Dynamically Expand and Collapse Table Rows with jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework Create a JavaScript Contact Form With the Smart Forms Framework Mar 07, 2025 am 11:33 AM

Create a JavaScript Contact Form With the Smart Forms Framework

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

Working With GraphQL Caching

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

Making Your First Custom Svelte Transition

Demystifying Screen Readers: Accessible Forms & Best Practices Demystifying Screen Readers: Accessible Forms & Best Practices Mar 08, 2025 am 09:45 AM

Demystifying Screen Readers: Accessible Forms & Best Practices

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Mar 04, 2025 am 10:22 AM

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

Show, Don't Tell

What the Heck Are npm Commands? What the Heck Are npm Commands? Mar 15, 2025 am 11:36 AM

What the Heck Are npm Commands?

See all articles