Yii Framework 开发教程(41) Zii组件-Tabs示例

jerry Yii 2015年11月24日 收藏

CJuiTabs 显示分页UI组件,和Yii Framework 开发教程(17) UI 组件 TabView示例功能类似,它封装了 JUI tabs插件。

前基本用法如下:

  1. <?php $this->widget('zii.widgets.jui.CJuiTabs', array(
  2. 'tabs'=>array(
  3. 'Static tab'=>'Static content',
  4. 'Render tab'=>$this->renderPartial('pages/_content1',null,true),
  5. 'Ajax tab'=>array('ajax'=>array('ajaxContent','view'=>'_content2')),
  6. ),
  7. 'options'=>array(
  8. 'collapsible'=>true,
  9. 'selected'=>1,
  10. ),
  11. 'htmlOptions'=>array(
  12. 'style'=>'width:500px;'
  13. ),
  14. )); ?>

显示了三个页面不同内容显示方法,Static Tab显示一个静态内容,Render Tab使用Partial渲染一个页面,而Ajax Tab则通过AJAX显示一个页面,注意后面两个需要在SiteController中定义Actions 如下:

  1. public function actions()
  2. {
  3. return array(
  4. 'page'=>array(
  5. 'class'=>'CViewAction',
  6. ),
  7. // ajaxContent action renders
  8. //"static" pages stored under 'protected/views/site/pages'
  9. // They can be accessed via:
  10. //index.php?r=site/ajaxContent&view=FileName
  11. 'ajaxContent'=>array(
  12. 'class'=>'application.controllers.AjaxViewAction',
  13. ),
  14. );
  15. }

其中AjaxViewAction为一自定义ViewAction,为CViewAction的子类,可以显示静态页面,其定义如下:

  1. class AjaxViewAction extends CViewAction
  2. {
  3. private $_viewPath;
  4.  
  5. public function run()
  6. {
  7. if(Yii::app()->request->isAjaxRequest)
  8. {
  9. $this->resolveView($this->getRequestedView());
  10. $controller=$this->getController();
  11. $controller->renderPartial($this->view, null, false, true);
  12. }
  13. else
  14. throw new CHttpException(400,'Invalid request.
  15.        Please do not repeat this request again.');
  16. }
  17. }

显示结果如下:

201212129010.png.jpg

下载地址