static function campus_areaOptions() { $criteria = new CDbCriteria(); $criteria->select = 'area_id, area_name'; $model = CampusArea::model()->findAll(); $campus_areas = array('' => '请选择相应园区'); foreach ($model as $row) { $campus_areas[$row->area_id] = $row->area_name; } return $campus_areas; }
echo $form->labelEx($model, 'email'); ?> <?php echo $form->emailField($model, 'email', array('size' => 60, 'maxlength' => 100)); ?> <?php echo $form->error($model, 'email'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'area_id'); ?> <?php echo $form->dropDownList($model, 'area_id', CampusArea::campus_areaOptions(), array('name' => 'campus_area')); ?> <?php echo $form->error($model, 'area_id'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'building_id'); ?> <?php echo $form->dropDownList($model, 'building_id', array(), array('class' => 'width140', 'id' => 'building', 'name' => 'building')); ?> <?php echo $form->error($model, 'building_id'); ?>
/** * 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 CampusArea the loaded model * @throws CHttpException */ public function loadModel($id) { $model = CampusArea::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function actionGetBuildings() { if (!isset($_POST['area_id'])) { return; } $area_id = $_POST['area_id']; $buildings = CampusArea::model()->findByPk($area_id)->buildings; $buildings = CHtml::listData($buildings, 'building_id', 'building_id'); $htmlData = CHtml::tag('option', array('selected' => 'selected', 'value' => ''), '请选择楼栋'); foreach ($buildings as $value => $key) { $htmlData .= Chtml::tag('option', array('value' => $value), Chtml::encode($key . '号')); } echo json_encode(array('htmlData' => $htmlData)); }
<?php $form = $this->beginWidget('CActiveForm', array('id' => 'building-form', 'enableAjaxValidation' => false)); ?> <p class="note">带有 <span class="required">*</span> 符号的都是必填项</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model, 'area_id'); ?> <?php echo $form->dropDownList($model, 'area_id', CampusArea::campus_areaOptions()); ?> <?php echo $form->error($model, 'area_id'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'building_id'); ?> <?php echo $form->textField($model, 'building_id', array('size' => 10, 'maxlength' => 10)); ?> <?php echo $form->error($model, 'building_id');