gpt4 book ai didi

javascript - 创建动态表格组件 Vue JS

转载 作者:搜寻专家 更新时间:2023-10-30 22:41:47 25 4
gpt4 key购买 nike

我正在学习 Vue JS。我想创建一个组件来显示我可以在其他组件中重用的表格数据。我有 2 个 Prop 项目(数据)和列(列名)

items: [
{
'id':'1',
'title': 'hello',
'description': 'ok ok',
'created_date': '2018-09-09'
},
{
'id':'2',
'title': 'hello 2',
'description': 'ok ok 2',
'created_date': '2018-10-09'
}
],
columns: [ 'id', 'title', 'description', 'created_date']

我想创建一个表如下

<table class="table">
<thead>
<tr>
<th v-for="(column, index) in columns" :key="index"> {{column}}</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in items" :key="index">
<td v-for="(column, indexColumn) in columns" :key="indexColumn">{{item.$column}}</td>
</tr>
</tbody>

我不知道如何按列名从项目中获取值(value)。是否可以创建这样的表?

最佳答案

一切都很好,只需将模板 item.$column 中的值更改为 item[column]

   <table class="table">
<thead>
<tr>
<th v-for="(column, index) in columns" :key="index"> {{column}}</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in items" :key="index">
<td v-for="(column, indexColumn) in columns" :key="indexColumn">{{item[column]}}</td>
</tr>
</tbody>
</table>

关于javascript - 创建动态表格组件 Vue JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53007008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com