zoukankan      html  css  js  c++  java
  • Yii widget使用

    关于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 

    Php代码  收藏代码
    1. <?php $this->widget('WidgetName'); ?>  


    或者 

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

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

    2. 创建Widget 
    自定义Widget类要继承CWidget,覆盖方法run 
    Php代码  收藏代码
    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实现 
    Php代码  收藏代码
    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. }  

    存储到protectedcomponentsBannerMagicWidget.php 

    对应的view文件可能的内容如下: 
    Php代码  收藏代码
    1. <img src="images/adverts/<?php echo $advert; ?>" alt="whatever" />  

    存储到protectedcomponentsviewsannermagic.php 

    3. 调用该Widget 
    Php代码  收藏代码
    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'=>'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'=>'500px;'
    ),
    ));
     
    ?>
     
     
     
    通过定义panels 属性定义Accordion的几个可折叠的页面,通过配置 options传送参数给 JUI Accordion插件。
     
     
  • 相关阅读:
    都是CSS惹的祸
    Ruby简介
    网络攻击利用DedeCms漏洞
    ASP.NET验证技术详解
    一个低级错误引发的血案
    FCKeditor配置和精简【附源码】
    邮件发送详解
    Timer定时器的设计实例详解
    常用的加密算法MD5、SHA1
    JS日历控件集合附效果图、源代码
  • 原文地址:https://www.cnblogs.com/youxin/p/3646331.html
Copyright © 2011-2022 走看看