简体   繁体   English

如何使用 ReactJS 在数组中分配数据值

[英]How to assign data values in array using ReactJS

I am new to ReactJS I want to read the excel file and show data in the react project.我是 ReactJS 的新手,我想读取 excel 文件并在 react 项目中显示数据。 To accomplish this I can successfully read data from excel file.为此,我可以成功地从 excel 文件中读取数据。 Now, the problem is I was unable to display data on the react project.现在,问题是我无法在 react 项目上显示数据。 I've search this issue and find that array values are not defined like that...我搜索了这个问题,发现数组值不是那样定义的......

Help me to display the data in the React project.帮我把React项目中的数据展示出来。

 import React from 'react' import { Table } from 'react-bootstrap' import * as XLSX from 'xlsx' import Accordion from './component/accordion' import './App.css' function App() { var items = []; const readExcel=(file) => { const promise = new Promise((resolve, reject)=>{ const fileReader = new FileReader(); fileReader.readAsArrayBuffer(file); fileReader.onload=(e)=>{ const bufferArray = e.target.result; const wb = {SheetNames:[], Sheets:{}}; const ws1 = XLSX.read(bufferArray, {type:"buffer"}).Sheets.Sheet1; const ws2 = XLSX.read(bufferArray, {type:"buffer"}).Sheets.Sheet2; wb.SheetNames.push("Sheet1"); wb.Sheets["Sheet1"] = ws1; wb.SheetNames.push("Sheet2"); wb.Sheets["Sheet2"] = ws2; const data1 = XLSX.utils.sheet_to_json(ws1); const data2 = XLSX.utils.sheet_to_json(ws2); resolve([data1, data2]); }; fileReader.onerror=(error) => { reject(error); }; }); promise.then((excelData) => { items.push(excelData); console.log(excelData); }); }; return( <div className="container-fluid"> <section className="heading"> <h4>Products Details</h4> <input type="file" className="input-field" onChange={(e) =>{ const file=e.target.files[0]; readExcel(file); }} /> </section> {items.map((d) => ( <Accordion title={ <div> <tr key={d.ID} className="btn-heading"> <td>{d.ID}</td> <td>{d.Mail}</td> <td>{d.Name}</td> <td>{d.PhoneNo}</td> <td>{d.City}</td> <td>{d.Date}</td> <td>{d.Time}</td> </tr> </div> } content={ <div> <p className="header"> <span className="header-content">Shipping Address:</span> 292 Naqshband Colony. Near rabbania Mosque. Multan </p> <Table size="sm"> <thead> <tr> <th>#</th> <th>Article No</th> <th>Product Name</th> <th>Quantity</th> <th>Price</th> <th>Total Amount</th> </tr> </thead> <tbody> {items.map((c) =>( <tr key={c.ArticleNo}> <td>{c.ArticleNo}</td> <td>{c.ProductName}</td> <td>{c.Quantity}</td> <td>{c.Price}</td> <td>{c.TotalAmount}</td> </tr> ))} </tbody> </Table> </div> } /> ))} </div> ); } export default App;

Here is the codesandox: https://codesandbox.io/s/tender-tharp-30t8j?file=/src/App.js这是codesandox: https ://codesandbox.io/s/tender-tharp-30t8j ? file =/ src/App.js

Ok, I've tried it and got that error in the console好的,我已经尝试过了,但在控制台中出现了该错误

错误

Here's what you need to do这是你需要做的

  • Move the key to the Accordion, (the component being mapped)移到 Accordion,(被映射的组件)
  • Make sure your excel sheet headers match the key names you're using below确保您的 Excel 工作表标题与您在下面使用的键名匹配

The key needs to be the component right inside of the map.关键需要是地图内部的组件。

Also, the name Sheet1 should be the name of your sheet此外,名称Sheet1应该是您的工作表的名称

{items.map((d) => (
        <Accordion   key={d.ID}   <-- move the key to here, 
          title={
            <div>
              <tr className="btn-heading">
                <td>{d.ID}</td>   <-- These values should exactly match *headers of your excel sheet 
                <td>{d.Mail}</td>
                <td>{d.Name}</td>
                <td>{d.PhoneNo}</td>
                <td>{d.City}</td>
                <td>{d.Date}</td>
                <td>{d.Time}</td>
              </tr>
            </div>
          }
....

If you're still having some trouble, please share an excel sheet with sample data.如果您仍然遇到一些问题,请分享包含示例数据的 Excel 表格。

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

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