How to implement the linkage event of dropDownList in Yii (code)

不言
Release: 2023-04-04 07:34:02
Original
2018 people have browsed it

The content of this article is about how Yii implements the linkage event (code) of dropDownList. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Due to functional requirements, linkage needs to be used, hereby record and share

1. View

<div>

    <?php  $form = ActiveForm::begin(); ?>


    = $form->field($model, 'cid')->dropDownList(
        \common\models\service\common\Category::getCateOptions(),
        [
            'prompt' => '请选择单位',
            'onchange'=>'
                $.post("/carousel/main/cate?id='.'"+$(this).val(),function(data){
                    $("select#main-aid").html(data);
                });',
        ]
    )->hint('父级,必选') ?>
    = $form->field($model, 'aid')->dropDownList( \common\models\service\common\Category::getCateOptions(), ['prompt' => '请选择子级']) ?>
    <div>
        = Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php  ActiveForm::end(); ?>

</div>
Copy after login

2. Controller

ublic function actionCate($id)
{
    $departments = Category::find()
        ->where(['parent_id' => $id])
        ->asArray()
        ->all();

    if(!$departments){
        echo "<option>"  . "</option>";
    }
    foreach ($departments as $department) {
        echo "<option>" . $department['name'] . "</option>";
    }
}
Copy after login

3. Realize the effect

How to implement the linkage event of dropDownList in Yii (code)

Related recommendations:

Yii2 uses dropdownlist to realize the regional three-level linkage function

Realization of mutual linkage between dropdownlists (showing and hiding)_javascript skills

The above is the detailed content of How to implement the linkage event of dropDownList in Yii (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!