Answer the question
In order to leave comments, you need to log in
How to insert data from the database in DepDrop-select2?
Tell me how to insert data from the database into the second select, value for some reason does not work!
<?= $form->field($model, 'id')->label('')->dropDownList(ArrayHelper::map(Platforms::find()->all(), 'id', 'name'), ['id'=>'platform_id', 'prompt' => '- ' . Yii::t('main', 'Select platform') . ' -']) ?>
<?= $form->field($model, 'game_id')->label('')->widget(DepDrop::classname(), [
'type' => DepDrop::TYPE_SELECT2,
'options' => ['multiple' => true],
'disabled' =>false,
'pluginOptions'=>[
'depends'=>['platform_id'],
'placeholder'=>'- ' . Yii::t('main', 'Select games') . ' -',
'url'=>Url::to(['/user/default/getgame'])
]
])
?>
Answer the question
In order to leave comments, you need to log in
You can for example like this:
if (isset($model->game_id) ){
$gameValue = ArrayHelper::map(Game::find()->where(['id' => $model->game_id])->all(), 'id', 'name');
}else{
$gameValue = [];
<?= $form->field($model, 'game_id')->label('')->widget(DepDrop::classname(), [
'data' =>$ gameValue,
'type' => DepDrop::TYPE_SELECT2,
'options' => ['multiple' => true],
'disabled' =>false,
'pluginOptions'=>[
'depends'=>['platform_id'],
'placeholder'=>'- ' . Yii::t('main', 'Select games') . ' -',
'url'=>Url::to(['/user/default/getgame'])
]
])
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question