【问题标题】:Add and Substract values based on checkbox in php根据php中的复选框添加和减去值
【发布时间】:2021-10-02 16:56:10
【问题描述】:

我有一个简单的复选框表单,其中包含三个选项。我想动态计算价格,我没有将这些值存储在数据库中。

<div class="custom-control custom-checkbox">
   <input type="checkbox"id="option1" wire:click="test" wire:model="additions" value="option1">
   <label class="custom-control-label" for="option1">Option 1</label>
</div>
<div class="custom-control custom-checkbox">
   <input type="checkbox" id="option2" wire:click="test" wire:model="additions" value="option2">
   <label class="custom-control-label" for="option1">Option 2</label>
</div>
<div class="custom-control custom-checkbox">
   <input type="checkbox" id="option3" wire:click="test" wire:model="additions" value="option3">
   <label class="custom-control-label" for="option1">Option 3</label>
</div>

在我显示值的刀片文件中输出

{{ $checkout }}

每个选项都有不同的价格,我将添加到$checkout 以计算总价。

public $additions = [];
public $checkout = 0;

public function test()
{
    // if (in_array('option1', $this->additions)) {
    //     $this->checkout =+ 10;
    //     Log::info($this->checkout);
    // }
    // if (in_array('option2', $this->additions)) {
    //    $this->checkout += 10;
    //     Log::info($this->checkout);
    // }
    // if (in_array('option3', $this->additions)) {
    //     $this->checkout += 10;
    //     Log::info($this->checkout);
    // }

    if (!empty($this->additions)) {
        foreach ($this->additions as $key => $value) {
            if ($value == 'option1') {
                $this->checkout += 10;
            }

            if ($value == 'option2') {
                $this->checkout += 10;
            } else {
                $this->checkout -= 10;
            }
            
            if ($value == 'option3') {
                $this->checkout += 10;
            } else {
                $this->checkout -= 10;
            }        
        }
        Log::info($this->checkout);
    }
}

价格增加但不正确。例如,每个选项我有 10 个,所以当所有三个选项都被选中时,总数将为 30,而当两个选项被选中时,则为 20,依此类推。我得到的是 60 多岁,远不及 30 岁。

任何帮助都将不胜感激。

编辑: $this-&gt;additions 包含所有选中的项目。例如:如果选择了所有三个选项,$this-&gt;additions 将在数组中包含option1,option2,option3

【问题讨论】:

  • 很难说$this-&gt;additions 包含什么。是否有已检查项的列表?
  • @waterloomatt 是的。 $this-&gt;additions 有检查项目的列表。如果选中所有复选框,它将通过option1, option2, option3
  • 带有复选框的值总是被传递。您需要检查 isset($key) 以真正查看复选框是否被标记。从那里你可以做数学。
  • @Justin 谢谢你的帮助

标签: php laravel laravel-livewire


【解决方案1】:

首先,您的 Blade 视图应该只有一个根元素,因此您应该将整个内容包装在 &lt;div&gt; 标记中。这是 Livewire 的东西,它相当重要,因为它使 Livewire 更容易进行 DOM 差异。
其次,您不需要同时使用wire:clickwire:model,因为您可以单独使用wire:model 并通过updated() 生命周期钩子监听绑定字段的更新 - 或者专门通过监听additions 属性updatedAdditions().

然后,当您执行操作时,您应该为每次迭代重置计数器并避免减去 - 否则,您将为每次点击增加结帐计数器 - 这是您现在看到的部分内容。

这里更好的选择是使用包含每个选项的值的查找数组,并在此基础上进行聚合。

<div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="option1" wire:model="additions" value="option1">
        <label class="custom-control-label" for="option1">Option 1</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="option2" wire:model="additions" value="option2">
        <label class="custom-control-label" for="option1">Option 2</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="option3" wire:model="additions" value="option3">
        <label class="custom-control-label" for="option1">Option 3</label>
    </div>

    {{ $checkout }}
</div>
class Checkout extends Component
{
    public $additions = [];
    public $checkout = 0;

    public function updatedAdditions($value)
    {
        // Declare valid options and the value they increment
        $optionValues = [
            'option1' => 10,
            'option2' => 10,
            'option3' => 10,
        ];

        // Reset the counter before we iterate over the current additions
        $this->checkout = 0;

        // Iterate over additions, and find the value from $validOptions
        // If it doesn't exist in $validOptions, default to 0 (adding 0 makes no difference)
        foreach ($this->additions as $key => $value) {
            $this->checkout += $optionValues[$value] ?? 0;
        }
    }

    public function render()
    {
        return view('livewire.checkout');
    }
}

【讨论】:

  • 非常感谢您的逻辑和帮助。我在表单中有复选框列表。看来我必须检查 updated() 生命周期钩子上的 livewire 文档。
猜你喜欢
  • 2021-06-07
  • 1970-01-01
  • 2021-12-13
  • 2019-04-12
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
  • 2016-11-05
  • 1970-01-01
相关资源
最近更新 更多