2 回答
TA贡献1818条经验 获得超7个赞
我认为您的逻辑过于复杂,但您的代码似乎有效。
只在你的 map 函数中返回一些东西将标题添加到组件中:
const CustomerList = [
{
name: "Anny Larsson",
age: 23,
id: 1,
title: "Title1",
accountNumber: "12345",
address: "Stockholm 14, Stockholm Sweden",
hobbyList: [
"coding", "writing", "reading", "skiing"
],
emptyColumn: ""
}, {
name: "Helena hel",
age: 20,
id: 2,
title: "Title2",
accountNumber: "22245",
address: "Stockholm City, Stockholm Sweden",
hobbyList: [
"coding", "Cooking", "games", "skiing"
],
emptyColumn: ""
}, {
name: "Ayesha AAA",
age: 25,
id: 3,
title: "Title3",
accountNumber: "09845",
address: "Stockholm 21, Stockholm Sweden",
hobbyList: [
"coding", "Cooking", "games", "skiing"
],
emptyColumn: ""
}
];
class CustomerListTable extends React.Component {
state = {
customerList: CustomerList
}
headerTitle = Object.keys(this.state.customerList[0]).map((header) => {
return (<li key={header}>{header}</li>)
})
render() {
return (<div>
<h1>Customer table....</h1>
<div>
<table>
<thead>
<tr>
<th>{this.headerTitle}</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>);
}
}
ReactDOM.render(<CustomerListTable/>, document.getElementById('root'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
TA贡献1155条经验 获得超0个赞
const customerList = [
{
name: "Anny Larsson",
age: 23,
id: 1,
title: "Title1",
accountNumber: "12345",
address: "Stockholm 14, Stockholm Sweden",
hobbyList:["coding", "writing", "reading", "skiing"],
emptyColumn: ""
}
];
const headerTitle = Object.keys(customerList[0]).map((header , index) => header)
console.log(headerTitle)
在Thead Table,映射headerTitle创建动态th:
<thead>
<tr>
{
this.headerTitle.map((item, index) => <th key={index}>{item}</th>)
}
</tr>
</thead>
添加回答
举报