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

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

视图部分:

rush: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']) ?>
munity" class="btn btn-info">搜索

'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; } }

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

rush: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程序设计有所帮助。

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...