javascript - v-for 似乎改变了数组顺序
问题描述
数据对象:
{
"headers": {
"location": "Location",
"postcode": "Postcode",
"contributors": "Contributors",
"contributions": "Contributions",
"percentage": "Percentage"
},
"rows": [
{
"postcode": "3018",
"contributors": 2,
"contributions": 2,
"location": "Seaholme",
"percentage": 67
},
{
"postcode": "3013",
"contributors": 1,
"contributions": 1,
"location": "Yarraville West",
"percentage": 33
}
]
}
模板:
<thead>
<tr>
<th v-for="(v, k) in data.result.headers" :key="k">
{{ v }}
</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, i) in data.result.rows" :key="i">
<td :key="j" v-for="(col, j) in row">
{{ col }}
</td>
</tr>
</tbody>
所以表头和表体是两个独立的对象。虽然标题似乎遵循顺序,但行对象却没有。如何确保它们始终正确对齐?
解决方案
您可以创建computed
. rows
这将是相同的列表,但按键keys
的顺序排列。header
这是一个可能的解决方案:
new Vue({
el: "#app",
data: () => ({
"headers": { "location": "Location", "postcode": "Postcode", "contributors": "Contributors", "contributions": "Contributions", "percentage": "Percentage" },
"rows": [
{ "postcode": "3018", "contributors": 2, "contributions": 2, "location": "Seaholme", "percentage": 67 },
{ "postcode": "3013", "contributors": 1, "contributions": 1, "location": "Yarraville West", "percentage": 33 }
]
}),
computed: {
orderedRows() {
const headers = Object.keys(this.headers);
return this.rows.map(row =>
headers.reduce((orderedRow, key) =>
({ ...orderedRow, [key]: row[key] })
, {})
);
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<table>
<thead>
<tr>
<th v-for="(v, k) in headers" :key="k">{{ v }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, i) in orderedRows" :key="i">
<td v-for="(col, j) in row" :key="j">{{ col }}</td>
</tr>
</tbody>
</table>
</div>
受@CertainPerformance 评论启发的另一种可能方式:
new Vue({
el: "#app",
data: () => ({
"headers": { "location": "Location", "postcode": "Postcode", "contributors": "Contributors", "contributions": "Contributions", "percentage": "Percentage" },
"rows": [
{ "postcode": "3018", "contributors": 2, "contributions": 2, "location": "Seaholme", "percentage": 67 },
{ "postcode": "3013", "contributors": 1, "contributions": 1, "location": "Yarraville West", "percentage": 33 }
]
}),
computed: {
headerKeys() {
return Object.keys(this.headers);
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<table>
<thead>
<tr>
<th v-for="(v, k) in headers" :key="k">{{ v }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, i) in rows" :key="i">
<td v-for="(header, j) in headerKeys" :key="j">{{ row[header] }}</td>
</tr>
</tbody>
</table>
</div>
推荐阅读
- react-native-android - 为什么 Icon.onPress 比 View.onPress 或 TouchableOpacity.onPress 快?
- c++ - 为什么一个工作的主函数会显示这个智能感知错误?
- zsh - 如何让 zsh 完成以匹配文件名中间忽略大小写?
- excel - sumif/countif 的 VBA 替代方案
- ios - 将无法识别的选择器作为按钮发送到集合视图单元格的实例,该按钮将数据从按钮发送到另一个视图控制器
- vb.net - 如何以编程方式在 vb.net(visual basic) 中导入 DLL 文件?
- android - 不同风格的文本 PC 和智能手机
- python - 如何在手机屏幕中为“tinymce”设置小图标大小?
- python - 当从 csv 复制到“id”列中的空值时,复制命令不更新 Id_sequence 列违反非空约束
- react-native - 在 React Native 中更改目标元素的值