laravel - How can updating children components in table using livewire?
问题描述
I have problem with nesting component when changing the children parent is refresh and its ok, but when I add same product in CartItem by using barcode the quantity dose not change in child component this my parent Component including a table parent.blade.php >>>
<table class="table mb-0">
<thead>
<tr>
<th scope="col">product</th>
<th>price</th>
<th>quantity</th>
</tr>
</thead>
<tbody>
@foreach ($cartItems as $item)
<tr>
<td>{{$item['name']}}</td>
<td>{{Cart::session(auth()->id())->get($item['id'])->getPriceSum()}}</td>
<td>
<livewire:child :item="$item" wire:key="$item['id']"/>
</td>
</tr>
@endforeach
</tbody>
</table>
and this id parent.php
class Parent extends Component
{
public $service = null;
public $cartItems = [];
protected $listeners = ['cartUpdated' => 'onCartUpdate'];
public function mount()
{
$this->cartItems = \Cart::session(auth()->id())->getContent()->toArray();
}
public function onCartUpdate()
{
$this->mount();
}
public function render()
{
$this->mount();
return view('livewire.parent');
}
}
the child component in <td>
just have an input with livewire:model
<div>
<input type="number" wire:model="quantity" wire:change="updateCart"><span class="col-9"></span>
</div>
My Child.php
class Child extends Component
{
public $item;
public $quantity;
public function mount($item)
{
$this->item = $item;
$this->quantity = $item['quantity'];
}
public function updateCart()
{
\Cart::session(auth()->id())->update($this->item['id'], [
'quantity' => array(
'relative' => false,
'value' => $this->quantity,
),
]);
$this->emit('cartUpdated');
}
public function render()
{
return view('livewire.child');
}
}
I think this return to mount()
method in child. can I updating quantity every time?
解决方案
You're not utilizing data-bindings properly. You don't need to add wire:change
, since you already have wire:model
-- you can simply listen for changes to that property.
Even better, just use one property $item
and access the quantity from that.
<div>
<input type="number" wire:model="item.quantity" /> <span class="col-9"></span>
</div>
Then use the "magic" method updatedItemQuantity()
to listen for updates to the quantity
attribute on the $item
property.
class Child extends Component
{
public $item;
protected $rules = [
'item.quantity' => 'required|numeric|min:0',
];
public function mount($item)
{
$this->item = $item;
}
public function updatedItemQuantity($quantity)
{
\Cart::session(auth()->id())->update($this->item['id'], [
'quantity' => array(
'relative' => false,
'value' => $quantity,
),
]);
$this->emit('cartUpdated');
}
public function render()
{
return view('livewire.child');
}
}
Now in your parent, you can simply use the "magic" event $refresh
,
class Parent extends Component
{
public $service = null;
public $cartItems = [];
protected $listeners = ['cartUpdated' => '$refresh'];
public function mount()
{
$this->cartItems = \Cart::session(auth()->id())->getContent()->toArray();
}
public function render()
{
return view('livewire.parent');
}
}
推荐阅读
- python - 如果 distance() < 20 行永远不会执行
- javascript - 使用javascript滚动时如何播放暂停iframe youtube视频
- javascript - 如何从 Wordpress 帖子中获取数据 :(
- python - 每当对象属性发生变化时自动更新 Dataframe
- python - 查找数字的前后值 - Python
- go - 在 localhost 中使用 Go 和 Postfix 发送邮件
- java - 无法获得隔离的 JDBC 连接:org.hibernate.exception.JDBCConnectionException
- reactjs - 学习 React Native,问函数 vs 类
- mysql - 无法将任何 Arduino 连接到本地数据库服务器,我已经尝试了我能想到的一切
- android - 将相机照片保存为 PDF