javascript - 如何获取vue js中所有列的总和
问题描述
我在控制器中的查询
public function show($id)
{
$structural = DB::table('attendance')->where('payroll_daily_id',$id)
->where('assignment','STRUCTURAL')
->select('payroll_daily_attendance.*')
->get();
$fetch = [];
foreach($structural as $key){
if(!isset($fetch[$key->wrk_id]['total_ot']) && !isset($fetch[$key->wrk_id]['total_days']) ){
$fetch[$key->wrk_id]['total_ot'] = 0;
$fetch[$key->wrk_id]['total_days'] = 0;
}
$fetch[$key->wrk_id]['wrk_id'] = $key->wrk_id;
$fetch[$key->wrk_id]['daily_rate'] = $key->rate;
$fetch[$key->wrk_id]['date'][$key->date]['work_hours'] = $key->reg_hour;
$fetch[$key->wrk_id]['date'][$key->date]['adj_hour'] = $key->adj_hour;
$fetch[$key->wrk_id]['total_ot'] += $key->ot_adj;
$fetch[$key->wrk_id]['total_days'] += $key->reg_hour;
}
return $fetch;
}
我的查询结果
1: {total_ot: 1, total_days: 24, wrk_id: 1, daily_rate: 575,…}
daily_rate: 575
date: {2020-09-24: {work_hours: 8, adj_hour: 0}, 2020-09-25: {work_hours: 8, adj_hour: 0},…}
total_days: 24
total_ot: 1
wrk_id: 1
2: {total_ot: 0, total_days: 48, wrk_id: 2, daily_rate: 450,…}
daily_rate: 450
date: {2020-09-24: {work_hours: 8, adj_hour: 0}, 2020-09-25: {work_hours: 8, adj_hour: 0},…}
total_days: 48
total_ot: 0
wrk_id: 2
3: {total_ot: 0, total_days: 8, wrk_id: 3, daily_rate: 560,…}
daily_rate: 560
date: {2020-09-24: {work_hours: 8, adj_hour: 0}}
total_days: 8
total_ot: 0
wrk_id: 3
在我的 vue 中......我的公式是得到我的总数
work_hours
和 它total_days
的divide
总数8
取决于每个工人的比率。我想把它全部加起来multiply
daily_rate
daily_rate
<table class="table table-sm borderless">
<thead>
<th></th>
<th></th>
</thead>
<tbody>
<tr v-for="fetch in assignmentTotal">
<td style="height:10px;">Structural</td>
<td style="height:10px;">{{(fetch.total_days / 8) * fetch.daily_rate)}</td>
</tr>
</tbody>
</table>
解决方案
你可以在 PHP 端计算总和,而在 Vue.js 中你只输出结果。
$fetch = [];
$total_structural = 0;
foreach ($structural as $key) {
// ...
$total_structural += ($key->reg_hour / 8) * $key->rate;
}
$fetch['total_structural'] = $total_structural;
return $fetch;
推荐阅读
- javascript - Ngrx/效果在所有动作之后执行
- windows - 如果 VirtualBox VM 未在 Windows 上运行,如何通过命令行检查
- google-cloud-platform - 谷歌云:阻止用户同步到其他实例
- google-apps-script - 在 Google 协作平台中嵌入网络应用程序不起作用
- amazon-web-services - 更改 AWS 自定义资源的默认超时
- phpstorm - 如何在 PhpStorm 中的选项卡之间切换?
- apache-flink - 如何在纱线模式下运行 flink scala shell
- ios - 如何在 UITableview DataSource 方法中的 UIContextualAction 中添加图像
- c++ - 为什么 `operator<<` 对指向派生类型的成员不起作用?
- html - 如何使用表格在模态文本框