Yii2 how to use dropdownlist to implement regional three-level linkage function _ php instance

Source: Internet
Author: User
This article mainly introduces how to use dropdownlist in Yii2 to implement regional three-level linkage function. combined with the instance form, it analyzes in detail the specific steps and precautions for implementing three-level linkage call in the dropdownlist drop-down list, for more information about how to use dropdownlist in Yii2, see the following example. We will share this with you for your reference. The details are as follows:

View section:

<? Phpuse yii \ helpers \ Url; use yii \ widgets \ ActiveForm; use yii \ helpers \ ArrayHelper; use yii \ helpers \ Html; /* @ var $ this yii \ web \ View * // * @ var $ model common \ search \ service \ ItemSearch * // * @ var $ form yii \ widgets \ ActiveForm * /?>

<? Php $ form = ActiveForm: begin (['action' => ['index'], 'method' => 'GET ', 'options' => ['class' => 'form-inline']);?> <? = $ Form-> field ($ model, 'cityname', ['options' => ['class' => 'form-group col-lg-2 ']) -> dropDownList (ArrayHelper: map ($ cities, 'id', 'name'), ['propt' => 'Select city ']) -> label ('select city ', ['class' => 'Sr-only'])?> <? = $ Form-> field ($ model, 'areaname', ['options' => ['class' => 'form-group col-lg-2 ']) -> dropDownList (ArrayHelper: map ($ areas, 'id', 'name'), ['proppt' => 'Select a district/county ']) -> label ('select the district/county ', ['class' => 'Sr-only'])?> <? = $ Form-> field ($ model, 'communityname', ['options' => ['class' => 'form-group col-lg-2 ']) -> dropDownList (ArrayHelper: map ($ communities, 'id', 'name'), ['propt' => 'Select a cell ']) -> label ('select a cell ', ['class' => 'Sr-only'])?>

Search

<? = Html: submitButton ('search', ['class' => 'btn btn-primary '])?>

<? Php ActiveForm: end ();?>

<? Php $ this-> registerJs ('// City Address Change $ ("# itemsearch-cityname "). change (function () {// City id value var cityid = $ (this ). val (); $ ("# itemsearch-areaname" pai.html ("Select a district/county"); $ (" # Itemsearch-communityname "pai.html ("Select a residential area"); If (cityid> 0) {getArea (cityid) ;}}); // change the area address $ (" # itemsearch-areaname "). change (function () {// zone id value var areaid = $ (this ). val (); $ ("# itemsearch-communityname" pai.html ("Select a residential area"); If (areaid> 0) {getCommunity (areaid) ;}}); // Obtain the area list function getArea (id) {var href = "'. url: to (['/service/base/get-area-list'], true ). '"; $. ajax ({"type": "GET", "url": href, "data": {id: id}, success: function (d) {$ ("# itemsearch-areaname "). append (d) ;}}) ;}// obtain the cell list function getCommunity (id) {var href = "'. url: to (['/service/base/get-community-list'], true ). '"; $. aja X ({"type": "GET", "url": href, "data": {id: id}, success: function (d) {$ ("# itemsearch-communityname "). append (d) ;}}) ;}// search for a cell $ ("# search-community "). click (function () {var word = $ ("# keyword "). val (); var areaid = $ ("# itemsearch-areaname option: selected "). val (); var href = "'. url: to (['/service/base/search-community'], true ). '"; if (areaid> 0) {$. ajax ({"type": "GET", "url": href, "Data": {id: areaid, word: word}, success: function (d) {$ ("# itemsearch-communityname" ).html (d) ;}}) ;}}); ') ;?>

Model section:

Is our commonly used ajax requests, of course, php needs to be directly combinedThis structure is used directly. variable data tables in $ form-> field ($ model, $ var) are not necessarily included. you must define them in the model and set security fields, in addition, you may need to modify the search model to what you need. The model may look like this:

class HuangYeError extends \yii\db\ActiveRecord{  public $cityName;  public $areaName;  public $communityName;  public $group;  public $cate;  /**   * @inheritdoc   */  public static function tableName()  {    return 'll_hy_huangye_error';  }  public static function getDb()  {    return Yii::$app->get('dbnhuangye');  }}

I used to connect multiple tables with jjoinWith (). later I converted all tables into a single table. it was really slow to convert multiple tables into a single table. I would just use a single table if I could convert them into a single table:

Class HuangYeErrorSearch extends HuangYeError {const PAGE_SIZE = 20; public $ communityName; public $ startTime; public $ endTime;/*** @ inheritdoc */public function rules () {return [['id', 'serviceid', 'userid', 'categoryid', 'communityid', 'sortorder', 'ctime', 'utime ', 'status'], 'integer'], [['username', 'name', 'logo ', 'phone', 'address', 'content', 'error ', 'communityname', 'starttime', 'en Dtime'], 'Safe '],];}/*** @ inheritdoc */public function scenarios () {// bypass scenarios () implementation in the parent class return Model:: scenarios ();}/*** Creates data provider instance with search query applied ** @ param array $ params ** @ return ActiveDataProvider */public function search ($ params) {$ query = HuangYeError: find (); // status = 9 delete status $ condition = ''status '! =: Status '; $ p [': status'] = 9; $ query-> where ($ condition, $ p ); $ dataProvider = new ActiveDataProvider (['query' => $ query, 'pagination' => ['pagesize' => self: PAGE_SIZE,],]); $ this-> load ($ params); if (! $ This-> validate ()) {// uncomment the following line if you do not want to any records when validation fails // $ query-> where ('0 = 1'); return $ dataProvider ;} $ query-> andFilterWhere (['userid' => $ this-> userid]); $ query-> andFilterWhere (['like', 'username ', $ this-> username])-> andFilterWhere (['like', 'name', $ this-> name])-> andFilterWhere (['like', 'phone ', $ this-> phone])-> andFilterWhere (['like', 'address', $ this-> address])-> andFilterWhere (['like', 'content ', $ this-> content])-> andFilterWhere (['ll _ hy_huangye_error.status '=> $ this-> status]) -> andFilterWhere (['ll _ hy_huangye_error.categoryid '=> $ this-> categoryid])-> andFilterWhere (['between', 'ctime', strtotime ($ this-> startTime. '0: 0: 0'), strtotime ($ this-> endTime. '23: 59: 59')])-> andFilterWhere (['like', 'error', $ this-> error]); if (intval ($ this-> communityName) {$ query-> andFilterWhere (['ll _ hy_huangye_error.communityid '=> intval ($ this-> communityName)]);} $ order = 'ctime' DESC '; $ query-> orderBy ($ order); return $ dataProvider ;}}

It is easier to write data in the controller. just call it directly:

/*** Ajax request cell ** @ param $ id * @ return string */public function actionGetCommunityList ($ id) {$ option = ''; $ result = self :: getCommunity ($ id); if ($ result) {foreach ($ result as $ value) {$ option. =''. $ Value ['name'].'';}} Else {$ option. ='You have not activated an available community.';} Echo $ option ;}

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.