简体   繁体   English

显示带有从 react.js 中的表格元素带来的信息的弹出窗口

[英]Show popup with info brought from a table element in react.js

I'm developing an app in React.JS and I have to display information that further details the selected element of a table.我正在用 React.JS 开发一个应用程序,我必须显示进一步详细说明表中选定元素的信息。

I share an image as an explanation so that it can be understood or what I need to do.我分享一张图片作为解释,以便可以理解或我需要做什么。

在此处输入图像描述

How can I do it?我该怎么做? I understand that you have to pass variables to the popup so that they are taken and displayed according to the selected element.我知道您必须将变量传递给弹出窗口,以便根据所选元素获取和显示它们。

For this use case, create a local state in the component and update the details on click of the selected record and pass the state to the modal.对于这个用例,在组件中创建一个本地 state 并在单击所选记录时更新详细信息并将 state 传递给模态。

Here I created a selectedData state for populating the selected records and updating this state when onClick of more details in the table and also passing them as props to the modal so that it shows up in the UI.在这里,我创建了一个selectedData state 用于填充选定的记录并在 onClick 时更新此 state,并在表格中显示more details ,并将它们作为 UI 传递给模态。

Complete Code:-完整代码:-

import React, { useState } from "react";
import "./styles.css";
import "bootstrap/dist/css/bootstrap.min.css";

const data = [
  {
    id: 1001,
    firstname: "Mark",
    lastname: "Otto",
    age: 34,
    location: "London",
    address: "10 Downing Street"
  },
  {
    id: 1002,
    firstname: "Jacob",
    lastname: "Jacob",
    age: 34,
    location: "India",
    address: "#110 broad Street"
  }
];

export default function App() {
  const [show, setShow] = useState(false);
  const [selectedData, setSelectedData] = useState({});
  const hanldeClick = (selectedRec) => {
    setSelectedData(selectedRec);
    setShow(true);
  };

  const hideModal = () => {
    setShow(false);
  };

  return (
    <div className="App">
      <table class="table">
        <thead>
          <tr>
            <th scope="col">Id</th>
            <th scope="col">First</th>
            <th scope="col">Last</th>
            <th scope="col">Location</th>
            <th scope="col">Show More</th>
          </tr>
        </thead>
        <tbody>
          {data.map((v) => (
            <tr>
              <td>{v.id}</td>
              <td>{v.firstname}</td>
              <td>{v.lastname}</td>
              <td>@{v.location}</td>
              <td>
                <a href="#" onClick={() => hanldeClick(v)}>
                  More details
                </a>
              </td>
            </tr>
          ))}
        </tbody>
      </table>
      {show && <Modal details={selectedData} handleClose={hideModal} />}
    </div>
  );
}

const Modal = ({ handleClose, details }) => {

  return (
    <div className="modal display-block">
      <section className="modal-main">
        <div className="App">
          <table class="table">
            <thead>
              <tr>
                <th scope="col">Id</th>
                <th scope="col">First</th>
                <th scope="col">Last</th>
                <th scope="col">Age</th>
                <th scope="col">Location</th>
                <th scope="col">Address</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>{details?.id}</td>
                <td>{details?.firstname}</td>
                <td>{details?.lastname}</td>
                <td>{details?.age}</td>
                <td>{details?.location}</td>
                <td>{details?.address}</td>
              </tr>
            </tbody>
          </table>
        </div>
        <button onClick={handleClose}>close</button>
      </section>
    </div>
  );
};

Working Demo - https://codesandbox.io/s/awesome-lamarr-hy8hu?file=/src/App.js:0-2625工作演示 - https://codesandbox.io/s/awesome-lamarr-hy8hu?file=/src/App.js:0-2625

Hope you are looking for the same use case.希望您正在寻找相同的用例。 Let me know if you are facing any issue.如果您遇到任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM