当前位置: 动力学知识库 > 问答 > 编程问答 >

php - yii2 Multi select dropdown remember selected

问题描述:

I have a multi select dropdown menu, when I create the "project" it works just fine.

  1. If I want to edit the created "project" it doesn't remember what I selected in the dropdown menu when the "project" was created.

  2. It saves it to the database, it shows it is fine, but if you want to edit you need to select them again.

Is there anyway to make a auto-select where it selects them automatically according to the database? (I still want to be able to edit it, but I want them to be selected by default).

echo $form->field($model, 'company_id')->dropDownList($items_company, ['prompt' => 'Select Company','style' => 'width:400px;','onchange'=>'

$.post("index.php?r=project/lists&id='.'"+$(this).val(), function( data ) {

$( "select#client" ).html( data );

});

']);

if($model->client){

echo $form->field($model, 'client')

->dropDownList(

$items_client,

['id'=>'client','multiple' => true,'style' => 'width:400px;']

);

}

网友答案:

The easiest fix would be to just trigger the change() event of your company_id select box on document ready. Assuming the ID of your first select is client, here's the code:

$this->registerJs('$("select#client").trigger("change");', View::POS_READY);

However, I think you should try some widgets that handle dependent dropdowns, for example, this one.

分享给朋友:
您可能感兴趣的文章:
随机阅读: