Laravel Livewire Select2 Not Working with Wizard Form
In this tips, I will show you solution of select2 not working with wizard form in laravel livewire.
If you are using Livewire, you might face issues when reloading JavaScript libraries like Select2, Datepicker, or Tooltip, especially in step-by-step wizard forms. If these are not working for you, I have a solution.
We will use the morphed hook to reinitialize Select2 in Livewire. The morphed hook runs after all child elements in the component are updated.
Here is a simple example code:
Livewire Class
<?php
use Livewire\Component;
class StepComponent extends Component
{
public $currentStep = 1;
public $select1;
public $options = [];
public function mount()
{
$this->options = [
['id' => 1, 'name' => 'Option 1'],
['id' => 2, 'name' => 'Option 2'],
['id' => 3, 'name' => 'Option 3'],
];
}
public function updateSelectValue($data)
{
$this->select1 = $data['value'];
}
public function render()
{
return view('livewire.step-component');
}
}
Livewire Component File
<div>
@if($currentStep == 1)
<div wire:ignore>
<label for="select1">Select an option:</label>
<select id="select1" class="form-control" style="width: 100%;">
<option value="">-- Choose an option --</option>
@foreach($options as $option)
<option value="{{ $option['id'] }}"
@if($select1 == $option['id']) selected @endif>
{{ $option['name'] }}
</option>
@endforeach
</select>
</div>
<div class="mt-3">
<strong>Selected Value:</strong> {{ $select1 }}
</div>
@else
<div>Step 2 content</div>
@endif
</div>
<script>
document.addEventListener('livewire:init', () => {
// Function to initialize Select2
function initializeSelect2() {
$('#select1').select2();
// Dispatch Livewire event on change
$('#select1').on('change', function () {
const selectedValue = $(this).val();
@this.set('select1', selectedValue);
});
}
initializeSelect2();
Livewire.hook('morphed', ({ el, component }) => {
// Runs after all child elements in `component` are morphed
console.log($('#select1').html());
initializeSelect2();
})
});
</script>
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Manage User Timezone in Laravel?
- PHP Laravel Generate Apple Wallet Pass Example
- Laravel Datatables Relationship with Filter Column Example
- Laravel Eloquent Find with Trashed Record Example
- How to Read JSON File in Laravel?
- How to Set Custom Redirect URL After Login in Laravel Jetstream?
- Laravel Relationship with Comma Separated Values Example
- Laravel Relationship with JSON Column Example
- Laravel 11 Display Image from Storage Folder Example
- Customize Laravel Jetstream Registration and Login Example
- Laravel UI Auth: Login With Email Or Username Example
- How to Implement Email Verification in Laravel?