This paper describes the method of Yii2 using DropDownList to achieve the regional three-level linkage function. Share to everyone for your reference, 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 */?> &L t;? PHP $form = activeform::begin ([' action ' = = [' index '], ' method ' = ' get ', ' options ' = [' class ' =& Gt ' Form-inline ']);?> <?= $form->field ($model, ' cityname ', [' Options '] and [' class ' = ' Form-group col- Lg-2 '])->dropdownlist (Arrayhelper::map ($cities, ' id ', ' name '), [' Prompt ' = ' Please select City '])->label (' Please select City ', [' Class ' = ' sr-only '])?> <?= $form->field ($model, ' areaname ', [' options ' = ' = ' class ' = ' Form-group c Ol-lg-2 '])->dropdownlist (Arrayhelper::map ($areas, ' id ', ' name '), [' Prompt ' = ' Please select County '])->label (' Please select County ', [' Class ' = ' sr-only '])?> <?= $form->field ($model, ' communityname ', [' options ' = ' = ' class ' = ' form-gr OUP Col-lg-2 '])->dropdownlist (arrayhElper::map ($communities, ' id ', ' name '), [' Prompt ' = ' Please select cell '])->label (' Please select cell ', [' class ' = ' sr-only '])?> Search <?= Html::submitbutton (' Search ', [' class ' = ' btn btn-primary '])?> <?php activeform::end ();?><?php$this->registerjs ('///City address changed $ ("#itemsearch-cityname"). Change (function () {//city ID value var Cityid = $ (this). V Al (); $ ("#itemsearch-areaname"). HTML ("Please select District"); $ ("#itemsearch-communityname"). HTML ("Please select community"); if (Cityid > 0) {getarea (Cityid); } }); The zone address changes $ ("#itemsearch-areaname"). Change (function () {//zone id value var Areaid = $ (this). Val (); $ ("#itemsearch-communityname"). HTML ("Please select community"); if (Areaid > 0) {getcommunity (areaid); } }); Get the list of zones below the city function Getarea (id) {var href = "'. Url::to (['/service/base/get-area-list '], true). '"; $.ajax ({"Type": "GET", "url": href, "data": {Id:id}, Success:function (d) {$ ("#itemsea Rch-areaname "). Append (d); } }); }//Get zone below the cell list function getcommunity (id) {var href = "'. Url::to (['/service/base/get-community-list '], true). '"; $.ajax ({"Type": "GET", "url": href, "data": {Id:id}, Success:function (d) {$ ("#itemsea Rch-communityname "). Append (d); } }); }//Search 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 part:
Is our usual Ajax request, and of course PHP needs to be combined directly intoSuch a structure is used directly, $form->field ($model, $var) in the Variable data table does not necessarily have to be defined in the model, and set security fields, and the search model may need to be modified to their own needs, the model may be:
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 ');} }
Before is the multi-table, needs to use the Jjoinwith () the table, later I all converted to the single table, the multi-table is actually slow, can convert into the single table to use the single table bar:
Class Huangyeerrorsearch extends huangyeerror{const PAGE_SIZE = 20; Public $communityName; Public $startTime; Public $endTime; /** * @inheritdoc * * * Public Function rules () {return [[' ID ', ' serviceid ', ' userid ', ' CategoryID ', ' commun Ityid ', ' sortorder ', ' ctime ', ' utime ', ' status '], ' integer ', [[' Username ', ' name ', ' logo ', ' phone ', ' address ', ' cont Ent ', ' Error ', ' CommunityName ', ' startTime ', ' endTime '], ' 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 Activedat Aprovider */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::P age_size,],]); $this->load ($params); if (! $this->validate ()) {//Uncomment the following line if you don't 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; }}
The controller writes in a simple, direct call on the line:
/*** 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. = 'Have not yet opened the alternative Community '; } echo $option;}
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.