简体   繁体   English

如何在模态中显示选定的行

[英]How to show the selected row in the modal

Goal:目标:
When you press on the button, the first and last name from the selected row shall display in the modal.当您按下按钮时,所选行中的名字和姓氏将显示在模式中。 You should enable to reuse the same modal's content and code.您应该启用重用相同模式的内容和代码。

Problem:问题:
I do not know how to solve it and part am I missing in order to achieve the goal?我不知道如何解决它,为了实现目标我错过了一部分?

Info:信息:
*I'm new in ReactJS *我是 ReactJS 的新手

Stackblitz:堆栈闪电战:
https://stackblitz.com/edit/reactjs-part1-hello-world-xu6up2?file=style.css https://stackblitz.com/edit/reactjs-part1-hello-world-xu6up2?file=style.css


import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';
import Modal from 'react-modal';

class App extends Component {
  constructor() {
    super();

    this.state = {
      modalIsOpen: false
    };

    this.handleOpenModal = this.handleOpenModal.bind(this);
    this.handleCloseModal = this.handleCloseModal.bind(this);
  }

  handleOpenModal() {
    this.setState({ isModalOpen: true });
  }

  handleCloseModal() {
    this.setState({ isModalOpen: false });
  }

  render() {
    return (
      <div>
        <table border="1">
          <thead>
            <tr>
              <th>First Name</th>
              <th>Last Name</th>
              <th />
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Josef</td>
              <td>Andersson</td>
              <td>
                <button onClick={this.handleOpenModal}>Open Modal</button>
              </td>
            </tr>
            <tr>
              <td>Jim</td>
              <td>West</td>
              <td>
                <button onClick={this.handleOpenModal}>Open Modal</button>
              </td>
            </tr>
            <tr>
              <td>Joe</td>
              <td>West</td>
              <td>
                <button onClick={this.handleOpenModal}>Open Modal</button>
              </td>
            </tr>
          </tbody>
        </table>

        <div>
          <Modal className="confirmation-modal" isOpen={this.state.isModalOpen}>
            First Name: <br />
            Last Name: <br />
            <br />
            <button onClick={this.handleCloseModal}>Close Modal</button>
          </Modal>
        </div>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

h1,
p {
  font-family: Lato;
}

.confirmation-overlay.ReactModal__Overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  background-color: rgba(0, 0, 0, 0.6);
}

.confirmation-overlay.ReactModal__Overlay--after-open {
  opacity: 1;

  transition: opacity 300ms ease-out;
}

.confirmation-modal.ReactModal__Content {
  position: absolute;

  top: 25%;
  left: 50%;
  width: 500px;
  right: auto;
  bottom: auto;
  border: 1px solid #eee;
  margin-right: -50%;
  transform: scale(0);
  background-color: #fff;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  outline: none;
  padding: 20px;
  opacity: 0;
}

.confirmation-modal.ReactModal__Content--after-open {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  transition: all 300ms ease-out;
}

.confirmation-modal button {
  border: 1px solid black;
  background-color: #fff;
  color: #333;
  padding: 4px 8px;
  margin: 4px;
  border-radius: 3px;
  cursor: pointer;
}

.confirmation-modal button:hover {
  background-color: #eee;
}

I would recommend actually making the content dynamic.我建议实际上使内容动态化。 That will enable you to easily access that data.这将使您能够轻松访问该数据。

this.state = {
  modalOpenWith: null,
  items: [
    { firstName: 'Josef', lastName: 'Anderson', key: 'josef.anderson' },
    { firstName: 'Jim', lastName: 'West', key: 'jim.west' },
    { firstName: 'Joe', lastName: 'West', key: 'joe.west' }
  ]
};

Then render it with:然后渲染它:

<tbody>
  {items.map(({ firstName, lastName, key }, i) => (
    <tr key={key}>
      <td>{firstName}</td>
      <td>{lastName}</td>
      <td>
        <button onClick={() => this.handleOpenModal(i)}>Open Modal</button>
      </td>
    </tr>
  ))}
</tbody>;

You could also directly store the open item instead of the index.您也可以直接存储打开的项目而不是索引。 That would probably avoid some state errors.这可能会避免一些 state 错误。

Key钥匙

You will now need a key so in case the array changes, react knows which item was removed/added.您现在需要一个密钥,以便在数组更改的情况下,react 知道删除/添加了哪个项目。 This key has to be unique in the array.该键在数组中必须是唯一的。 So in that case it sounds likely that you would have a user id of some sort, but I opted to add a key property to the array that resembles a username to login (to whatever).因此,在这种情况下,您可能会拥有某种类型的用户 id,但我选择向数组添加一个类似于登录用户名的键属性(无论什么)。

Read more about keys here: https://reactjs.org/docs/lists-and-keys.html#keys在此处阅读有关密钥的更多信息: https://reactjs.org/docs/lists-and-keys.html#keys

Arrow functions instead of methods箭头函数而不是方法

I also took the liberty to convert your this binds, to attributes with arrow functions.我还冒昧地将您的 this 绑定转换为带有箭头函数的属性。 That way the context is always this.这样,上下文总是这样。 Take a look at it.看看它。

Stackblitz with index: Stackblitz 索引:

https://stackblitz.com/edit/reactjs-part1-hello-world-58yys2?file=index.js https://stackblitz.com/edit/reactjs-part1-hello-world-58yys2?file=index.js

Stackblitz with item: Stackblitz 与项目:

https://stackblitz.com/edit/reactjs-part1-hello-world-27rr7bhttps://stackblitz.com/edit/reactjs-part1-hello-world-27rr7b

Put your users in states:将您的用户置于以下状态:

users: [
    { fname: 'a', lname: 'b' },
    { fname: 'c', lname: 'd' },
    { fname: 'f', lname: 'e' }
  ]

And in your table you can use map, but check if it is set first if your data is from a state:在您的表格中,您可以使用 map,但如果您的数据来自 state,请检查是否先设置:

{this.state.users !== "undefined" &&
          this.state.users.map((user, index) => {
            const { fname, lname } = user; //destructuring
            return (
              <tr key={index}>
                <td>{fname}</td>
                <td>{lname}</td>
                <td>
                  <button
                    onClick={() => this.handleOpenModal(fname, lname)}
                  >
                    Open Modal
                  </button>
                </td>
              </tr>
            );
          })}

https://stackblitz.com/edit/reactjs-part1-hello-world-grvypp?file=index.js https://stackblitz.com/edit/reactjs-part1-hello-world-grvypp?file=index.js

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

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