使用React.js返回动态表
我试图使用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中,我正在使用.map
在data
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..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在循环中创建许多桌子。而不是在
.map()
主体中输出整个表,而只输出行。将其他所有内容保持在该循环外:&lt; tbody&gt; elements明确地使其更加清楚自己。
您可以通过明确分开
&lt; thead&gt;
and当没有数据显示时,添加默认的“无记录”行,只是为了获得更多用户体验:
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:You can make it a little more clear for yourself by explicitly separating
<thead>
and<tbody>
elements:You might even add a default "no records found" row when there's no data to display, just for a bit more user experience: