使用React.js返回动态表

发布于 2025-02-11 16:27:19 字数 2585 浏览 0 评论 0原文

我试图使用react创建动态表。 这是我到目前为止所拥有的...

datatable.js

import React from 'react'
import data from '../data/customerData.json'
import '../styles/dataTable.css'



const DataTable = () => {
    return (
        <div>
          {data.map(dat => {
            return (
                <div>
                    <table className='data-table-container'>
                        <tr>
                            <th>Customer Name</th>
                            <th>Phone Number</th>
                            <th>Loan Vehicle</th>
                            <th>Time Out</th>
                            <th>Time Due</th>
                        </tr>
                        <tr>
                            <td>{dat.CustomerName}</td>
                            <td>{dat.PhoneNumber}</td>
                            <td>{dat.LoanVehicle}</td>
                            <td>{dat.TimeOut}</td>
                            <td>{dat.TimeDue}</td>
                        </tr>
                    </table>
                </div>
            );
          })}
        </div>
      );
    }

export default DataTable

home.js

import React, { useState } from 'react';
import Calendar from 'react-calendar';
import DataTable from '../components/DataTable';
import '../styles/calendar.css'




const Home = () => {
  const [value, onChange] = useState(new Date());
  let today = new Date().toDateString()
  return (
    <>
      <div className='date-display'>
        <h3 className='current-date-display'>{today}</h3>
      </div>
      <Calendar onChange={onChange} value={value} />
      <DataTable/>
    </>
  )
}

export default Home

这是浏览器中呈现的。

“

因此,当前在我的dataTable.js Component中,我正在使用.mapdata in customerdata.json中循环循环。我遇到的问题是,您可以从屏幕截图IMG中看到&lt; th&gt;标签在每个循环中呈现的标签。 我的问题是如何一次显示&lt; th&gt;标签,并保留循环以显示&lt; td&gt;标签内容。 请参阅下面的预期输出。

“预期输出”

Im trying to create a dynamic table using react.
Here is what I have so far...

DataTable.js

import React from 'react'
import data from '../data/customerData.json'
import '../styles/dataTable.css'



const DataTable = () => {
    return (
        <div>
          {data.map(dat => {
            return (
                <div>
                    <table className='data-table-container'>
                        <tr>
                            <th>Customer Name</th>
                            <th>Phone Number</th>
                            <th>Loan Vehicle</th>
                            <th>Time Out</th>
                            <th>Time Due</th>
                        </tr>
                        <tr>
                            <td>{dat.CustomerName}</td>
                            <td>{dat.PhoneNumber}</td>
                            <td>{dat.LoanVehicle}</td>
                            <td>{dat.TimeOut}</td>
                            <td>{dat.TimeDue}</td>
                        </tr>
                    </table>
                </div>
            );
          })}
        </div>
      );
    }

export default DataTable

Home.js

import React, { useState } from 'react';
import Calendar from 'react-calendar';
import DataTable from '../components/DataTable';
import '../styles/calendar.css'




const Home = () => {
  const [value, onChange] = useState(new Date());
  let today = new Date().toDateString()
  return (
    <>
      <div className='date-display'>
        <h3 className='current-date-display'>{today}</h3>
      </div>
      <Calendar onChange={onChange} value={value} />
      <DataTable/>
    </>
  )
}

export default Home

Here is what that renders in the browser..

Browser Render

So currently in my DataTable.js component I am using .map to loop over the data in customerData.json which works fine. The issue I have is as you can see from the screenshot img the table headers in the <th> tags render with each loop.
My question is how can I display the <th> tags once and keep the loop to display the <td> tag content..
See expected output below..

Expected Output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2025-02-18 16:27:19

您正在循环中创建许多桌子。而不是在.map()主体中输出整个表,而只输出行。将其他所有内容保持在该循环外:

return (
    <div>
        <div>
            <table className='data-table-container'>
                <tr>
                    <th>Customer Name</th>
                    <th>Phone Number</th>
                    <th>Loan Vehicle</th>
                    <th>Time Out</th>
                    <th>Time Due</th>
                </tr>
                {data.map(dat => {
                  return (
                    <tr>
                        <td>{dat.CustomerName}</td>
                        <td>{dat.PhoneNumber}</td>
                        <td>{dat.LoanVehicle}</td>
                        <td>{dat.TimeOut}</td>
                        <td>{dat.TimeDue}</td>
                    </tr>
                  );
                })}
            </table>
        </div>
    </div>
  );

