Yii2使用dropdownlist实现地区三级联动功能的方法

前端之家收集整理的这篇文章主要介绍了Yii2使用dropdownlist实现地区三级联动功能的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了Yii2使用dropdownlist实现地区三级联动功能方法分享给大家供大家参考,具体如下:

视图部分:

PHP;">
['index'],'method' => 'get','options' => ['class' => 'form-inline'] ]); ?> field($model,'cityName',['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($cities,'id','name'),['prompt' => '请选择城市'])->label('请选择城市',['class' => 'sr-only']) ?> field($model,'areaName',['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($areas,['prompt' => '请选择区县'])->label('请选择区县','communityName',['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($communities,['prompt' => '请选择小区'])->label('请选择小区',['class' => 'sr-only']) ?>
搜索

'btn btn-primary']) ?>

registerJs(' //市地址改变 $("#itemsearch-cityname").change(function() { //市id值 var cityid = $(this).val(); $("#itemsearch-areaname").html("

模型部分:

就是我们常用的ajax请求,当然PHP中需要直接组合成

get('dbnhuangye'); } }

之前是多表,需要使用jjoinWith()连表,后来被我全部转化为单表了,多表实在是慢,能转化成单表就用单表吧:

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',$this->name]) ->andFilterWhere(['like',$this->phone]) ->andFilterWhere(['like',$this->address]) ->andFilterWhere(['like',$this->content]) ->andFilterWhere(['ll_hy_huangye_error.status' => $this->status]) ->andFilterWhere(['ll_hy_huangye_error.categoryid' => $this->categoryid]) ->andFilterWhere(['between',strtotime($this->startTime . '0:0:0'),strtotime($this->endTime . '23:59:59')]) ->andFilterWhere(['like',$this->error]); if (intval($this->communityName)) { $query->andFilterWhere(['ll_hy_huangye_error.communityid' => intval($this->communityName)]); } $order = ' `ctime` DESC'; $query->orderBy($order); return $dataProvider; } }

控制器中写比较简单一点,直接调用就行了:

PHP;"> /** * ajax请求小区 * * @param $id * @return string */ public function actionGetCommunityList($id) { $option = ''; $result = self::getCommunity($id); if ($result) { foreach ($result as $value) { $option .= '

更多关于Yii相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

猜你在找的PHP相关文章