laravel - Livewire Select2 Dynamic 不更新公共视图
问题描述
我正在使用 select2 组件,wire:ignore
并且我想在单击按钮后动态更新 select2 值。我使用事件设置了这个功能,并且事件工作正常,变量也被初始化。我未能更新此 select2 的公开视图。
我的刀片
<select class="select2-example form-control" id="subjects" wire:model.lazy="subjects" name="subjects">
</select>
@push('scripts')
<script>
$('#subjects').select2({
maximumSelectionLength: 1,
minimumInputLength:2,
tags: false,
placeholder: 'Enter Subject(s)',
.... // this all works great
});
$('#subjects').on('change', function (e) {
let data = $(this).val();
@this.set('subjects', data);
});
// my event listener and it is working as well
Livewire.on('editSubject', subject => {
console.log(subject);
@this.set('subjects', subject);
$('#subjects').val(subject);
$('#subjects').trigger('change'); //the public view doesn't get updated
})
</script>
@endpush
到目前为止,我也尝试过浏览器调度事件。没有任何效果。解决方法是什么?任何帮助是极大的赞赏。
解决方案
在刀片
<div class="col d-flex display-inline-block">
<label for="contact_devices">{{ __('Select Device') }}</label>
<select id="contact_devices" wire:model="selectedDevice" class="form-control contact_devices_multiple" multiple="multiple" data-placeholder="{{ __('Select') }}">
@foreach($devices as $device)
<option value="{{ $device->id }}">{{ $device->alias }}</option>
@endforeach
</select>
</div>
<script>
window.loadContactDeviceSelect2 = () => {
$('.contact_devices_multiple').select2({
// any other option
}).on('change',function () {
livewire.emitTo('tenant.contact-component','devicesSelect',$(this).val());
});
}
loadContactDeviceSelect2();
window.livewire.on('loadContactDeviceSelect2',()=>{
loadContactDeviceSelect2();
});
</script>
在组件中
public $selectedDevice;
protected $listeners = [
'devicesSelect'
];
public function devicesSelect($data)
{
dd($data);
$this->selectedDevice = $data;
}
public function hydrate()
{
$this->emit('loadContactDeviceSelect2');
}
推荐阅读
- python - Xpath 在列表实例上执行时返回完整列表的结果
- html - 有没有办法让一个动画或者
当它的高度由于 html 值的变化而变化时?
- c++ - 使用 C++ 的 Freetype 无法正确加载所有字形
- vue.js - 我想显示存储在 store.js 文件中的状态数组中的任意值
- sql - SQL - 具有最小值、最大值的动态日期列
- pymodbus - pymodbus 自定义请求中的单元参数
- java - 如何使用 java 将漂亮格式的 json 文件转换为简单的单行 json 文件?
- java - 倒计时在 4 月和 10 月之间增加一小时
- elasticsearch - 聚合连接事件的序列
- javascript - 不确定如何在 useEffect 中包含函数