如官方文档所示,我在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>
我想念什么?
答案 0 :(得分:0)
您可以使用Bootstrap选择
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']);
}
答案 1 :(得分:0)
我努力将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>