关于widgets,他们在yii中的关系如下 
system.web.widgets 系统自带最基本的widget 
zii.widgets 是基本扩展 
zii.widgets.grid 是基本扩展的重要分支 
zii.widgets.jui 是插件扩展

CWidget:http://www.yiiframework.com/doc/api/1.1/CWidget/

CWidget is the base class for widgets.

A widget is a self-contained component that may generate presentation based on model data. It can be viewed as a micro-controller that embeds into the controller-managed views.

Compared with controller, a widget has neither actions nor filters.

Usage is described at CBaseController and CBaseController::widget.

下面以一个随机广告图片为例说明Yii中Widget的用法 
1. 调用Widget

  1. <?php $this->widget('WidgetName'); ?>

或者

    1. <?php $widget=$this->beginWidget('path.to.WidgetClass'); ?>
    2. ...可能会由小物件获取的内容主体...
    3. <?php $this->endWidget(); ?>
也可以传参到Widget类

  1. <?php $userId = 1; ?>
  2. <?php $this->widget('WidgetName',array('userId'=>$userId)); ?>

参数userId自动映射到Widget类的同名属性,所以在定义Widget时,别忘记了声明该属性。

2. 创建Widget 
自定义Widget类要继承CWidget,覆盖方法run

  1. <?php
  2. class BannerMagic extends CWidget {
  3. public function run(){
  4. }
  5. }

或者:

  1. class MyWidget extends CWidget {
  2. public function init() {
  3. // 此方法会被 CController::beginWidget() 调用
  4. }
  5. public function run() {
  6. // 此方法会被 CController::endWidget() 调用
  7. }
  8. }

下面是是BannerMagicWidget实现

  1. <?php class BannerMagicWidget extends CWidget {
  2. public function run() {
  3. $random = rand(1,3);
  4. if ($random == 1) {
  5. $advert = "advert1.jpg";
  6. }  else if ($random == 2) {
  7. $advert = "advert2.jpg";
  8. }  else {
  9. $advert = "advert3.jpg";
  10. }
  11. $this->render('bannermagic',array(
  12. "advert"=>$advert,
  13. ));
  14. }
  15. }

存储到protected\components\BannerMagicWidget.php

对应的view文件可能的内容如下:

  1. <img src="data:images/adverts/<?php echo $advert; ?>" alt="whatever" />

存储到protected\components\views\bannermagic.php

3. 调用该Widget

  1. <?php $this->widget('BannerMagicWidget'); ?>

转自:http://koda.iteye.com/blog/1134606

yii dropdownlisthttp://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail

public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))

 
 
Zii组件中包含了一些基于JQuery的UI组件,这些UI组件定义在包zii.widgets.jui中,包括CJuiAccordion ,CJuiAutoComplete,CJuiDatePicker等。本篇介绍CJuiAccordion,显示一个Accordion组件(类似手风琴可以折叠的UI组件)。这个控件封装了 JUI Accordion插件。
基本用法如下:
 
 
[php] 
<?php  
$this->widget('zii.widgets.jui.CJuiAccordion', array(  
    'panels'=>array(  
                'panel 1'=>'Content for panel 1',  
                'panel 2'=>'Content for panel 2',  
                'panel 3'=>$this->renderPartial('_content1',null,true),  
                ),  
            'options'=>array(  
                'collapsible'=>true,  
                'active'=>1,  
                ),  
            'htmlOptions'=>array(  
                'style'=>'width:500px;'  
                ),  
            ));  
  
?>  
 
<?php
$this->widget('zii.widgets.jui.CJuiAccordion', array(
'panels'=>array(
'panel 1'=>'Content for panel 1',
'panel 2'=>'Content for panel 2',
'panel 3'=>$this->renderPartial('_content1',null,true),
),
'options'=>array(
'collapsible'=>true,
'active'=>1,
),
'htmlOptions'=>array(
'style'=>'width:500px;'
),
));
 
?>
 
 
 
