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

How to Expand and Collapse Table Rows with jQuery?

DDD
Release: 2024-11-03 10:10:03
Original
942 people have browsed it

How to Expand and Collapse Table Rows with jQuery?

Expand and Collapse Table Rows with jQuery

Problem:

Expand and collapse table rows when a specific header column is clicked, limiting the expansion/collapse to rows beneath the clicked header.

Proposed Solution:

  1. Identify the Header Rows: Add a class, such as "header," to the header rows.
  2. Toggle Expansion/Collapse: Use jQuery's .nextUntil() method to select all rows beneath the clicked header until the next header is encountered. Then, use .slideToggle() to toggle the visibility of these rows.

Code Snippet:

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

HTML Structure:

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

Additional Features:

  • Expand/Collapse Icon: Use a span element within the header cells to display an expand or collapse icon. Update the icon based on the current state.
  • Promise Usage: Use jQuery's .promise() method to handle asynchronous operations, such as animation.
  • CSS Pseudo-Element: Utilize a CSS pseudo-element to represent the expansion/collapse indicator and toggle a class on the header to control visibility.

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

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template