首页 > 解决方案 > 如何获取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_daysdivide总数8取决于每个工人的比率。我想把它全部加起来multiplydaily_ratedaily_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>

标签: javascriptlaravelvue.js

解决方案


你可以在 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;

推荐阅读