You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术实现需求:实现Div的隐藏/恢复按钮及工单的单条隐藏与全局批量恢复功能

Solution for Your Frontend Interaction Features

Hey there! Let's break down how to implement both of your requested features step by step.


1. Standalone Div Hide/Restore Button

For a basic hide/show toggle on a single div, vanilla HTML/CSS/JS works perfectly. Here's a complete, self-contained example:

HTML

<button id="toggleBtn">Hide Div</button>
<div id="targetDiv" class="content">
  This is the div we'll hide and restore!
</div>

CSS

.content {
  padding: 20px;
  background: #f0f0f0;
  margin-top: 10px;
  border-radius: 4px;
}

.hidden {
  display: none;
}

JavaScript

const toggleBtn = document.getElementById('toggleBtn');
const targetDiv = document.getElementById('targetDiv');

toggleBtn.addEventListener('click', () => {
  targetDiv.classList.toggle('hidden');
  // Update button text to match current action
  toggleBtn.textContent = targetDiv.classList.contains('hidden') 
    ? 'Restore Div' 
    : 'Hide Div';
});

This uses a CSS class to toggle visibility and updates the button text dynamically to reflect what it will do next.


2. React Ticket Hide/Global Restore Implementation

For your ticket list, we'll use React state to track hidden tickets and add the required buttons. Here's how to modify your existing code:

Step 1: Set Up State and Handlers

First, import useState and add state to track hidden ticket IDs:

import { useState } from 'react';

function TicketList({ filteredTickets }) {
  // Use a Set for efficient lookups of hidden ticket IDs
  const [hiddenTickets, setHiddenTickets] = useState(new Set());

  // Hide a single ticket by adding its ID to the hidden set
  const handleHideTicket = (ticketId) => {
    setHiddenTickets(prev => new Set(prev).add(ticketId));
  };

  // Restore all hidden tickets by clearing the set
  const handleRestoreAll = () => {
    setHiddenTickets(new Set());
  };

  // Filter out hidden tickets before rendering
  const visibleTickets = filteredTickets.filter(ticket => !hiddenTickets.has(ticket.id));

  // Rest of your component code below...
}

Step 2: Update the JSX

Modify your return statement to include the global restore button and per-ticket hide buttons:

return (
  <div className="ticket-container">
    {/* Global Restore Button (only shows when there are hidden tickets) */}
    {hiddenTickets.size > 0 && (
      <button onClick={handleRestoreAll} className="restore-all-btn">
        Restore All Hidden Tickets
      </button>
    )}

    <ul className="tickets">
      {visibleTickets.map((ticket) => (
        <li key={ticket.id} className="ticket">
          <h5 className="headline">{ticket.headline}</h5>
          <p className="text">{ticket.text}</p>
          <footer style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <div className="data">
              By {ticket.address} | {new Date(ticket.time).toLocaleString()}
            </div>
            {/* Per-Ticket Hide Button */}
            <button 
              onClick={() => handleHideTicket(ticket.id)} 
              className="hide-ticket-btn"
            >
              Hide Ticket
            </button>
          </footer>
        </li>
      ))}
    </ul>
  </div>
);

Step 3: Optional Styling for Buttons

Add some basic CSS to make the buttons look polished:

.restore-all-btn {
  margin: 10px 0;
  padding: 8px 16px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.2s;
}

.restore-all-btn:hover {
  background: #45a049;
}

.hide-ticket-btn {
  padding: 4px 8px;
  background: #f44336;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9em;
  transition: background 0.2s;
}

.hide-ticket-btn:hover {
  background: #da190b;
}

How It Works:

  • We use a Set to track hidden ticket IDs for fast visibility checks.
  • The handleHideTicket function adds a ticket's ID to the hidden set (using a new Set to maintain React state immutability).
  • The handleRestoreAll function clears the hidden set, making all tickets visible again.
  • We filter the ticket list to only render tickets not marked as hidden.
  • The global restore button only appears when there are hidden tickets to avoid unnecessary UI elements.

内容的提问来源于stack exchange,提问作者daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:29:06