如何将数据数组插入表

亨利

我在前端有一个表,需要在表中放入一些数据。以下是我要实现的目标

____________________________________________________
| Product Name | Product Number | Product Location |
----------------------------------------------------
|       A      |       123      |      TEXAS       |
----------------------------------------------------
|       B      |       456      |      NEW YORK    |
----------------------------------------------------

但是,我一直在获得下表。

____________________________________________________
| Product Name | Product Number | Product Location |
----------------------------------------------------
|       A      |       123      |       TEXAS      |
                                       NEW YORK
----------------------------------------------------
|       B      |       456      |       TEXAS      |
                                       NEW YORK
----------------------------------------------------

下面是我的带有JSX和渲染功能的代码

    const getTheLocation = () => {
        return (
            productsHistoryLocation.map((productHistory) => (
                <p key={productHistory.product_id}>{productHistory.product_location}</p>
            ))
        )
    }

    const renderProducts = () => {
        return (
            productsData.map((product) => 
            (
                <tr key={product.product_number}>
                        <td>{products.bacs_unit}</td>
                        <td>{products.serial_number}</td>
                        <td>{getTheLocation()}</td>
                </tr>   
            ))
        )
    }
        <Table aria-label="simple table">
                    <TableHead>
                        <TableRow>
                            <TableCell><b>Product Name</b></TableCell>
                            <TableCell><b>Product Number</b></TableCell>
                            <TableCell><b>Product Location</b></TableCell>
                        </TableRow>
                    </TableHead>
                    <TableBody>
                        {renderProducts()}
                    </TableBody>
             </Table>
Praveen Nambiar

问题出在您的功能getTheLocation以及可能的数据结构上。

将getTheLocation更改为

  const getTheLocation = (idx) => {
    const location = productsHistoryLocation.filter(
      (product) => product.product_id === idx
    );
    return <p key={idx}>{location[0].product_location}</p>;
  };

这里检查工作逻辑

检查完整代码:

import "./styles.css";
import {
  Table,
  TableHead,
  TableRow,
  TableCell,
  TableBody
} from "@material-ui/core";

export default function App() {
  const productsData = [
    {
      product_number: 1,
      bacs_unit: "A",
      serial_number: "123"
    },
    {
      product_number: 2,
      bacs_unit: "B",
      serial_number: "456"
    }
  ];

  const productsHistoryLocation = [
    {
      product_id: 1,
      product_location: "TEXAS"
    },
    {
      product_id: 2,
      product_location: "NEW YORK"
    }
  ];

  const getTheLocation = (idx) => {
    const location = productsHistoryLocation.filter(
      (product) => product.product_id === idx
    );
    return <p key={idx}>{location[0].product_location}</p>;
  };

  const renderProducts = () => {
    return productsData.map((product) => (
      <tr key={product.product_number}>
        <td>{product.bacs_unit}</td>
        <td>{product.serial_number}</td>
        <td>{getTheLocation(product.product_number)}</td>
      </tr>
    ));
  };

  return (
    <div className="App">
      <Table aria-label="simple table">
        <TableHead>
          <TableRow>
            <TableCell>
              <b>Product Name</b>
            </TableCell>
            <TableCell>
              <b>Product Number</b>
            </TableCell>
            <TableCell>
              <b>Product Location</b>
            </TableCell>
          </TableRow>
        </TableHead>
        <TableBody>{renderProducts()}</TableBody>
      </Table>
    </div>
  );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将数组值插入表

来自分类Dev

如何将数据插入表中?

来自分类Dev

如何将数据插入mysql表?

来自分类Dev

如何将数据从变量插入数据表

来自分类Dev

如何将数组项插入PostgreSQL表

来自分类Dev

如何将嵌套的JSON数组插入SQL Server表

来自分类Dev

如何将JSON数组插入MySQL数据库

来自分类Dev

如何将内爆数组插入数据库?

来自分类Dev

如何将所有数组数据插入mysql?

来自分类Dev

如何将JSON数组插入MySQL数据库

来自分类Dev

如何将数据作为数组值插入MySQL?

来自分类Dev

如何将xml数据插入SQL Server表?

来自分类Dev

Sequelize-如何将数据插入多个表?

来自分类Dev

如何将数据插入表并获取列的值

来自分类Dev

如何将xml数据插入到表中?

来自分类Dev

如何将UNION数据插入表中

来自分类Dev

如何将数组数据放入表中?

来自分类Dev

如何将MySQL远程数据库表插入本地数据库表?

来自分类Dev

将数组插入数据库表

来自分类Dev

如何将元素插入数组

来自分类Dev

如何将数组的内容插入向量?

来自分类Dev

如何将数组插入不同的行?

来自分类Dev

如何将HTMLTableRowElement插入表主体?

来自分类Dev

如何将值插入联结表?

来自分类Dev

如何将值插入联结表?

来自分类Dev

如何将值插入MySQL表

来自分类Dev

如何将文件插入表中?

来自分类Dev

如何将数组的 $_post 数组分配给变量以插入数据库

来自分类Dev

如何将每个tr的HTML表数据插入到SQL表中