注意事项:
如果是在admin这个modules下的CRUD,则'url'=>Yii::app()->createUrl('admin/TblMembers/dynamicCity')这样的形式。
如果是在前台,则为:'url'=>Yii::app()->createUrl('TblMembers/dynamicCity')
另外:'url'=>Yii::app()->createUrl('admin/TblMembers/dynamicCity')这里的TblMembers一定要区分大小写,否则会出错。这里最好去复制模型的名称,以防出错。
访问不了,有时会是控制器下的accessRules没有设置好。
代码如下:
控制器
<?php class TblConsigneeController extends Controller { /** * @var string the default layout for the views. Defaults to '//layouts/column2', meaning * using two-column layout. See 'protected/views/layouts/column2.php'. */ public $layout='//layouts/column2'; /** * @return array action filters */ public function filters() { return array( 'accessControl', // perform access control for CRUD operations 'postOnly + delete', // we only allow deletion via POST request ); } /** * Specifies the access control rules. * This method is used by the 'accessControl' filter. * @return array access control rules */ public function accessRules() { return array( array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=>array('index','view'), 'users'=>array('*'), ), array('allow', // allow authenticated user to perform 'create' and 'update' actions //'actions'=>array('create','update'), //改这里的访问权限,以下为改变后的 //这一步是非常重要的 'actions'=>array('create','update','dynamicCity','dynamicDistrict'), 'users'=>array('@'), ), array('allow', // allow admin user to perform 'admin' and 'delete' actions 'actions'=>array('admin','delete'), 'users'=>array('admin'), ), array('deny', // deny all users 'users'=>array('*'), ), ); } /** * Displays a particular model. * @param integer $id the ID of the model to be displayed */ public function actionView($id) { $this->render('view',array( 'model'=>$this->loadModel($id), )); } /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model=new TblConsignee; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['TblConsignee'])) { $model->attributes=$_POST['TblConsignee']; if($model->save()) $this->redirect(array('view','id'=>$model->con_id)); } $this->render('create',array( 'model'=>$model, )); } /** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model=$this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['TblConsignee'])) { $model->attributes=$_POST['TblConsignee']; if($model->save()) $this->redirect(array('view','id'=>$model->con_id)); } $this->render('update',array( 'model'=>$model, )); } /** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'admin' page. * @param integer $id the ID of the model to be deleted */ public function actionDelete($id) { $this->loadModel($id)->delete(); // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if(!isset($_GET['ajax'])) $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); } /** * Lists all models. */ public function actionIndex() { $dataProvider=new CActiveDataProvider('TblConsignee'); $this->render('index',array( 'dataProvider'=>$dataProvider, )); } /** * Manages all models. */ public function actionAdmin() { $model=new TblConsignee('search'); $model->unsetAttributes(); // clear any default values if(isset($_GET['TblConsignee'])) $model->attributes=$_GET['TblConsignee']; $this->render('admin',array( 'model'=>$model, )); } /** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return TblConsignee the loaded model * @throws CHttpException */ public function loadModel($id) { $model=TblConsignee::model()->findByPk($id); if($model===null) throw new CHttpException(404,'The requested page does not exist.'); return $model; } /** * Performs the AJAX validation. * @param TblConsignee $model the model to be validated */ protected function performAjaxValidation($model) { if(isset($_POST['ajax']) && $_POST['ajax']==='tbl-consignee-form') { echo CActiveForm::validate($model); Yii::app()->end(); } } //增加的代码,三级省市联动 public function actionDynamicCity($pid,$typeid=0) { //增加的代码,三级省市联动 //将Member改为TblConsignee这个模型 //$model = Member::model()->getCityList($pid,$typeid); $model = TblConsignee::model()->getCityList($pid,$typeid); if($typeid==1){$aa="-请选择市-";}else if($typeid==2 && $model){$aa="-请选择区-";} echo CHtml::tag('option', array('value'=>'empty'), $aa, true); foreach($model as $value=>$name) { echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true); } } }
模型:<?php /** * This is the model class for table "tbl_consignee". * * The followings are the available columns in table 'tbl_consignee': * @property string $con_id * @property string $con_name * @property string $con_address * @property string $con_mobile * @property string $members_id * @property string $province * @property string $city * @property string $district * @property integer $is_default */ class TblConsignee extends CActiveRecord { //增加的代码,三级省市联动 public $aa; /** * @return string the associated database table name */ public function tableName() { return 'tbl_consignee'; } /** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('is_default', 'numerical', 'integerOnly'=>true), array('con_name, con_mobile', 'length', 'max'=>16), array('con_address', 'length', 'max'=>64), array('members_id, province, city, district', 'length', 'max'=>8), // The following rule is used by search(). // @todo Please remove those attributes that should not be searched. array('con_id, con_name, con_address, con_mobile, members_id, province, city, district, is_default', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( ); } /** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { return array( 'con_id' => 'Con', 'con_name' => 'Con Name', 'con_address' => 'Con Address', 'con_mobile' => 'Con Mobile', 'members_id' => 'Members', 'province' => 'Province', 'city' => 'City', 'district' => 'District', 'is_default' => 'Is Default', ); } /** * Retrieves a list of models based on the current search/filter conditions. * * Typical usecase: * - Initialize the model fields with values from filter form. * - Execute this method to get CActiveDataProvider instance which will filter * models according to data in model fields. * - Pass data provider to CGridView, CListView or any similar widget. * * @return CActiveDataProvider the data provider that can return the models * based on the search/filter conditions. */ public function search() { // @todo Please modify the following code to remove attributes that should not be searched. $criteria=new CDbCriteria; $criteria->compare('con_id',$this->con_id,true); $criteria->compare('con_name',$this->con_name,true); $criteria->compare('con_address',$this->con_address,true); $criteria->compare('con_mobile',$this->con_mobile,true); $criteria->compare('members_id',$this->members_id,true); $criteria->compare('province',$this->province,true); $criteria->compare('city',$this->city,true); $criteria->compare('district',$this->district,true); $criteria->compare('is_default',$this->is_default); return new CActiveDataProvider($this, array( 'criteria'=>$criteria, )); } /** * Returns the static model of the specified AR class. * Please note that you should have this exact method in all your CActiveRecord descendants! * @param string $className active record class name. * @return TblConsignee the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } //增加的代码,三级省市联动 public function getProvinceList() { $model = City::model()->findAllByAttributes(array('pid'=>0)); return CHtml::listData($model, 'id', 'name'); } public function getCityList($pid,$typeid=0) { $model = City::model()->findAllByAttributes(array('pid'=>$pid)); return CHtml::listData($model, 'id', 'name',$typeid); } public function getDistrictList($pid) { $model = City::model()->findAllByAttributes(array('pid'=>$pid)); return CHtml::listData($model, 'id', 'name'); } public function getCityName($id) { $model = City::model()->findByPk($id); return $model->name; } }
视图:
<?php /* @var $this TblConsigneeController */ /* @var $model TblConsignee */ /* @var $form CActiveForm */ ?> <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'tbl-consignee-form', // Please note: When you enable ajax validation, make sure the corresponding // controller action is handling ajax validation correctly. // There is a call to performAjaxValidation() commented in generated controller code. // See class documentation of CActiveForm for details on this. 'enableAjaxValidation'=>false, )); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'con_name'); ?> <?php echo $form->textField($model,'con_name',array('size'=>16,'maxlength'=>16)); ?> <?php echo $form->error($model,'con_name'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'con_address'); ?> <?php echo $form->textField($model,'con_address',array('size'=>60,'maxlength'=>64)); ?> <?php echo $form->error($model,'con_address'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'con_mobile'); ?> <?php echo $form->textField($model,'con_mobile',array('size'=>16,'maxlength'=>16)); ?> <?php echo $form->error($model,'con_mobile'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'members_id'); ?> <?php echo $form->textField($model,'members_id',array('size'=>8,'maxlength'=>8)); ?> <?php echo $form->error($model,'members_id'); ?> </div> <!--增加的代码begin--> <div class="row"> <?php echo $form->labelEx($model,'province'); ?> <?php echo $form->dropDownList($model,'province',$model->provinceList,array( "onchange"=>"aa()", 'empty'=>'-请选择省-', 'ajax'=>array( //将下面的member改为TblConsignee //'url'=>Yii::app()->createUrl('member/dynamicCity'), //'update'=>'#Member_city', 'url'=>Yii::app()->createUrl('TblConsignee/dynamicCity'), 'update'=>'#TblConsignee_city', 'data'=>array('pid'=>'js:this.value','typeid'=>1), ), )); ?> <?php echo $form->error($model,'province'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'city'); ?> <?php echo $form->dropDownList($model,'city',$model->getCityList($model->province,1),array( 'onchange'=>'bb(this.value)', 'empty'=>'-请选择市-', 'ajax'=>array( //将下面的member改为TblConsignee 'url'=>Yii::app()->createUrl('TblConsignee/dynamicCity'), 'update'=>'#TblConsignee_district', 'data'=>array('pid'=>'js:this.value','typeid'=>2), ), )); ?> <?php echo $form->error($model,'city'); ?> </div> <div class="row" id="ddd" style="<?php if(!$model->district){?>display:none<?php }?>"> <?php echo $form->labelEx($model,'district'); ?> <?php echo $form->dropDownList($model,'district',$model->getCityList($model->city,2),array('empty'=>'-请选择区-')); ?> <?php echo $form->error($model,'district'); ?> </div><!--增加的代码end--> <div class="row"> <?php echo $form->labelEx($model,'is_default'); ?> <?php echo $form->textField($model,'is_default'); ?> <?php echo $form->error($model,'is_default'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> </div> <script> //增加的代码,三级省市联动 function aa() { $("#ddd").hide() } function bb(obj){ $("#ddd").show() //if(obj=="") //$("#ddd").html("12233"); } </script> <?php $this->endWidget(); ?> </div><!-- form -->