Yii Framework 开发教程(36) Zii组件-DatePicker示例

jerry Yii 2015年11月24日 收藏

CJuiDatePicker 用于日期输入,它封装了 JUI datepicker插件,其基本用法如下:

  1. <?php echo $form->errorSummary($model); ?>
  2.  
  3.  <?php
  4.  $this->widget('zii.widgets.jui.CJuiDatePicker', array(
  5.   'name'=>'my_date',
  6.   'language'=>'en',
  7.   'options'=>array(
  8.             // 'show' (the default), 'slideDown', 'fadeIn', 'fold'
  9.   'showAnim'=>'fold',
  10.   'showOn'=>'button', // 'focus', 'button', 'both'
  11.   'buttonText'=>'Select form calendar',
  12.   'buttonImage'=>'images/calendar.png',
  13.   'buttonImageOnly'=>true,
  14.   ),
  15.   'htmlOptions'=>array(
  16.   'style'=>'width:80px;vertical-align:top'
  17.   ),
  18.   ));
  19.  
  20.  ?>
  21. <div class="row submit">
  22.     <?php echo CHtml::submitButton('Submit'); ?>
  23. </div>
  24.  
  25. <?php $this->endWidget(); ?>
  26. </div><!-- form -->

为了获取输入的日期,首先为CJuiDatePicker的Name属性赋值,为my_date,然后定义DataModel

  1. class DataModel extends CFormModel
  2. {
  3. public $my_date;
  4. }

当用户提交时,显示用户输入的日期,修改SiteController的actionIndex

  1. public function actionIndex()
  2. {
  3.  
  4. $model=new DataModel();
  5.  
  6. if(!empty($_POST['my_date']))
  7. {
  8. $model->my_date=$_POST['my_date'];
  9.  
  10. if($model->validate()) {
  11. $this->render('result', array(
  12. 'model' => $model,
  13.  
  14. ));
  15.    return;
  16. }
  17.  
  18. }
  19.  
  20. $this->render('index', array(
  21. 'model' => $model,
  22.  
  23. ));
  24. }

201212129005.png.jpg

下载地址