最新更新的yii三级省市联动
控制器
<?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/shop';
/**
* @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' => '收贷人ID',
'con_name' => '收货人',
'con_address' => '地址',
'con_mobile' => '手机',
'members_id' => '会员ID',
'province' => '省份',
'city' => '城市',
'district' => '县区',
'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;
/**
*按照特定的条件显示数据表的内容
*/
//创建访问控制对象,该类在components组件文件夹下accessCtrl,该类是自定义的by ping
$accessCtrl=new accessCtrl();
//把上面得到的criteria对象传递到shopmanager方法,得到返回结果
if($temp=$accessCtrl->accessForConsignee($criteria)){
$criteria=$temp;
}
//END:按照特定的条件显示数据表的内容
$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="center" style="padding: 10px 0 0 0">
<div class="m_add">
<div class="b">
<?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,
)); ?>
<?php echo $form->errorSummary($model); ?>
<p>
<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>
</p><p>
<div class="row">
<?php echo $form->labelEx($model,'con_address'); ?>
<?php echo $form->textarea($model,'con_address',array('rows'=>3,'cols'=>32)); ?>
<?php echo $form->error($model,'con_address'); ?>
</div>
</p><p>
<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>
</p><p>
<div class="row">
<?php echo $form->labelEx($model,'members_id'); ?>
<?php
session_start();
//echo $_SESSION['members_id'];
echo $form->textField($model,'members_id',array('value'=>$_SESSION['members_id'],'readonly'=>'true')); ?>
<?php echo $form->error($model,'members_id'); ?>
</div>
</p><p>
<!--增加的代码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>
</p><p>
<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,如果是在admin这个modules下,则为admin/TblConsignee/dynamicCity
//这里一定要注意区分大小写
'url'=>Yii::app()->createUrl('TblConsignee/dynamicCity'),
'update'=>'#TblConsignee_district',
'data'=>array('pid'=>'js:this.value','typeid'=>2),
),
)); ?>
<?php echo $form->error($model,'city'); ?>
</div>
</p><p>
<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-->
</p><p>
<div class="row">
<?php echo $form->labelEx($model,'is_default'); ?>
<?php echo $form->checkBox($model,'is_default'); ?>
<?php echo $form->error($model,'is_default'); ?>
</div>
</p><p>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? '增加' : '保存'); ?>
</div>
</p><p>
<script>
//增加的代码,三级省市联动
function aa()
{
$("#ddd").hide()
}
function bb(obj){
$("#ddd").show()
//if(obj=="")
//$("#ddd").html("12233");
}
</script>
<?php $this->endWidget(); ?>
</div><!--b end-->
</div><!--m_add end-->
</div><!--center end-->
1008

被折叠的 条评论
为什么被折叠?



