In yii2, dropDownList implements two-level and three-level linkage writing,

Source: Internet
Author: User

In yii2, dropDownList implements two-level and three-level linkage writing,

Sort out the document, search out the code of the dropDownList in yii2 to implement the second-level and third-level linkage writing, and sort it down and share it.

View page:

<? Php $ form = ActiveForm: begin (['action' => ['index'], 'method' => 'get',]);?> <! -- Level-1 directory --> <? = $ Form-> field ($ model, 'cocate _ id')-> dropDownList (Helper: courseCateMap (), ['propt' => yii :: t ('backend', 'Please select course cate')])?> <! -- Level 2 Directory --> <? = $ Form-> field ($ model, 'course _ id')-> dropDownList (Helper: courseMap ($ model-> cocate_id), ['propt' => yii:: t ('backend', 'Please select first course cate')])?> <! -- Level 3 directory --> <? = $ Form-> field ($ model, 'person _ id')-> dropDownList (Helper: personMap (1, $ model-> cocate_id ), ['propt' => yii: t ('backend', 'Please select person')])?> <? Php ActiveForm: end ();?> Page nested js <? Php $ js = '// classification $ ("# classsearch-cocate_id "). change (function () {var cocateId = $ (this ). val (); // get the value of the first-level directory $ ("# classsearch-course_id" cmd.html ("<option value = \" \ "> '. yii: t ('backend', 'Please select course '). '</option> "); // second-level display directory tag $ (" # classsearch-person_id ").html (" <option value = \ "\"> '. yii: t ('backend', 'Please select person '). '</option> "); // Three-Level Display directory label (if you only need two or three levels, you can delete them directly) if (cocateId> 0) {getCourse (coca TeId); // The getPerson (cocateId) method for querying second-level directories; // The method for querying third-level directories (if you only need second-level three, you can delete them directly )}}); function getCourse (cocateId) {var href = "'. url: to (['/ajax/option']). '"; // request address $. ajax ({"type": "GET", "url": href, "data": {cocateId: cocateId, type: "course "}, // required parameter and type success: function (d) {$ ("# classsearch-course_id "). append (d); // return value output});} function getPerson (cocateId) {var href = "'. url: to (['/ajax/option']). '"; // Same as above $. ajax ({"type": "GET", "url": href, "data": {cocateId: cocateId, type: "person "}, // required parameter and type success: function (d) {$ ("# classsearch-person_id "). append (d); // same as above});} '; $ this-> registerJs ($ js);?>

Php code:

This is the Controller declared by ajax:

<?php namespace backend\controllers;class AjaxController extends BaseController{    public function actionOption($cocateId, $type)  {    switch ($type) {      case 'course':        $_data = Helper::courseMap($cocateId);        break;      case 'person':        $_data = Helper::personMap(1, $cocateId);        break;      case 'class':        $_data = Helper::classMap($cocateId);        break;    }      $_tmp = '';    foreach ($_data as $key => $val) {      $_tmp .= "<option value='" . $key . "'>{$val}</option>";    }    echo $_tmp;  }

The classes encapsulated by Helper. php can be referenced in the configuration by creating files separately:

<? Phpnamespace backend \ components; // The inherited space Path class Helper // Declaration class {// declare the query method level public static function courseCateMap () {$ _ data = CourseCate :: find ()-> select ('cocate _ id, cocate_name ')-> all (); $ _ data = ArrayHelper: map (array_merge ($ _ data ), 'cocate _ id', 'cocate _ name'); return $ _ data;} // declare the second-level public static function courseMap ($ cocateId) of the query method) {$ condition ['cocate _ id'] = $ cocateId; $ _ data = Course: find ()-> sele Ct ('course _ id, course_name ')-> where ($ condition)-> all (); $ _ data = ArrayHelper: map (array_merge ($ _ data ), 'Course _ id', 'course _ name'); return $ _ data;} // declare the three-level public static function personMap ($ percateId, $ cocateId = 0) of the query method) {$ shopId = Yii: $ app-> user-> identity-> shop_id; $ condition = []; if ($ shopId) {$ condition ['shop _ id'] = $ shopId;} if ($ percateId) {$ condition ['percate _ id'] = $ percateId;} if ($ CocateId) {$ condition ['cocate _ id'] = intval ($ cocateId);} $ _ data = Person: find ()-> select ('person _ id, person_name ')-> where ($ condition)-> all (); $ _ data = ArrayHelper: map (array_merge ($ _ data), 'person _ id ', 'person _ name'); return $ _ data ;}}?>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.