【问题标题】:update on change with livewire?使用 livewire 更新更改?
【发布时间】:2020-12-22 03:56:13
【问题描述】:

我有 qty 的输入类型号:

html代码:

<span class="qty-btn minus-btn" onclick="this.parentNode.querySelector('input[type=number]').stepDown()">
    <i class="fal fa-minus-circle"></i>
</span>

<input
    wire:model.lazy="quantity"
    wire:change="updateQuantity({{ $product->id }})"
    type="number"
    class="input-text qty text"
    title="Qty" inputmode="numeric"
    step="1" min="1" max="" lang="en"
>

<span class="qty-btn plus-btn" onclick="this.parentNode.querySelector('input[type=number]').stepUp()">
    <i class="fal fa-plus-circle"></i>
</span>

当我点击任何按钮stepDown 或stepUp 时我需要更新qty

我的 Livewire 组件:

class CartQtySection extends Component
{
    public $product;
    public $quantity;

    public function mount($product)
    {
        $this->product = $product;
        $this->quantity = $product->pivot->quantity;
    }

    public function updateQuantity($id)
    {
        user()->cart()->updateExistingPivot($id, [
        'quantity' => $this->quantity,
        ]);

        $this->emit('quantityUpdated');
    }
}

【问题讨论】:

  • 不要使用javascript onclick作为你使用liveware的函数stepDown()这个函数你可以在livewire本身中编写

标签: php laravel laravel-livewire


【解决方案1】:

试试wire:click

<span class="qty-btn minus-btn" wire:click="stepDown({{ $product->id }})">
    <i class="fal fa-minus-circle"></i>
</span>


<span class="qty-btn plus-btn" wire:click="stepUp({{ $product->id }})">
    <i class="fal fa-plus-circle"></i>
</span>

在组件中

<?php

class CartQtySection extends Component
{
    public $product;
    public $quantity;

    public function mount($product)
    {
        $this->product = $product;
        $this->quantity = $product->pivot->quantity;
    }

    public function updateQuantity($id)
    {
        user()->cart()->updateExistingPivot($id, [
            'quantity' => $this->quantity,
        ]);

        $this->emit('quantityUpdated');
    }

    public function stepDown($id)
    {
        $this->quantity++;
        $this->updateQuantity($id)
    }
    public function stepUp($id)
    {
        $this->quantity--;
        $this->updateQuantity($id)
    }
}

【讨论】:

  • 对不起,它没有:(
  • @akin 立即查看
  • 很酷,谢谢工作,但是我如何在点击任何功能时更新数量?我还需要通过 updateQuantity 函数更新数量
  • @akin 立即查看
猜你喜欢
  • 2023-01-30
  • 2021-03-09
  • 2021-06-18
  • 1970-01-01
  • 2022-08-17
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
相关资源
最近更新 更多