[英]How to apply select2 in laravel livewire with 1 component in multiple blade file
**Component File**
class PurchaseRequests extends Component
}
public $show = 'index';
public function create()
{
$this->reset();
$this->method = 'store';
$this->show = 'form';
}
public function render()
{
return view('livewire.purchase_requests.'.$this->show);
}
}
Blade File index.blade.php form.blade.php刀片文件索引.blade.php form.blade.php
I tried to apply the select2 in index.blade.php and its working but when i try it to form.blade.php its not working.我尝试在 index.blade.php 中应用 select2 及其工作,但是当我尝试将其应用于 form.blade.php 时它不工作。 i dont know whats the problem.
我不知道是什么问题。
I don't know what could be causing the issue once I can't see any code.一旦我看不到任何代码,我不知道是什么导致了这个问题。 But for my experience with select2 and Livewire I'm going to share with you my codding.
但是对于我在 select2 和 Livewire 方面的经验,我将与您分享我的编码。 This is a simple example for one component and the blade related.
这是一个组件和刀片相关的简单示例。 component
零件
public $models;
public $selected_item;
protected $listeners = ['selected_select2_item']; // this listen for the select2 emitted event
public function hydrate() // hydrate the select2 element on every re-render
{
$this->emit('select2');
}
public function mount()
{
$this->models = Model::all();
}
public function render()
{
//......
}
public function selected_select2_item($value) // function load from listener
{
dd($value);
}
and in the blade component在刀片组件中
@section('content')
//......
<select id="select2_element" wire:change="$emit('selected_select2_item',$event.target.value)">
@foreach($models as $model)
<option value="{{$model->id}}">{{$model->name}}</option>
@endforeach
</select>
//......
@endsection
@section('custom_scripts')
<script>
$(document).ready(function() {
window.initSelectDrop=()=>{
$('#select2_element').select2({
placeholder: '-- Select model--',
allowClear: true});
}
initSelectDrop();
$('#select2_element').on('change', function (e) {
livewire.emit('selected_select2_item', e.target.value)
});
window.livewire.on('select2',()=>{
initSelectDrop();
});
});
</script>
@endsection
Hope this code can help you like it did it before with me;希望这段代码可以帮助你喜欢它以前和我一起做的事情; ;)
;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.