【问题标题】:How to manage in edit items already selected如何在已选择的编辑项目中进行管理
【发布时间】:2019-06-14 08:18:44
【问题描述】:

我有一个 select2,它列出了数据库表的所有项目。其中一项已被选中。在编辑模式下,如何显示已选择的项目并列出其余项目?

在控制器中:

$offertaheader = Offertaheader::find($id_offertaheader);
$clients = Client::all()->where('visibility',1)
                        ->sortBy('nome');
return view('offertas.edit',compact('offertaheader','clients'));

IN VIEW BLADE:

<select class="form-control select2" name="id_cliente" style="width: 100%">
  <optgroup label="<?php echo htmlentities(utf8_encode('CLIENTE'), 0, "UTF-8"); ?>">
    <option></option>
    @foreach($clients as $client)
    <option value="{{$client->id}}" {{ (old('id_client', $client->id) == ($client ? $client->id : '') ? ' selected' : '') }}>{{$client->nome}}</option>
    @endforeach
</select>

我希望必须显示已选择的项目,同时用户可以在刀片中更改它。我更喜欢避免使用 jquery 或 ajax 等前端编码。

【问题讨论】:

    标签: laravel jquery-select2


    【解决方案1】:

    你可以使用同步方法更新,像这样,

    public function upate()
    {
        $todo = Todo::findOrFail($id);
        $data = $request->all();
        $todo->update($data);
        $users = $request->users;
        $users = User::find($users);
        $todo->users()->sync($users);
        return response()->json("success",200);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 2012-04-08
      • 2020-04-19
      • 2013-12-12
      • 2019-10-22
      相关资源
      最近更新 更多