简体   繁体   English

使用 React 和 boostrap 的行信息单击打开模式

[英]Open modal on click with row info with React and boostrap

i just want to open a modal with bootstrap and react.我只想用引导程序打开一个模式并做出反应。 I want to put the row infos into the modal, the modals are in the html but when I click on a row, it trigger just the first modal in the list.我想将行信息放入模态,模态在 html 但是当我点击一行时,它只触发列表中的第一个模态。 I tried to put specific IDs on the modal but it does not work... Someone could help me on this?我试图在模式上放置特定的 ID,但它不起作用......有人可以帮助我吗?

const ProductsList = ({ products }) => {
  return (
    <div className="col-10 offset-2 p-0">
      <nav className="navbar navbar-light bg-light">
        <ul className="navbar-nav ml-auto">
          <li style={{ zIndex: "1" }} className="nav-item active">
            <Link style={{ cursor: "pointer" }} href="/products/add-product">
              <a className="nav-link">
                <i className="fas fa-plus-square"></i> Ajouter un produit
              </a>
            </Link>
          </li>
        </ul>
      </nav>
      <div className="table-responsive-lg">
        <div className="row text-white bg-dark px-4">
          <div className="col-3">ID</div>
          <div className="col-2">Nom du produit</div>
          <div className="col-2">Prix/unité</div>
          <div className="col-2">Stock</div>
          <div className="col-3">Catégorie</div>
        </div>
        {products &&
          products.products.map((product) => {
            return (
              <div
                key={product._id}
                className="row border px-4"
                data-toggle="modal"
                data-target="#exampleModal"
              >
                <div className="col-3 border-right">{product._id}</div>
                <div className="col-2 border-right">
                  {product.productName.charAt(0).toUpperCase() +
                    product.productName.slice(1)}
                </div>
                <div className="col-2 border-right">{product.price} €</div>
                <div className="col-2 border-right">{product.stock}</div>
                <div className="col-3">{product.categorie.categoryName}</div>
                <div
                  className="modal fade"
                  id="exampleModal"
                  tabIndex="-1"
                  aria-hidden="true"
                  role="dialog"
                >
                  <div className="modal-dialog modal-dialog-centered">
                    <div className="modal-content">
                      <div className="modal-header">
                        <h5 className="modal-title">
                          {product.productName}
                        </h5>
                        <button
                          type="button"
                          className="close"
                          data-dismiss="modal"
                          aria-label="Close"
                        >
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div className="modal-body">...</div>
                      <div className="modal-footer">
                        <button
                          type="button"
                          className="btn btn-secondary"
                          data-dismiss="modal"
                        >
                          Close
                        </button>
                        <button type="button" className="btn btn-primary">
                          Save changes
                        </button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            );
          })}
        
      </div>
    </div>
  );
};

export default ProductsList;

I tried with table before but it's the same result.我之前尝试过使用表格,但结果相同。

As you mentioned, your id attribute is a constant ( exampleModal ) and does not vary by row.正如您所提到的,您的id属性是一个常量( exampleModal )并且不会因行而异。 Note that the data-target attribute must also change on your row (the click target).请注意,您的行(点击目标)上的data-target属性也必须更改。

Additionally, as Bootstrap modals require JavaScript to toggle visibility outside of React you may get odd results:此外,由于引导模式需要 JavaScript 来切换 React 之外的可见性,您可能会得到奇怪的结果:

React is unaware of changes made to the DOM outside of React. React 不知道对 React 之外的 DOM 所做的更改。 It determines updates based on its own internal representation, and if the same DOM nodes are manipulated by another library, React gets confused and has no way to recover.它根据自己的内部表示来确定更新,如果相同的 DOM 节点被另一个库操作,React 会感到困惑并且无法恢复。 Source 资源

Consider using a React-Bootstrap library (like reactstrap ) to manage this toggling within React.考虑使用 React-Bootstrap 库(如reactstrap )来管理 React 中的这种切换。

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

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