Laravel Livewire 上的 select2 不起作用
2020-08-28
11308
我按照官方文档所示在 select 中实现了 select2,但无法使其完全正常工作。
<div>
<div wire:ignore>
<select class="js-example-basic-single" name="state">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
<!-- Select2 will insert it's DOM here. -->
</div>
@push('scripts')
<script>
$(document).ready(function() {
$('.js-example-basic-single').select2();
$('.js-example-basic-single').on('change', function (e) {
@this.set('foo', e.target.value);
});
});
</script>
@endpush
如果我在视图中删除以下脚本,select2 组件将正常呈现
$('.js-example-basic-single').on('change', function (e) {
@this.set('foo', e.target.value);
});
但当然,我失去了所需的功能。
我使用的 selct2 添加链接如下
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="{{asset('SitioWeb/assets/select2/js/select2.min.js')}}"></script>
我遗漏了什么?
3个回答
由于 select2 的工作方式,livewire 的 wire:model 和 wire:change 可能无法与 select2 配合使用。Livewire 的 wire:model 和 wire:change 与传统的 HTML select 控件完美配合。要将 select 与 livewire 的 wire:model 和 wire:change 配合使用,请编写一个 js 代码从 select2 获取选定的值,然后使用
Livewire.emit()
将选定的值发送到您的组件并从那里进行处理。示例 js 代码如下:
$(document).ready(function() {
$('#id').select2();
$('#id').on('change', function(e) {
Livewire.emit('listenerReferenceHere',
$('#id').select2("val"));
});
});
您的 Livewire 组件:
...
protected $listeners = ['listenerReferenceHere'];
public function listenerReferenceHere($selectedValue)
{
//Do something with the selected2's selected value here
}
希望这有帮助...😊
Felix Wiafe
2022-02-01
您可以使用 Bootstrap-select
npm install bootstrap-select
对我来说效果很好。这是我的代码。
<div class="form-group">
<label for="permissions">{{ __('role.permissions') }}</label>
<div class="col-sm-10">
<div wire:key="UNIQUE_KEY">
<div wire:ignore>
<select id="permissions" wire:model="permissions"
data-live-search="true"
data-actions-box="true"
title="{{__('generic.select')}} {{ __('role.permissions') }}"
name="permissions[]" data-width="100%"
class="selectpicker permissions" multiple>
@foreach($list_permission as $permission)
<option value="{{ $permission->id }}"
data-subtext="{{ $permission->key }} {{ ' -'. $permission->table_name }}">
{{ __('permission.'.$permission->key) }}
</option>
@endforeach
</select>
</div>
</div>
@error('permissions') <span class="text-danger">{{ $message }}</span> @enderror
</div>
</div>
在脚本中:
....
@push('scripts')
<script>
Livewire.restart();
$(function () {
$('.permissions').selectpicker();
});
</script>
@endpush
</div>
在组件中
public $permissions = [];
public $old_permissions = [];
public function updatedPermissions()
{
$filter_arrays = array_filter($this->permissions);
$unique = array_unique($filter_arrays);
$this->permissions = $unique;
}
在更新中:
if (! empty($this->old_permissions)){
$updateRole = $this->role->find($this->modelId)->permissions()->detach();
$updateRole = $this->role->find($this->modelId)->permissions()->sync($validatedData['permissions']);
}elseif ($this->old_permissions != $this->permissions ){
$updateRole = $this->role->find($this->modelId)->permissions()->attach($validatedData['permissions']);
}
Abdulmajeed
2020-10-17
我努力尝试将 livewire 与 select2 结合起来,最后通过这种方式找到了解决方案。
UsersComponent.php
class Users extends Component
{
public $users = [];
public function mount()
{
$this->users= User::all();
}
public function render()
{
return view('livewire.users');
}
}
然后
users.blade.php
<div class="col-sm-12 col-xl-3 m-b-30" wire:ignore >
<div >
<h4 class="sub-title" >USERS</h4>
<select class="js-example-basic-single form-control-warning" name="states" >
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name}}</option>
@endforeach
</select>
</div>
</div>
index.blade.php
<p>
@livewire(livewire.users)
</p>
ersen
2020-11-17