javascript - 如何计算 HTML 表格中的价格?
问题描述
我已经查看了数据库中的数据并过滤了数据。现在,我想做计算,但我没有找到在 HTML 本身内部进行计算的方法。这是我的html编码。我应该在这里做javascript函数吗?或如何?我对如何根据表的数组进行计算感到困惑。
@extends('admin.layouts.app')
@section('content')
<style>
tfoot {
background-color: #a8d1e3;
}
</style>
<script>
{{$items->links()}}
console.log("try");
</script>
<div class="container">
<div class="card">
<div class="card-header">
<div class="float-right">{{$items->links()}}</div>
<h3>Total Sales</h3>
</div>
<div class="card-body">
<form action="{{route('admin-insurance.filter')}}" method="POST">
@csrf
<label>Filter by date : </label>
<div class="row">
<div class="col-md-2">
<div class="form-group">
<input id="from" name="from" value="{{$from}}" placeholder="From" type="text"
class="form-control datepicker">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input id="to" name="to" value="{{$to}}" placeholder="To" type="text"
class="form-control datepicker">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<select class="form-control" name="insurance_id">
<option value=-1">ALL</option>
@foreach ($insurances as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<select class="form-control" name="exported">
<option value="-1">ALL</option>
<option value="1">CHECKED</option>
<option value="2">UNCHECKED</option>
</select>
</div>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-success btn-sm pr-5 pl-5">Search</button>
</div>
<div class="col-md-4">
</div>
</div>
</form>
@include('admin.layouts.notification')
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Policy No</th>
<th scope="col">Product Name</th>
{{-- <th scope="col">Region</th> --}}
<th scope="col">Package</th>
<th scope="col">Plan</th>
<th scope="col">Enroll Date</th>
<th scope="col">Departure</th>
<th scope="col">Return</th>
<th scope="col">Price</th>
<th scope="col">Exported</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php $count = 1; ?>
@foreach ($items as $item)
<tr>
<th scope="row">{{$count++}}</th>
<td>{{$item->policy_no}}</td>
<td>{{$item->insurance->name}}</td>
{{-- <td>{{$item->region->name}}</td> --}}
<td>{{$item->insurance->name}}</td>
<td>{{$item->plan->name}}</td>
<td>{{$item->created_at}}</td>
<td>{{$item->depart_date}}</td>
<td>{{$item->return_date}}</td>
<td>RM {{number_format($item->amount,2)}}</td>
<td>
@if ($item->exported == 0)
<strong>NO</strong>
@else
<strong>YES</strong>
@endif
</td>
<td>
<a class="btn btn-xs btn-success mb-1 btn-block text-white"
href="/admin/insurance/product/sales/{{$item->id}}">View</a>
</td>
@endforeach
</tbody>
</table>
</div>
</div>
<div class=" card-footer">
@csrf
<label for="TotalSales" align="right">Total Sales: </label>
<input id="TotalSales"><br>
<div class="float-right">{{$items->links()}}</div>
</div>
<div class=" card-footer">
<form action="{{route('admin-insurance.download')}}" method="POST">
@csrf
<input id="from1" value="{{$from}}" name="from1" type="hidden">
<input id="to1" value="{{$to}}" name="to1" type="hidden">
<input id="insurance_id" value="{{$insurance_id}}" name="insurance_id" type="hidden">
<input id="exported" value="{{$exported}}" name="exported" type="hidden">
<button type="submit" class="btn btn-primary">Export to CSV</button>
<a href="/admin/insurance/product/mark-exported" class="btn btn-success">Mark as Exported</a>
<a href="/admin/insurance/product/benefit" class="text-white btn btn-secondary">Generate Report</a>
</form>
<div class="float-right">{{$items->links()}}</div>
</div>
</div>
</div>
@endsection
这是控制器..
public function filterDate(Request $request)
{
$insurances = Insurance::all();
$from = $request->from;
$to = $request->to;
$insurance_id = $request->insurance_id;
$exported = $request->exported;
$enrollments = InsuranceEnrollment::where('status', '=', 2)
->where("insurance_id", $insurance_id)
->whereBetween('created_at', [$from, $to])
->orderBy('created_at', 'DESC')
->paginate(10);
$items = array(
'items' => $enrollments,
'insurances' => $insurances,
'insurance_id' => $insurance_id,
'exported' => $exported,
'from' => $from,
'to' => $to
);
return view('admin.insurance.sales.home')->with($items);
}
解决方案
您可以在查询中计算总和。在选择中使用 SUM 函数。
例如:
DB::raw("SUM(marks) as total_marks")
推荐阅读
- c - 我试图在 c 中的结构中获取输入时出错,而 fgets() 没有解决它
- c# - 为 WPF 应用程序创建通用设置
- python-3.x - Python 初学者:如何使文本 #1 看起来像文本 #2
- tensorflow - 多层神经网络 TensorFlow
- python-3.x - 无法访问 jarfile 'tabula-1.0.2-jar-with-dependencies.jar'
- mysql - 值超出特定数字的范围
- r - 如何按列查找矩阵列表的最小值
- ios - 文本输入期间的文本字段验证
- ios - 在从初始化程序错误返回之前,未在所有路径上调用 Super.init
- sql - 匹配值颜色后的 SSRS 查找条件