How to assign data values in array using ReactJS

Muzamil Hussain

I am new to ReactJS I want to read the excel file and show data in the react project. To accomplish this I can successfully read data from excel file. Now, the problem is I was unable to display data on the react project. 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.

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

mbkfa93

Ok, I've tried it and got that error in the console

error

Here's what you need to do

  • Move the key to the Accordion, (the component being mapped)
  • Make sure your excel sheet headers match the key names you're using below

The key needs to be the component right inside of the map.

Also, the name Sheet1 should be the name of your sheet

{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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to count specific array values and assign the values in a table data

how to assign variables to an object for submitting the Data using reactjs?

How to assign values to an array by using an array passed in a constructor?

how to loop through data and assign values to state array in react

How can I assign values to an array in a constructor using Typescript?

How to access array after using for loop to assign values

how to assign the values to a contenteditable div in reactjs

How to assign values to array in object

How to assign values to a void array

How to assign values to the elements of an array?

Using a numpy array to assign values to another array

how to remove duplicate values from Array using reactjs?

How to assign indexed array values to multidimensional array?

ReactJS assign values dynamic 2D array in componentWillMount

How to access data from an item in an array using reactjs state?

Assign array values in R using function of indices

Pandas assign column values using array

Assign values to numpy array using row indices

Efficiently using Numpy to assign function values to array

Using array values to assign button labels in HTML

How do you assign array values to class member array in constructor, without using the std library?

ReactJS set state does not assign array from Axios response data

How to map an array of data in reactjs?

How to sort Array Data in ReactJs

How to assign values to Nested Json array

How to assign values in an array of arrays properly?

How to assign values to array with linq correctly?

How to assign series of values to an array in php?

How can assign values to a array after initialization

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive