【问题标题】:Select2 in edit form not triggering the selected values in edit form- Livewires编辑表单中的 Select2 不会触发编辑表单中的选定值 - Livewires
【发布时间】:2021-03-05 14:35:36
【问题描述】:

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

如果有人可以帮忙...

【问题讨论】:

    标签: laravel jquery-select2 livewires


    【解决方案1】:

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

    $('#edit-select').select2()
    $('#create-select').select2()
    

    【讨论】:

      【解决方案2】:

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

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

      一切都只在选择选项标签中改变:如下代码:

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

      【讨论】:

        猜你喜欢
        • 2021-09-13
        • 1970-01-01
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多