The example in this article describes how Yii2 uses dropdownlist to implement the regional three-level linkage function. Share it with everyone for your reference, the details are as follows:
View section:
<?php use 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 */ ?> <div class="row"> <div class="item-search"> <?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'), ['prompt' => '请选择城市'])->label('请选择城市', ['class' => 'sr-only']) ?> <?= $form->field($model, 'areaName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($areas, 'id', 'name'), ['prompt' => '请选择区县'])->label('请选择区县', ['class' => 'sr-only']) ?> <?= $form->field($model, 'communityName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($communities, 'id', 'name'), ['prompt' => '请选择小区'])->label('请选择小区', ['class' => 'sr-only']) ?> <div class="col-lg-2 col-lg-offset-1"> <input class="form-control" id="keyword" placeholder="请输入小区名" value="" /> </div> <div class="col-lg-1"> <button type="button" id="search-community" class="btn btn-info">搜索</button> </div> <p></p> <div class="form-group col-lg-1 pull-right"> <?= Html::submitButton('搜索', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> </div> </div> <p> </p> <?php $this->registerJs(' //市地址改变 $("#itemsearch-cityname").change(function() { //市id值 var cityid = $(this).val(); $("#itemsearch-areaname").html("<option value=\"0\">请选择区县</option>"); $("#itemsearch-communityname").html("<option value=\"0\">请选择小区</option>"); if (cityid > 0) { getArea(cityid); } }); //区地址改变 $("#itemsearch-areaname").change(function() { //区id值 var areaid = $(this).val(); $("#itemsearch-communityname").html("<option value=\"0\">请选择小区</option>"); if (areaid > 0) { getCommunity(areaid); } }); //获取市下面的区列表 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); } }); } //获取区下面的小区列表 function getCommunity(id) { var href = "' . Url::to(['/service/base/get-community-list'], true) . '"; $.ajax({ "type" : "GET", "url" : href, "data" : {id : id}, success : function(d) { $("#itemsearch-communityname").append(d); } }); } //搜索小区 $("#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:
It is our commonly used ajax request. Of course, it needs to be directly combined into a structure like in php. Use it directly, in $form->field($model, $var) It may not be in the variable data table. You have to define it yourself in the model and set up security fields. Moreover, the search model may also need to be modified 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'); } }
It used to be multiple tables, and you needed to use jjoinWith() to connect the tables. Later, I converted them all into a single table. Multiple tables are really slow. If you can convert them into a single table, just use 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', '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 ActiveDataProvider */ public function search($params) { $query = HuangYeError::find(); //status == 9 删除状态 $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; } }
Writing in the controller is simpler, just call it directly:
/** * ajax请求小区 * * @param $id * @return string */ public function actionGetCommunityList($id) { $option = ''; $result = self::getCommunity($id); if ($result) { foreach ($result as $value) { $option .= '<option value="' . $value['id'] . '">' . $value['name'] . '</option>'; } } else { $option .= '<option value="0">暂未开通可选择的小区</option>'; } echo $option; }
Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction to PHP Object-Oriented Programming" Tutorial", "php string usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.