&lt; tbody&gt; elements明确地使其更加清楚自己。

return (
    <div>
        <div>
            <table className='data-table-container'>
                <thead>
                    <tr>
                        <th>Customer Name</th>
                        <th>Phone Number</th>
                        <th>Loan Vehicle</th>
                        <th>Time Out</th>
                        <th>Time Due</th>
                    </tr>
                </thead>
                <tbody>
                    {data.map(dat => {
                      return (
                        <tr>
                            <td>{dat.CustomerName}</td>
                            <td>{dat.PhoneNumber}</td>
                            <td>{dat.LoanVehicle}</td>
                            <td>{dat.TimeOut}</td>
                            <td>{dat.TimeDue}</td>
                        </tr>
                      );
                    })}
                </tbody>
            </table>
        </div>
    </div>
  );

您可以通过明确分开&lt; thead&gt; and 当没有数据显示时,添加默认的“无记录”行,只是为了获得更多用户体验:

return (
    <div>
        <div>
            <table className='data-table-container'>
                <thead>
                    <tr>
                        <th>Customer Name</th>
                        <th>Phone Number</th>
                        <th>Loan Vehicle</th>
                        <th>Time Out</th>
                        <th>Time Due</th>
                    </tr>
                </thead>
                <tbody>
                    {data.length > 0 ? data.map(dat => {
                      return (
                        <tr>
                            <td>{dat.CustomerName}</td>
                            <td>{dat.PhoneNumber}</td>
                            <td>{dat.LoanVehicle}</td>
                            <td>{dat.TimeOut}</td>
                            <td>{dat.TimeDue}</td>
                        </tr>
                      );
                    }) : <tr><td colspan="5">No records found</td></tr>}
                </tbody>
            </table>
        </div>
    </div>
  );

You're creating many tables, in a loop. Instead of outputting the entire table in the .map() body, only output the rows. Keep everything else static outside of that loop:

return (
    <div>
        <div>
            <table className='data-table-container'>
                <tr>
                    <th>Customer Name</th>
                    <th>Phone Number</th>
                    <th>Loan Vehicle</th>
                    <th>Time Out</th>
                    <th>Time Due</th>
                </tr>
                {data.map(dat => {
                  return (
                    <tr>
                        <td>{dat.CustomerName}</td>
                        <td>{dat.PhoneNumber}</td>
                        <td>{dat.LoanVehicle}</td>
                        <td>{dat.TimeOut}</td>
                        <td>{dat.TimeDue}</td>
                    </tr>
                  );
                })}
            </table>
        </div>
    </div>
  );

You can make it a little more clear for yourself by explicitly separating <thead> and <tbody> elements:

return (
    <div>
        <div>
            <table className='data-table-container'>
                <thead>
                    <tr>
                        <th>Customer Name</th>
                        <th>Phone Number</th>
                        <th>Loan Vehicle</th>
                        <th>Time Out</th>
                        <th>Time Due</th>
                    </tr>
                </thead>
                <tbody>
                    {data.map(dat => {
                      return (
                        <tr>
                            <td>{dat.CustomerName}</td>
                            <td>{dat.PhoneNumber}</td>
                            <td>{dat.LoanVehicle}</td>
                            <td>{dat.TimeOut}</td>
                            <td>{dat.TimeDue}</td>
                        </tr>
                      );
                    })}
                </tbody>
            </table>
        </div>
    </div>
  );

You might even add a default "no records found" row when there's no data to display, just for a bit more user experience:

return (
    <div>
        <div>
            <table className='data-table-container'>
                <thead>
                    <tr>
                        <th>Customer Name</th>
                        <th>Phone Number</th>
                        <th>Loan Vehicle</th>
                        <th>Time Out</th>
                        <th>Time Due</th>
                    </tr>
                </thead>
                <tbody>
                    {data.length > 0 ? data.map(dat => {
                      return (
                        <tr>
                            <td>{dat.CustomerName}</td>
                            <td>{dat.PhoneNumber}</td>
                            <td>{dat.LoanVehicle}</td>
                            <td>{dat.TimeOut}</td>
                            <td>{dat.TimeDue}</td>
                        </tr>
                      );
                    }) : <tr><td colspan="5">No records found</td></tr>}
                </tbody>
            </table>
        </div>
    </div>
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文