简体   繁体   English

yii2多选下拉菜单记得选择

[英]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. 最简单的解决方法是仅在准备好文档时触发company_id选择框的change()事件。 Assuming the ID of your first select is client , here's the code: 假设您第一个选择的ID是client ,则代码如下:

$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 . 但是,我认为您应该尝试一些处理依赖下拉菜单的小部件,例如this

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

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