0

我在 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);
            });

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

4

2 回答 2

0

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

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

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

<option value="{{$key}}" @if (in_array($key, $notificationType)) {{'selected'}} @endif>{{ $option }}</option>
于 2021-07-01T13:21:59.853 回答
0

为您的选择框使用单独的 id 并按 id 调用 select2

$('#edit-select').select2()
$('#create-select').select2()
于 2021-03-05T16:53:42.813 回答