关于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. Dapper Use For Net

    Dapper.Net by example januari 6, 2012 When the team behind StackOverflow released the mini-ORM Dappe ...

  2. xslt语法之---If Else

    大家都知道,XSL中是没有if else的,那么要想实现if else该怎么办呢? 其实很简单 <xsl:choose> <xsl:when test="position( ...

  3. Struts2 ValueStack

    一.作用 可以作为一个数据中转站,用在前台和后台数据传递 二.生命周期 ValueStack的生命周期是随着request的创建而创建,随request的销毁而销毁. 三.结构 OgnlValueSt ...

  4. oracle重建、更新索引、索引统计信息命令

    在oracle中查找所有的表的索引的命令 select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_na ...

  5. jsp中Java代码中怎么获取jsp页面元素

    举例,页面元素<td><input   value="${sl }" type="text" id="sl" name=& ...

  6. listActivity和ExpandableListActivity的简单用法

    http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...

  7. oracle备份表

    oracle与sql单表备份的区别 (     oracle中备份表: create table 备份表名 as select * from 原表 sql server中备份表: select * i ...

  8. oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断

    oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断 查询方式一: SELECT CASE WHEN (SELECT CAST(SUM(CASE ) THEN ...

  9. 关于c:\fakepath\的解决办法

    (2014.11.25 最后更新) 一.碎碎念:关于访问本地图片的路径的问题,比较典型的例子就是上传头像.在以往的解决办法中,我们大多是先将图片上传到服务器然后从服务器返回图片,显示在页面上以达到预览 ...

  10. 百度地图API地址转换成经纬度

    public class LngAndLatUtil { public static Map<String,Double> getLngAndLat(String address){ Ma ...