"日期選擇器在Livewire更新時重置"
P粉696146205
P粉696146205 2023-08-25 21:12:55
0
1
494
<p>我有一個非常簡單的Livewire元件,其中包含一個文字欄位和一個日期選擇器:</p> <pre class="brush:html;toolbar:false;"><!-- test.blade.php --> <div> <input type="text" wire:model="test" placeholder="測試"> <input datepicker="" wire:model="start" datepicker-format="dd.mm.yyyy" type="text" placeholder=" ...">>> </div> </pre> <pre class="brush:php;toolbar:false;">/* Test.php */ class Test extends Component { public $test; public $start; public function mount() { $this->start = now()->format('d.m.Y'); } public function render() { return view('livewire.test'); } } </pre> <p>我使用的日期選擇器是Flowbite Datepicker。 </p> <p>當我更改日期然後更改測試輸入欄位時,日期選擇器會重置為今天的日期。 </p> <p>我需要做什麼來保持start的值? </p> <p><strong>我已經嘗試過什麼? </strong> 我嘗試在日期選擇器上使用wire:ignore,但這沒有幫助。 </p>
P粉696146205
P粉696146205

全部回覆(1)
P粉807471604

我在這裡做了一些調試,發現在日期選擇器的程式碼中,我們可以使用「changeDate」事件來將其與Livewire連接起來。不知道為什麼這一點沒有被記錄下來。 以下是程式碼:

元件視圖:

<div>
<input type="text" wire:model="test" placeholder="测试">
<input name="start" id="startInput" inline-datepicker="" wire:model="start" datepicker-format="dd.mm.yyyy" type="text" placeholder="{{$start}}" value="{{$start}}" datepicker-autohide>

<br>
<br>
当前属性:
<hr>
<div>
    {{$test}} <br>
    {{$start}}
</div>
</div>

元件:

namespace App\Http\Livewire;

use Livewire\Component;

class SomeComponent extends Component
{
public $test;
public $start;

protected $listeners = ['changeDate' => 'changeDate'];

public function changeDate($date)
{
    $this->start = $date;
}

public function mount()
{
    $this->start = now()->format('d.m.Y');
}

public function render()
{
    return view('livewire.some-component');
}
}

以及包含Livewire元件的HTML程式碼,以及監聽Flowbite日期選擇器事件並在此之後觸發Livewire事件的js程式碼。

<body>
<div>

    <br>
    <br>

    @livewire('some-component')
</div>
<script>
    document.getElementById("startInput").addEventListener("changeDate", function (e) {
        Livewire.emit('changeDate', e.detail.datepicker.inputField.value)
    });
</script>
@livewireScripts
</body>

在我的環境中按預期工作。 乾杯

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!