YII+DWZ三级城市联动挂件

挂件PHP文件

class CountryCityCombox extends RXWidget {
	public $provinceId = 2; 
	public $cityId = 3687;
	public $regionId = 64;
        public $level = 3;  //限定联动层级 为2说明是2级联动 
	
	public function init() {
		$this->provinceId = intval ( $this->provinceId );
		$this->cityId = intval ( $this->cityId );
		$this->regionId = intval ( $this->regionId );
                $this->level = intval( $this->level );
	}

	public function run() {
		$province = Area::model ()->province()->findAll ();
		$province = CHtml::listData ( $province, 'id', 'name' );
		
		
		$city = array ();
		if($this->provinceId) {
			// city record
			$cri = new CDbCriteria ();
			$cri->condition = 'pid=:pid';
			$cri->params = array (
                ':pid' => $this->provinceId 
			);
			$city = Area::model ()->findAll ( $cri );

			$city = CHtml::listData ( $city, 'id', 'name' );
		}

		$region = array ();
		if($this->cityId) {
			//init region
			// city record
			$cri = new CDbCriteria ();
			$cri->condition = 'pid=:pid';
			$cri->params = array (
                      ':pid' => $this->cityId 
			);
			$region = Area::model ()->findAll ( $cri );

			$region = CHtml::listData ( $region, 'id', 'name' );
		}
                
                $views = array();
                $views['ccc_province'] = array (0 => '请选择省') + $province;
                $views['ccc_provinceId'] = $this->provinceId;
                $views['ccc_city'] = array (0 => '选择城市') + $city;
                $views['ccc_cityId'] = $this->cityId ;
                if ($this->level != 2) {
                    $views['ccc_region'] = array (0 => '选择区') + $region;
                    $views['ccc_regionId'] = $this->regionId ;
                }
                $views['level'] = $this->level;
                
		$this->render ( 'countrycitycombox', $views);
		 
	}
}

 挂件视图文件

<label>选择地区:</label>

<?php
$t = time () . uniqid ();
$htmlOptions = array (
    'class' => 'combox', 
    'ref' => 'combox_city' . $t, 
    'refUrl' => 'area/get?id={value}', 
    'id' => 'provinceId' . $t 
);

echo CHtml::DropDownList ( 'provinceId', $ccc_provinceId, $ccc_province, $htmlOptions );
?>

<?php
$htmlOptions = array (
    'class' => 'combox', 
    'ref' => 'combox_region' . $t, 
    'refUrl' => 'area/get?id={value}', 
    'id' => 'combox_city' . $t 
);

echo CHtml::DropDownList ( 'cityId', $ccc_cityId, $ccc_city, $htmlOptions );

$htmlOptions = array (
    'class' => 'combox', 
    'id' => 'combox_region' . $t 
);
if ($level != 2) {
    echo CHtml::DropDownList ( 'regionId', $ccc_regionId, $ccc_region, $htmlOptions );
}

 挂件视图调用

一.2级联动

 $widget=$this->Widget('CountryCityCombox',array(
        'level'=>2
  ));

 二.3级联动

 $widget=$this->Widget('CountryCityCombox');
原文地址:https://www.cnblogs.com/darktime/p/DWZ.html