通过定义panels 属性定义Accordion的几个可折叠的页面,通过配置 options传送参数给 JUI Accordion插件。
 
 

Yii widget使用的更多相关文章

  1. yii widget使用的3个用法

    yii视图中使用的widget方式总结:常用的有3种方式:一.显示详细信息: $this->widget('zii.widgets.CDetailView', array( 'data' =&g ...

  2. Yii使用笔记 2

    yii中的 getId等函数, id更多的是一个 string, 而不是数字. CCaptchaAction > CAction > CComponent. 实现是 IAction. yi ...

  3. yii2开发后记

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  4. [moka同学摘录]Yii2.0开发初学者必看

    想要了解更多YII,PHP方面内容,请关注本博客. 基础总结 1.修改默认控制器/方法 yii默认是site控制器,可以在web.php中设置$config中的'defaultRoute'='xxxx ...

  5. 转:Yii实战中8个必备常用的扩展,模块和widget

    转载自:http://www.yiiframework.com/wiki/180/yii8/ 在经过畅K网的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自 ...

  6. Yii实战中8个必备常用的扩展,模块和widget

    Yii实战中8个必备常用的扩展,模块和widget 在经过畅K网 的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自己留个备忘录,下面我以代码加图片说明. ...

  7. yii使用gii创建后台模块与widget使用

    yii使用gii创建后台模块与widget使用 1.在protected/config/main.php中打开gii的配置属性. 'gii'=>array( 'class'=>'syste ...

  8. Yii 通过widget小物件生成添加表单

    通过widget小物件创建添加商品的表单 视图里,表单以endWidget();?>结束 最终效果: 把表单提交过来的信息保存到数据库中去. 补充要点: 密码表单: <?php echo ...

  9. Yii中的CCheckBoxColumn在widget中的用法

    'columns'=>array(        array(            'class'=>'CCheckBoxColumn',            'id'=>'us ...

随机推荐

  1. jsp页面使用javascript添加页面元素示例代码

    <body>    <input type="button" value="test" onclick="javascript:a( ...

  2. 关于mvc 分页的 这两个结合着用

    http://www.cnblogs.com/JackFeng/archive/2010/01/25/JackFeng.html http://www.webdiyer.com/mvcpager/de ...

  3. mvc form

    当点击提交按钮后,想在Controll里取到Form里的数据. 必须在控件上设置name属性 例如<input type='text',name='userId'/>, 在controll ...

  4. 原生JS与jQuery文档加载完毕的写法

    HTML是有执行顺序的,默认是自上而下执行.所以当我们的js代码在html代码下边的时候,可以正常执行,而当我们的js代码在html代码上边的时候,可以就无法正常执行了,这时,我们需要在文档加载完毕的 ...

  5. 表达式:使用API创建表达式树(2)

    一.BlockExpression类:表式一个包含可在其中定义变量的表达式序列的块.是一组表达式,类似于多个委托的 += 后的效果,其返回表达式是最后一个表达式决定.以下是BlockExpressio ...

  6. C#-日期格式表

    自定义格式表: 格式模式      说明 d                   月中的某一天.一位数的日期没有前导零. dd                 月中的某一天.一位数的日期有一个前导零. ...

  7. recursive - simple screenshot but detail principle.

    the code below demonstates the principle of the'recursive-call' that the programing beginner may be ...

  8. retain two decimal digits.

    package kju.o; import static kju.print.Printer.*; import java.text.*; class MathDemo { public static ...

  9. wpf 大控件 打印 将控件转成 xps格式 并分页打印

    //PayRollPrintList:要打印的 list 可换成自己要打印的类型 private List<PayRoll> _PayRollPrintList = new List< ...

  10. 十二、C# 委托与Lambda表达式(匿名方法的另一种写法)

    委托与Lambda表达式   1.委托概述 2.匿名方法 3.语句Lambda 4.表达式Lambda 5.表达式树   一.委托概述 相当于C++当中的方法指针,在C#中使用delegate 委托来 ...