Livewire和flatpickr:更改时的空数据

huangapple go评论51阅读模式
英文:

Livewire and flatpickr: null data on change

问题

免责声明: 我知道已经有一些关于如何将 Livewire 和 flatpickr 配合使用的问题,但是我不明白提供的解决方案,因为它们与我处理问题的方式非常不同。 话虽如此,我仍在学习 Livewire,所以我可能做错了。

我有一个 Livewire 组件,在其中我使用 flatpickr 来选择日期和时间。

<div class="mb-3">
    <input id="chosendatetime" type="datetime-local" name="chosendatetime" value="{{ old('chosendatetime') }}" wire:model.debounce.500ms="chosendatetime" />
</div>

在 Blade 组件中,我还有一个脚本部分来初始化 flatpickr:

<script>
    flatpickr("#chosendatetime", {
        enableTime: true,
        dateFormat: "d-m-Y H:i",
    });
</script>

日期选择器被正确渲染,但当我更改其值时,客户端发送的数据为空。

我应该怎么办?

英文:

Disclaimer: I know there are already a few questions regarding coupling Livewire and flatpickr, yet I don't understand the solutions provided, since they are very different from how I'm approaching the problem. That said, I'm still learning Livewire, so I might just be doing it wrong.

I have a Livewire component where I use flatpickr to select both date and time.

&lt;div class=&quot;mb-3&quot; &gt;
    &lt;input id=&quot;chosendatetime&quot; type=&quot;datetime-local&quot; name=&quot;chosendatetime&quot; value=&quot;{{ old(&#39;chosendatetime&#39;) }}&quot; 
               wire:model.debounce.500ms=&quot;chosendatetime&quot; /&gt;
&lt;/div&gt;

In the blade component, I have also the script section to init flatpickr:

&lt;script&gt;
    flatpickr(&quot;#chosendatetime&quot;, {
        enableTime: true,
        dateFormat: &quot;d-m-Y H:i&quot;,
    });
&lt;/script&gt;

The date picker is rendered correctly, but when I change its value the data sent by the client is empty.

What should I do?

答案1

得分: 1

尝试将 wire:ignore 添加到 div 元素中,就像这样:

<div class="mb-3" wire:ignore>
    <input id="chosendatetime" type="datetime-local" name="chosendatetime" value="{{ old('chosendatetime') }}" 
               wire:model.debounce.500ms="chosendatetime" />
</div>

这个指令让 Livewire 知道在组件刷新时应跳过页面的这部分内容,不进行更改。如果不使用它,Livewire 可能会替换 flatpickr 实例并导致其停止工作。

英文:

try to add wire:ignore to the div element, like this:

&lt;div class=&quot;mb-3&quot; wire:ignore&gt;
    &lt;input id=&quot;chosendatetime&quot; type=&quot;datetime-local&quot; name=&quot;chosendatetime&quot; value=&quot;{{ old(&#39;chosendatetime&#39;) }}&quot; 
               wire:model.debounce.500ms=&quot;chosendatetime&quot; /&gt;
&lt;/div&gt;

This directive lets Livewire know that it should skip this part of the page and not change it when the component refreshes. If you don't use it, Livewire could replace the flatpickr instance and make it stop working.

答案2

得分: 0

你的代码运行正常,如果你已经正确设置了Livewire。

  • 确保在你的Livewire类中有一个名为chosendatetime的公共属性 public $chosendatetime;
  • 添加一个名为**updatedChosenDatetime()**的函数,并使用 dd() 打印 $this->chosendatetime 来查看是否接收到值。
  • 确保在你的布局模板中包含了 @livewireStyles@livewireScripts
  • 确保你已经包含了FlatPickr的JS和CSS。

Livewire类将如下所示:

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class FlatPickr extends Component
{
    public $chosendatetime;

    public function updatedChosenDatetime()
    {
        dd($this->chosendatetime);
    }

    public function render()
    {
        return view('livewire.flat-pickr');
    }
}
英文:

Your code is working fine, IF you have setup your Livewire properly.

  • Make sure you have a public property called chosendatetime in your Livewire class public $chosendatetime;
  • Add a function called updatedChosenDatetime() and dd() the $this->chosendatetime to see if the value is received or not
  • Make sure you have included @livewireStyles and @livewireScripts inside your layout blade.
  • Make sure you have included FlatPickr JS and CSS

The Livewire class will look like

&lt;?php

namespace App\Http\Livewire;

use Livewire\Component;

class FlatPickr extends Component
{
    public $chosendatetime;

    public function updatedChosenDatetime()
    {
        dd($this-&gt;chosendatetime);
    }

    public function render()
    {
        return view(&#39;livewire.flat-pickr&#39;);
    }
}

huangapple
  • 本文由 发表于 2023年7月3日 18:50:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定