javascript - 如何在 vue js 中嵌套循环和数组?
问题描述
我从控制器查询的结果
date: {2020-09-24: {work_hours: 7}, 2020-09-30: {work_hours: 8}}
2020-09-24: {work_hours: 7}
2020-09-30: {work_hours: 8}
这是我正在尝试嵌套 for 循环的 vue,但我得到的循环结果是原来的两倍
<table class="table table-hover table-bordered table-sm" >
<thead>
<tr>
<template v-for="disp in iDate.slice(1)">
<th scope="col" v-if="toWordDay(disp.date) == 'Sunday'" style="color:red">{{disp.date | forThDate}}</th>
<th scope="col" v-else>{{disp.date | forThDate}}</th>
</template>
</tr>
</thead>
<tbody>
<template v-for="fetch in attendanceData">
<tr>
<template v-for="disp in iDate.slice(1)">
<td style="height:10px;" v-for="(data,ind) in fetch.date" v-if="ind == disp.date" >{{data.work_hours}}</td>
<td style="height:10px;" v-else>0</td>
</template>
</tr>
</template>
</tbody>
</table>
解决方案
在不知道attendanceData
or的情况下iDate
,我假设fetch.date
您的意思是Result of my query from the controller
,它是一个以日期为键的对象。您可以disp.date
用作访问器密钥。
<table class="table table-hover table-bordered table-sm" >
<thead>
<tr>
<template v-for="disp in iDate.slice(1)">
<th scope="col" v-if="toWordDay(disp.date) == 'Sunday'" style="color:red">{{disp.date | forThDate}}</th>
<th scope="col" v-else>{{disp.date | forThDate}}</th>
</template>
</tr>
</thead>
<tbody>
<template v-for="fetch in attendanceData">
<tr>
<template v-for="disp in iDate.slice(1)">
<td style="height:10px;">
<template v-if="fetch.date[disp.date]">
{{fetch.date[disp.date].work_hours || 0}}
</template>
<template v-else>0</template>
</td>
</template>
</tr>
</template>
</tbody>
</table>
推荐阅读
- github - 创建 github 远程分支
- powershell - 启用防火墙后,有没有办法测试 IP 地址的可达性?
- matlab - 在matlab中搜索和替换文本文件的多行中的特定字符串
- ios - UIScreenEdgePanGestureRecognizer 无法更改方向?
- kubernetes - 如何显示掌舵版本号
- angular - Docker - nginx angular - 403 禁止错误
- angular - 我的选择框没有填充 Angular4+
- node.js - 使用 mongodb 创建联系人时引发错误 - 发送后无法设置标题
- r - 如何与 Azure 数据工厂一起执行 R 脚本?
- ios - Xcode 9.4.1 没有显示函数调用者