【发布时间】:2021-08-13 23:29:36
【问题描述】:
我的 vue 应用程序目前正在迭代一个包含小时数和滑动次数的员工数据对象,该数据对象每天都在为每个员工成倍增加。
这是在计算新的数据行,所以我每天都有给定的数据。
但是,在我的模板中,我试图将多重数据设置为显示在具有相应日期的列中,并为每个员工只提供一行。我的预期结果是
Employee | 2021-08-31 | 2021-09-01 | 2021-09-02
-----------------------------------------------------
Evan 60 60
Stan 100 200
Kelly 60 164
我如何修改我正在做的事情,以便最终每个员工只有一行,并根据列标题日期和该记录对象中的日期将数据放入正确的列?
<div id="app">
<table>
<thead>
<tr>
<th>Employee</th>
<th>2021-08-31</th>
<th>2021-09-01</th>
<th>2021-09-02</th>
<th>Grand Total</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in compRows">
<td v-html="row.employee"></td>
<td v-if="row.workDate == {{$date->addDay()->format($format)}}"></td>
<td v-html="row.hours"></td>
</tr>
</tbody>
</table>
</div>
@endsection
@section('loadjs')
<script>
new Vue({
el: "#app",
data: {
rows: [
{
employee: "Evan",
hours: "15",
workDate: "2021-08-31",
swipes: "4",
},
{
employee: "Kelly",
hours: "15",
workDate: "2021-08-31",
swipes: "4",
},
{
employee: "Evan",
hours: "15",
workDate: "2021-09-01",
swipes: "4",
},
{
employee: "Stan",
hours: "25",
workDate: "2021-08-31",
swipes: "4",
},
{
employee: "Kelly",
hours: "82",
workDate: "2021-09-02",
swipes: "2",
},
{
employee: "Stan",
hours: "40",
workDate: "2021-09-01",
swipes: "5",
}
]
},
methods: {
},
computed: {
compRows() {
const grouped = this.rows.reduce((r, o) => {
r[o.employee] ??= {};
r[o.employee][o.workDate] ??= {employee: o.employee, workDate: o.workDate, swipes: 0, hours: 0};
r[o.employee][o.workDate].swipes += +o.swipes;
r[o.employee][o.workDate].hours += +o.hours;
return r;
}, {});
return Object.values(grouped).map(o => Object.values(o)).flat();
}
}
});
</script>
【问题讨论】:
标签: javascript vue.js vuejs2