Yii2使用dropdownlist实现地区三级联动功能的方法
分类:PHP作者:编程之家
本文实例讲述了Yii2使用dropdownlist实现地区三级联动功能的方法。分享给大家供大家参考,具体如下:
视图部分:
rush:
PHP;">
['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,['prompt' => '请选择区县'])->label('请选择区县','com
munityName',['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($com
munities,['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中需要直接组合成
之前是多表,需要使用jjoinWith()连表,后来被我全部转化为单表了,多表实在是慢,能转化成单表就用单表吧:
where($condition,$p);
$dataProvider = new ActiveDataProvider([
'query' => $query,'pagination' => [
'pageSize' => self::PAGE_SIZE,],]);
$this->load($p
arams);
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->com
munityName)) {
$query->andFilterWhere(['ll_hy_huangye_error.com
munityid' => intval($this->com
munityName)]);
}
$order = ' `ctime` DESC';
$query->orderBy($order);
return $dataProvider;
}
}
控制器中写比较简单一点,直接调用就行了:
rush:
PHP;">
/**
* ajax请求小区
*
* @p
aram $id
* @return string
*/
public function actionGetCom
munityList($id)
{
$option = '';
$result = self::getCom
munity($id);
if ($result) {
foreach ($result as $value) {
$option .= '
更多关于Yii相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。