拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 自动计算总值laravellivewire

自动计算总值laravellivewire

白鹭 - 2022-03-24 2137 0 0

我有一个未计算值的问题。我有用户可以更新重量/尺寸和数量的功能(如果输入不是只读的)。当用户输入新的重量/尺寸时,它将在订单总计中自动计算。显然,订单总计中已经有一个值(因为它是一个更新),所以我希望它在用户更新重量/尺寸或数量的新输入时自动更新订单总计中的值。 请点击这张图片

这是我的updates.blade.php

<div class="col-sm-12 col-md-6" >
    <div class="card">
        <div class="card-body">
        <div class="form-actions">  
                <div class="container" style="padding: 0;margin: 0;">
                <div class="row">
                    <div class="col-sm-6" >
                    <span class="float-sm-left">
                    <h3 class="card-title">Update Order</h3>
                    </span>
                    </div>
                </div>
                </div>
                </div>
                </br>
                <form action="" method="POST" >
                @csrf
                @method('PUT')
                <div class="form-row">
                    <div class="col-md-4 mb-3">
                        <label for="validationTooltip01">Order ID</label>
                        <input type="text" class="form-control" wire:model="orderID" value="" readonly>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label for="validationTooltip02">Order Date</label>
                        <input type="text" class="form-control" wire:model="orderDate" value="" readonly>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label for="validationTooltipUsername">Order Status</label>
                        <input type="text" class="form-control" wire:model="orderStatus" value="" readonly>
                     </div>
                </div>

                <div class="form-row">
                    <div class="col-md-6 mb-3">
                        <label class="form-control-label" >Customer Phone Number</label>
                        <input type="text" name="" value="" class="form-control" maxlength="11" wire:model="custPhone"  readonly>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-control-label" >Customer Name</label>
                        <input type="text" name="" value="" class="form-control" maxlength="11" wire:model="custName"  readonly>
                    </div>

                </div>
                <div class="form-row">
                @foreach($serviceOrder as $o)
                    <div class="col-md-5 mb-3">
                        <label class="form-control-label" >Service {{ $loop->iteration }} </label>
                        <input type="text"  wire:model="serviceOrder.{{ $loop->index }}.serv.serviceName" class="form-control " readonly>
                       
                    </div>
                    <input type="hidden"  wire:model="serviceOrder.{{ $loop->index }}.serv.servicePrice" hljs-number">1 mb-3" readonly>
                   
                    <div hljs-number">3 mb-3">
                        <label hljs-string">" >Weight/Size</label>
                        <input type="text" name="" value=""  wire:model="serviceOrder.{{ $loop->index }}.weightsize"  hljs-string">">
                    </div>
                    
                    <div hljs-number">3 mb-3"> 
                        <label hljs-string">" >Quantity*opt</label>
                            <input type="text" name="" value="" wire:model="serviceOrder.{{ $loop->index }}.quantity"  hljs-string">" @if(!$serviceOrder[$loop->index]['quantity']) readonly @endif >
                    </div>
                @endforeach
                </div>
                    
                    <label hljs-string">" >Order Total RM</label>
                    <input type="number" wire:model="orderTotal" value="" hljs-string">" readonly>
                    
                     </br>
                                         
                <div hljs-string">">  
                <div hljs-string">" style="padding: 0;margin: 0;">
                <div hljs-string">">
                    <div hljs-number">6" >
                    <span hljs-keyword">float-sm-left">
                    <a hljs-string">" href="{{ route('orders.indexInProcess') }}"> Back</a>
                    </span>
                    </div>
                    <div hljs-number">6">
                    <span hljs-keyword">float-sm-right">
                        <div hljs-string">">
                            <button type="submit" hljs-number">2">Update</button>
                            <button type="reset" hljs-keyword">float-right">Reset</button>
                        </div>
                    </span>
                    </div>
                </div>
                </div>
                </div>

                 </form>
                </div>
                </div>
                

这是livewire updates.php

<?php

 namespace App\Http\Livewire;

 use Livewire\Component;
 use App\Customer;
 use App\Service;
 use App\Order;
 use App\ServiceOrder;

 class Updates extends Component
 {
 public $orders;
public $orderID;
public $orderDate;
public $orderStatus;
public $orderTotal;
public $custName;
public $custPhone;
public $serviceName;
public $servicePrice;
public $weightsize;
public $quantity;
public $serviceOrder;


public function mount($order,$so)
{
    //$orders = Order::with('customer')->get();
    //ServiceOrder::with('serv')->where('order_id', $order->id)->get();
    $this->orderID = $order->id;
    $this->orderDate = $order->orderDate;
    $this->orderStatus = $order->orderStatus;
    $this->orderTotal = $order->orderTotal;
    $this->custPhone = $order->customer->custPhone;
    $this->custName = $order->customer->custName;
    $this->serviceOrder = $so->toArray();
    //dd($this->serviceOrder);
    //$so->serviceName,
    //$so->weightsize,
    //$so->quantity 

}

public function render()
{
    //$so = ServiceOrder::with('serv')->where('order_id', $order->id)->get();
    return view('livewire.updates');
}

public function updatedInputs($name)
{
    
    $array = explode('.', $name);
    if ($array[1] == 'serviceName') {
        $this->inputs[$array[0]]['servicePrice'] = $this->services->find($value)->servicePrice;
    }
    try {
        $this->calculateTotal();
    } catch (\Exception $e) {
    }
}

 // perform calculation here.
 public function calculateTotal(){
    $this->orderTotal =  $this->orderTotal;
        foreach ($this->serviceOrder as $item) {
            if($item['quantity'] == ''){
                $item['optQuantity']= 1;
                $this->orderTotal  = ($item['servicePrice'] * $item['weightsize']) * ($item['quantity']);
                
            }else{
                $this->orderTotal  = $item['servicePrice'] * ($item['weightsize']) * ($item['quantity']);
                
            }
               
                //$this->total *=  $item['optQuantity']; // * price;
        }
    }
  }

请帮帮我 TT

uj5u.com热心网友回复:

您可以从组件(例如)触发事件,然后在类中注册侦听器并呼叫更新总数的方法。检查档案

uj5u.com热心网友回复:

“显然,订单总额中已经有一个值(因为它是一个更新),所以我希望它在用户更新重量/尺寸或数量的新输入时自动更新订单总额中的值。”

那么,问题是$orderTotal 只更新一次?当用户更新新输入时它不会更新?. 每次有动作或任何输入时,Livewire 都应该渲染。

首先,我建议你检查当用户使用 dd 函式更新新输入时你的函式会发生什么。

public function calculateTotal(){
    dd('is this func work?');
    $this->orderTotal =  $this->orderTotal;
        foreach ($this->serviceOrder as $item) {
            if($item['quantity'] == ''){
                $item['optQuantity']= 1;
                $this->orderTotal  = ($item['servicePrice'] * $item['weightsize']) * ($item['quantity']);
                
            }else{
                $this->orderTotal  = $item['servicePrice'] * ($item['weightsize']) * ($item['quantity']);
                
            }
               
                //$this->total *=  $item['optQuantity']; // * price;
        }
    }

除错完你的函式后,也许你已经找到了你要找的答案,或者你可以回来问我任何问题。^^

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *