使用Laravel而不使用js时bootstrap-select中的默认值

vxbzzdmp  于 2022-11-18  发布在  Bootstrap
关注(0)|答案(1)|浏览(148)

我想在编辑select的时候使用bootstrap-select在里面显示默认值。我该怎么做呢?
bootstrap-select在数组中发送数据,但我可以将数组传递到数组中吗?我如何设置select的默认值?

默认数据

@foreach ($product->categories as $origin)
    {{$origin->category->id}} // <- This one needs to be sent
    {{$origin->category->name}} // <- This one needs to be displayed
@endforeach

我的选择:

<select name="category_id[]" values="" class="form-control selectpicker" multiple data-live-search="true">
   @foreach($categories as $category)
      <option value="{{$category->id}}">{{ $category->name }}</option>
   @endforeach
</select>

我试着用数组来做,所以我只把数组放入值中:

@foreach ($product->categories as $index=>$origin)
    {{$passTo[$index] = $origin->category->id}}
    {{$origin->category->name}}
@endforeach

"但有两个问题“
1.它在创建数组时输出值(因为{{}}就像刀片中的echo)
1.它不起作用:

<select name="category_id[]" values="{{$passTo}}" class="form-control selectpicker" multiple data-live-search="true">

我的第二个想法是将它们作为选定选项传递,但我不知道如何避免在两个foreach之间复制记录:

@foreach($product->categories as $origin)
    @foreach($categories as $category)
        @if ($category->id == $origin->category->id)
            <option value="{{$origin->category->id}}" selected>
                {{$origin->category->name}}
            </option>
        @else
            <option value="{{$category->id}}">
                {{ $category->name }}
            </option>
        @endif
    @endforeach
@endforeach

第二个解可能是最简单的一个,但我仍然不知道如何完成它:c
我想实现的目标屏幕(以防万一)

nuypyhwy

nuypyhwy1#

这可能不是最好的解决方案。但它很有效:)

@php
                    $taken = array();
                    foreach ( $product->categories as $origin )
                    {
                        array_push($taken, $origin->category);
                    }
                    $all = array();
                    foreach ($categories as $category)
                    {
                        array_push($all, $category);
                    }
                    $rest = array();
                    $rest = array_diff($all, $taken);
                @endphp

相关问题