The first time to do the provincial and municipal three-level linkage encountered a pit, feeling or own too much food. The headache has long been studied for a long time, finally found the problem. Take a rough look at the idea
The model is instantiated in the controller, then all countries are rendered in the view, and when the country is selected, Ajax passes the country_id to the controller through the id= "community-country_id", and the controller passes the $this-> Ajaxgetprovince (); Get country_id the same province, and then render in the view of the country under the province, select the province, Ajax again through the id= "community-province_id" transfer province_id to the controller, controller through $this Ajaxgetcity (), get province_id the same city, and then show the saved city in the view.
1. In the view of the time
The first type: (because it is the regional management library, the national table, the provincial table, the city table three separate, but not the same table)
<divclass= "Form-group field-community-country_id required" > <labelclass= "Col-lg-1 Control-label" for= "community-country_id" > Area </label> <divclass= "Col-lg-8" > <?PHP$countryList=Array(); $countryList= Arrayhelper::map (Countryregion::find ()->where ([' is_deleted ' =>0])
->orderby ([' Order_num ' =>SORT_ASC, ' id ' =>sort_asc])->all (), ' id ', ' name '); ?> <selectid= "community-country_id" class= "Form-control" name= "community[country_id" "style=" Width:200px;display:inline-block "> <option value=" "Selec ted> Please select </option> <?phpforeach($countryList as $key=$val):?> <option value= "<?=$key?> "
<?=$modelCommunity->country_id! =NULL&&$modelCommunity->country_id = =$key? ' Selected ': '? >><?=$val?></option> <?phpEndforeach;?> </select> <?PHP$provinceList=Array(); if($modelCommunity-country_id) { $provinceList= Arrayhelper::map (Province::find ()
->where ([' country_id ' =$modelCommunity->country_id, ' is_deleted ' =>0])
->orderby ([' Order_num ' =>SORT_ASC, ' id ' =>sort_asc])->all (), ' id ', ' name '); } ?> <selectid= "community-province_id" class= "Form-control" name= "community[province_id" "style=" Width:200px;display:inline-block "> <option value=" "> Please select </option> <?phpforeach($provinceList as $key=$val):?> <option value= "<?=$key?> "
<?=$modelCommunity->province_id! =NULL&&$modelCommunity->province_id = =$key? ' Selected ': '?>>
<?=$val?></option> <?phpEndforeach;?> </select> <?PHP$cityList=Array(); if($modelCommunity-province_id) { $cityList= Arrayhelper::map (City::find ()->where ([' province_id ' = =$modelCommunity->province_id, ' is_deleted ' =>0])
->orderby ([' Order_num ' =>SORT_ASC, ' id ' =>sort_asc])->all (), ' id ', ' name '); } ?> <select id= "community-city_id" class= "Form-control" name= "community[city_id" "style=" Width:200px;display:inline-block "> <option value=" "> Please select & Lt;/option> <?phpforeach($cityList as $key=$val):?> <option value= "<?=$key?> "<?=$modelCommunity->city_id! =NULL&&$modelCommunity->city_id = =$key? ' Selected ': '? >><?=$val?></option> <?phpEndforeach;?> </select> </div></div>
The second kind: (In the school management, the country, the province, the city, the three are in the school table)
<divclass= "Form-group field-community-country_id required" > <labelclass= "Col-lg-1 Control-label" for= "community-country_id" > Area </label> <divclass= "Col-lg-8" > <?PHP$countryList=Array(); $countryList= Arrayhelper::map (Countryregion::find ()->where ([' is_deleted ' = 0])
->orderby ([' order_num ' = + SORT_ASC, ' id ' = = sort_asc])->all (), ' id ', ' name '); ?> <selectid= "community-country_id" class= "Form-control" name= "school[country_id]"style= "Width:200px;display:inline-block" > <option value= "" Selected> Please select </option> <?phpforeach($countryList as $key=$val):?> <option Value= "<?=$key?> "<?=$model->country_id! =NULL&&$model->country_id = =$key? ' Selected ': '? >><?=$val?></option> <?phpEndforeach;?> </select> <?PHP$provinceList=Array(); if($model country_id) { $provinceList= Arrayhelper::map (Province::find ()->where ([' country_id ' = =$model->country_id, ' is_deleted ' = 0])
->orderby ([' order_num ' = + SORT_ASC, ' id ' = = sort_asc])->all (), ' id ', ' name '); } ?> <selectid= "community-province_id" class= "Form-control" name= "school[province_id]"style= "Width:200px;display:inline-block" > <option value= "" > Please select </option> <?phpforeach($provinceList as $key=$val):?> <option Value= "<?=$key?> "<?=$model->province_id!=NULL&&$model->province_id = =$key? ' Selected ': '? >><?=$val?></option> <?phpEndforeach;?> </select> <?PHP$cityList=Array(); if($model-province_id) { $cityList= Arrayhelper::map (City::find ()->where ([' province_id ' = =$model->province_id,' is_deleted ' = 0])
->orderby ([' order_num ' = + SORT_ASC, ' id ' = = sort_asc])->all (), ' id ', ' name '); } ?> <select id= "community-city_id" class= "Form-control" name= "school[city_id]"style= "Width:200px;display:inline-block" > <option value= "" > Please select </option> <?phpforeach($cityList as $key=$val):?> <option Value= "<?=$key?> "<?=$model->city_id!=NULL&&$model->city_id = =$key? ' Selected ': '? >><?=$val?></option> <?phpEndforeach;?> </select> </div></div>
The Third Kind: (Only the province and the city linkage, the country is China)
<?PHP$provinceList= Arrayhelper::map (Province::find ()->where ([' country_id ' = + 142])->all (), ' id ', ' name ');? ><?=$form->field ($model, ' province_id ')->dropdownlist ($provinceList, [' Prompt ' = ' Please select ', ' style ' = ' width:200px ',' id ' = ' community-province_id '])? ><?PHP$cityList=Array();if($model-province_id) { $cityList= Arrayhelper::map (City::find ()->where ([' province_id ' = =$model->PROVINCE_ID])->all (), ' id ', ' name ');}? ><?=$form->field ($model, ' city_id ')->dropdownlist ($cityList, [' Prompt ' = ' Please select ', ' style ' = ' width:200px ',' id ' = ' community-city_id '])?>
2. In the controller
The first type:
//Add Region Public functionactioncreate () {$this->ajaxgetprovince ();//used as a city linkage this paragraph is very important, because needs to obtain the front desk to pass over the country_id, therefore cannot lose $CountryModel=NewCountryRegion (); $ProvinceModel=Newprovince (); $CityModel=NewCity (); $CityProvince=Newprovince (); /** Other code * / return $this->render (' Create ',[ ' Level ' =$level, ' countrymodel ' =$CountryModel, ' provincemodel ' =$ProvinceModel, ' citymodel ' =$CityModel, ' cityprovince ' =$CityProvince ]); }
The second and third controllers have the same code as the following:
Public Function Actioncreate ()
{
$this->ajaxgetprovince (); As a city linkage
$this->ajaxgetcity (); As a city linkage
$model = new School ();
/** Middle Other Code */
return $this->render (' Create ', [
' Model ' = $model,
]);
3. Front End
/*national and provincial linkage ActiveRecord*/$ (document). On (' Change ', ' #community-country_id ',function () { var$select = $ ( This), Requesturl= $select. attr (' Data-country-url ') | |window.location.href; if($select. attr (' data-requestform ') = = = ' true ') {Requesturl= $select. Parents (' form '). attr (' action ')); } $(' #community-province_id '). html (' <option value= ' > Please select </option> '); $(' #community-city_id '). html (' <option value= ' > Please select </option> '); if($select. Val ()) {$.ajax ({url:requesturl, data: {country_id: $select. V Al (), Ajax_get_province:true}, type:"GET", Success:function(e) {$ (' #community-province_id '). HTML (e); $(' #community-city_id '). html (' <option value= ' > Please select </option> '); Console.log (e); } }); }});/*provincial and Urban linkage ActiveRecord*/$ (document). On (' Change ', ' #community-province_id ',function () { var$select = $ ( This), Requesturl= $select. attr (' Data-province-url ') | |window.location.href; if($select. attr (' data-requestform ') = = = ' true ') {Requesturl= $select. Parents (' form '). attr (' action ')); } $(' #community-city_id '). html (' <option value= ' > Please select </option> '); if($select. Val ()) {$.ajax ({url:requesturl, data: {province_id: $select. Val (), Ajax_get_city:true}, type:"GET", Success:function(e) {$ (' #community-city_id '). HTML (e); Console.log (e); } }); }});
Above ~ ~
The first time you do this, there should be an easier way to ask for expert guidance.
Reproduced herein please specify the source.
[Moka students ' notes] Three-level linkage of state provinces and cities under Yii