首页 > 解决方案 > 编辑表单中的 Select2 不会触发编辑表单中的选定值 - Livewires

问题描述

我在 livewire 组件中有一个 select2。一切正常,但在我的编辑视图中,选定的选项不会显示在选中的框中。当我打开下拉菜单时,它们显示为突出显示,因此数据来自后端。

 <div  wire:ignore class="col-8">
                                        <select wire:model="notificationType" class="form-control select2 text-sm"
                                                multiple="multiple">
                                            @foreach($notificationTypeOptions as $key => $option)
                                                <option value="{{ $key }}">{{ $option }}</option>
                                            @endforeach
                                        </select>
                                    </div>

     $('.select2').select2({
                width: '100%',
                tags: true,
                multiple: "multiple",
            });

            $('.select2').on('change', function (e) {
                var data = $('.select2').val();
            @this.set('notificationType', data);
            });

请如果有人可以帮助...

标签: laraveljquery-select2livewires

解决方案


在刀片组件中使用此代码:

@if (in_array($key, $notificationType)) {{'selected'}} @endif

一切按原样仅在选择选项标签中发生变化:如下面的代码:

<option value="{{$key}}" @if (in_array($key, $notificationType)) {{'selected'}} @endif>{{ $option }}</option>

推荐阅读