简体   繁体   English

laravel jquery select2 multiselect-初始化选定的元素

[英]laravel jquery select2 multiselect - initialize selected elements

Good morning everybody, I'm stuck while trying to set initial selected options (got through an ajax requests) to a jquery select2 (multi-select). 大家早上好,我在尝试将初始选择的选项(通过ajax请求获得)设置为jquery select2(多选)时遇到了麻烦。 If I use the control, multi selection works just fine. 如果我使用该控件,则多重选择效果很好。 The problem arise when I have to edit a previously saved model, as none of the options I previously selected are shown. 当我必须编辑以前保存的模型时出现问题,因为没有显示我先前选择的选项。

This is my view: 这是我的看法:

<div class="form-group {{ $errors->has('pubblicazione_giuridica_id') ? 'has-error' : ''}}"> 
    {!! Form::label('pubblicazione_giuridica_id', 'Law Pubs', ['class' => 'col-sm-3 control-label']) !!} 
    <div class="col-sm-6"> 
        {!! Form::select('pubblicazione_giuridica_id[]', [$pubGiurList], null, ['class' => 'form-control', 'id'=>'select-pgiur', 'multiple'=>'multiple']) !!} 
        {!! $errors->first('pubblicazione_giuridica_id', '<p class="help-block">:message</p>') !!} 
    </div> 

The javascript: JavaScript:

$('#select-pgiur').select2({ 
    ajax: { 
        url: "{!! URL::to('/gare/proc-getpubgiur') !!}", 
        dataType: 'json', 
        delay: 150, 
        data: function (params) { 
            return { q: params.term, };
        }, 
        processResults: function (data, params) {
            return { results: data.items, }; 
        }, 
        cache: true 
    }, 
    language: 'it', 
    theme: "bootstrap", 
    placeholder: "Choose an option."
});

The Controller: 控制器:

public function edit($id, Request $request) { 
    $procedure = $this->getProcedure($id, $request, true); 
    if (!$procedure) { 
        return redirect('gare/procedure')->with('alert-warning', 'Gara non trovata - Operazione non consentita'); 
    } 
    return view('gare.procedures.edit', [ 'procedure'=>$procedure, 'opChoiceList'=>$this->getOpChoiceList(), 'pubGiurList'=>json_encode(GaraPubblicazioneGiuridica::GetPubByGaraId($procedure->id)) ]); 
}

Model: 模型:

public static function GetPubByGaraId($id){
    //TODO: visualizzare le selected publications
    $procagg = GaraPubblicazioneGiuridica::where('gara_id','=',$id)->select('id')->get();
    if($procagg){
        $plucked = $procagg->pluck('nome');
        $toReturn = array();
        foreach($plucked as $key=>$value){
            $toReturn[$key]=$value;
        }
        return $toReturn;
    }
    return "";
}

Could you shed some light on this? 您能对此有所了解吗? Thank you in advance. 先感谢您。

You can create an ajax call on J-query Event and Bind the data through id. 您可以在J-query事件上创建ajax调用,并通过ID绑定数据。

$("#select-pgiur").val(data.items).trigger("change"); $( “#选择-pgiur”)VAL(data.items).trigger( “变”);

Maybe it helps you..... 也许可以帮助您.....

If I understand your end-goal correctly, you want to update the Select2 multi-select by selecting options after the Select2 has been initialized using data received from an Ajax request. 如果我正确理解了您的最终目标,那么您想在使用从Ajax请求接收的数据初始化Select2 之后 ,通过选择选项来更新Select2多重选择。

If that's the case, you're looking for the .trigger('change') event. 在这种情况下,您正在寻找.trigger('change')事件。

I personally use VueJS and hand-crafted forms, but this should point you in the right direction: 我个人使用VueJS和手工制作的表单,但这应该为您指明正确的方向:

$("#your_select2_id").val(selected_values_array).trigger("change");

The selected_values_array is a JS array that contains the values: <option value="1"></option> that you want selected. selected_values_array是一个JS数组,其中包含要选择的值: <option value="1"></option> The .trigger() then updates the Select2 to show them. 然后, .trigger()更新Select2以显示它们。

Hope that helps! 希望有帮助!

When you make a dropdown selector you can specify the initially selected options: 当您创建下拉选择器时,可以指定最初选择的选项:

Form::select(
   'pubblicazione_giuridica_id[]',
   [$pubGiurList],  
   $opChoiceList, 
  ['class' => 'form-control', 'id'=>'select-pgiur', 'multiple'=>'multiple']
)

However this means you need to pre-populate your AJAX select2 dropdown with the initially selected options. 但是,这意味着您需要使用最初选择的选项来预先填充AJAX select2下拉列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM