I use plug-ins from Kartik DepDrop and Select2 In the second select I select several values ​​at once (multiple), I get an array, I loop through the data to the database. The question is how to re-open the page with selects to load the saved data. Here is my code

Использую плагины от Kartik DepDrop и Select2 Во втором селекте я выбираю сразу несколько значений(multiple), у меня получается массив Как мне сохранить это в базу, так чтобы при повторном открытие страницы с селектами у меня были уже выбранные ранее сохраненные значения. Вот моя форма <?php $form = ActiveForm::begin(['id' => 'games-form']);?> <?= $form->field($model, 'id')->label('')->dropDownList(ArrayHelper::map(Platforms::find()->all(), 'id', 'name'), ['id'=>'cat-id', 'prompt' => '- ' . Yii::t('main', 'Select platform') . ' -']) ?> <?= $form->field($model, 'game_id')->label('')->widget(DepDrop::classname(), [ 'type' => DepDrop::TYPE_SELECT2, 'options' => ['multiple' => true], 'pluginOptions'=>[ 'depends'=>['cat-id'], 'placeholder'=>'- ' . Yii::t('main', 'Select games') . ' -', 'url'=>Url::to(['/user/default/getgame']) ] ]) ?> <?= Html::submitButton(Yii::t('main', 'Save'), ['class' => 'btn btn-primary'])?> <?php ActiveForm::end(); ?> 

    3 answers 3

    collect your value in masivchik and stuff

     'value' => ['red', 'green'], // initial value 

    Below is a simple example straight from the widget documentation.

     use kartik\widgets\Select2; $data = [ "red" => "red", "green" => "green", "blue" => "blue", "orange" => "orange", "white" => "white", "black" => "black", "purple" => "purple", "cyan" => "cyan", "teal" => "teal" ]; // Tagging support Multiple echo '<label class="control-label">Tag Multiple</label>'; echo Select2::widget([ 'name' => 'color_1', 'value' => ['red', 'green'], // initial value 'data' => $data, 'options' => ['placeholder' => 'Select a color ...', 'multiple' => true], 'pluginOptions' => [ 'tags' => true, 'maximumInputLength' => 10 ], ]); 

    http://demos.krajee.com/widget-details/select2#usage-tags

    • cool to connect with DepDrop - Kostya
    • What exactly do you need? - Mackiavelly
    • you need to select a category in the select1 so that the records of the selected category are loaded in the second one, you can select several records and save, then be able to edit it all as well - Kostya

    Take a sample from the table:

     if (isset($model->field) ){ $value= ArrayHelper::map(Table::find()->where(['id' => $model->field])->all(), 'id', 'field'); }else{ $value= []; } 

    And already further:

     'data'=> $value, 

      In the database, save depending on your structure. To display the same data when editing, use the data parameter, and add data to it by type id => name:

       'data'=>[2 => 'Tablets'], 

      Read more: http://demos.krajee.com/widget-details/depdrop#advanced-usage

      • If you add 'data' => [2 => 'Tablets'], then it will appear in the drop-down list, and I need it to be already selected. 1. First you need to create these tags, 2 then the ability to edit them. - Kostya
      • I need to go to the edit page, I got it a.disquscdn.com/uploads/mediaembed/images/4256/6595/… - Kostya