如何对动态字段输入中的每一行数据求和 Laravel Livewire
How to Sum every rows data in dinamic field input Laravel Livewire
我输入了动态字段,因此我可以添加更多列并将其删除。在此输入中,我有一列 total_price
,关于价格,我需要 price * qty 。但我不知道如何在多个输入上做到这一点。我只能在单一输入上做到这一点。我的表单和 livewire 是这样的:
形式
<form>
<button wire:click.prevent="add({{$i}})">
Add
</button>
<input type="hidden" wire:model="newid.0">
<input wire:model="nama_barang.0" type="text" />
<input wire:model="qtt.0" type="text" />
<input wire:model="price.0" type="text" />
<input wire:model="qty.0" type="text" />
<input wire:model="total_price" type="text" /> // here the problem
@foreach($inputs as $key => $value)
<input wire:model="nama_barang.{{ $value }}" type="text" />
<input wire:model="qtt.{{ $value }}" type="text" />
<input wire:model="price.{{ $value }}" type="text" />
<input wire:model="qty.{{ $value }}" type="text" />
<input wire:model="total_price" type="text" /> // on here i get the problem
@endforeach
<button wire:click.prevent="store()">Submit</button>
</form>
这是我的 livewire
public $belanja_id, $nama_barang, $qtt,$newid;
public $updateMode = false;
public $inputs = [];
public $i = 1;
public $total_price ;
public $price= [] ;
public $qty = [];
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs ,$i);
}
public function mount($id)
{
$belanja = $this->belanja = Belanja::findOrFail($id);
$this->newid = $belanja->id;
$this->k_uraian = $belanja->uraian;
}
public function remove($i)
{
unset($this->inputs[$i]);
}
public function render()
{
$this->total_price =array_sum($this->price) * array_sum($this->qty) ; // i try with this but only get 1 rows , can someone help ?
return view('livewire.input-belanja-lw');
}
你可以在这个 picture 上看到我的表格(最好你看到这张图片,这样你就可以知道我的问题),我不能加总和 total_price 。有人可以帮忙吗?
我的参考来自这个网站site
更新。我的输入现在是正确的,但我的商店有错误
这是我的商店功能
public function store()
{
foreach ($this->nama_barang as $key => $value) {
$bel = AnakBelanja::create([
'belanja_id' => $this->newid,
'nama_barang' => $this->nama_barang[$key],
'qtt' => $this->qtt[$key],
'price' => $this->price[$key],
'qty' => $this->qty[$key]
]);
}
$this->inputs = [];
$this->resetInputFields();
return redirect()->route('detail', $bel->belanja_id);
$this->emit('alert', ['type' => 'success', 'message' =>'Succes Melakukan Input / Update']);
}
因为你这样做是不可能的所以我做了一些修复试试这个
组件
public $belanja_id, $nama_barang, $qtt, $newid;
public $updateMode = false;
public $inputs = [
[
"newid" => "",
"nama_barang" => "",
"qtt" => "",
"price" => "",
"qty" => "",
"total_price" => "",
]
];
public function render()
{
return view('livewire.input-belanja-lw');
}
public function add()
{
array_push($this->inputs, [
"newid" => "",
"nama_barang" => "",
"qtt" => "",
"price" => "",
"qty" => "",
"total_price" => "",
]);
}
public function mount($id)
{
$belanja = $this->belanja = Belanja::findOrFail($id);
$this->newid = $belanja->id;
$this->k_uraian = $belanja->uraian;
}
public function remove($i)
{
unset($this->inputs[$i]);
}
Blade
<form>
<button wire:click.prevent="add()">
Add
</button>
@foreach($inputs as $key => $value)
<input type="hidden" wire:model="inputs.{{ $key }}.newid" value="{{ $key }}">
<input wire:model="inputs.{{ $key }}.nama_barang" type="text" />
<input wire:model="inputs.{{ $key }}.qtt" type="text" />
<input wire:model="inputs.{{ $key }}.price" type="text" />
<input wire:model="inputs.{{ $key }}.qty" type="text" />
<input value="{{ (int)$value['price'] * (int)$value['qty'] }}" type="text" />
<br>
@endforeach
<button wire:click.prevent="store()">Submit</button>
</form>
我输入了动态字段,因此我可以添加更多列并将其删除。在此输入中,我有一列 total_price
,关于价格,我需要 price * qty 。但我不知道如何在多个输入上做到这一点。我只能在单一输入上做到这一点。我的表单和 livewire 是这样的:
形式
<form> <button wire:click.prevent="add({{$i}})"> Add </button> <input type="hidden" wire:model="newid.0"> <input wire:model="nama_barang.0" type="text" /> <input wire:model="qtt.0" type="text" /> <input wire:model="price.0" type="text" /> <input wire:model="qty.0" type="text" /> <input wire:model="total_price" type="text" /> // here the problem @foreach($inputs as $key => $value) <input wire:model="nama_barang.{{ $value }}" type="text" /> <input wire:model="qtt.{{ $value }}" type="text" /> <input wire:model="price.{{ $value }}" type="text" /> <input wire:model="qty.{{ $value }}" type="text" /> <input wire:model="total_price" type="text" /> // on here i get the problem @endforeach <button wire:click.prevent="store()">Submit</button> </form>
这是我的 livewire
public $belanja_id, $nama_barang, $qtt,$newid;
public $updateMode = false;
public $inputs = [];
public $i = 1;
public $total_price ;
public $price= [] ;
public $qty = [];
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs ,$i);
}
public function mount($id)
{
$belanja = $this->belanja = Belanja::findOrFail($id);
$this->newid = $belanja->id;
$this->k_uraian = $belanja->uraian;
}
public function remove($i)
{
unset($this->inputs[$i]);
}
public function render()
{
$this->total_price =array_sum($this->price) * array_sum($this->qty) ; // i try with this but only get 1 rows , can someone help ?
return view('livewire.input-belanja-lw');
}
你可以在这个 picture 上看到我的表格(最好你看到这张图片,这样你就可以知道我的问题),我不能加总和 total_price 。有人可以帮忙吗?
我的参考来自这个网站site
更新。我的输入现在是正确的,但我的商店有错误
这是我的商店功能
public function store()
{
foreach ($this->nama_barang as $key => $value) {
$bel = AnakBelanja::create([
'belanja_id' => $this->newid,
'nama_barang' => $this->nama_barang[$key],
'qtt' => $this->qtt[$key],
'price' => $this->price[$key],
'qty' => $this->qty[$key]
]);
}
$this->inputs = [];
$this->resetInputFields();
return redirect()->route('detail', $bel->belanja_id);
$this->emit('alert', ['type' => 'success', 'message' =>'Succes Melakukan Input / Update']);
}
因为你这样做是不可能的所以我做了一些修复试试这个
组件
public $belanja_id, $nama_barang, $qtt, $newid;
public $updateMode = false;
public $inputs = [
[
"newid" => "",
"nama_barang" => "",
"qtt" => "",
"price" => "",
"qty" => "",
"total_price" => "",
]
];
public function render()
{
return view('livewire.input-belanja-lw');
}
public function add()
{
array_push($this->inputs, [
"newid" => "",
"nama_barang" => "",
"qtt" => "",
"price" => "",
"qty" => "",
"total_price" => "",
]);
}
public function mount($id)
{
$belanja = $this->belanja = Belanja::findOrFail($id);
$this->newid = $belanja->id;
$this->k_uraian = $belanja->uraian;
}
public function remove($i)
{
unset($this->inputs[$i]);
}
Blade
<form>
<button wire:click.prevent="add()">
Add
</button>
@foreach($inputs as $key => $value)
<input type="hidden" wire:model="inputs.{{ $key }}.newid" value="{{ $key }}">
<input wire:model="inputs.{{ $key }}.nama_barang" type="text" />
<input wire:model="inputs.{{ $key }}.qtt" type="text" />
<input wire:model="inputs.{{ $key }}.price" type="text" />
<input wire:model="inputs.{{ $key }}.qty" type="text" />
<input value="{{ (int)$value['price'] * (int)$value['qty'] }}" type="text" />
<br>
@endforeach
<button wire:click.prevent="store()">Submit</button>
</form>