Widgets¶
http://docs.djangoproject.com/en/dev/ref/forms/widgets/#module-django.forms.widgets

widgets
widgets有挂件、微件的意思
简单的说就是一个微型的控制器
创建目录
1、在项目目录下创建 widgets
目录
2、下widgets目录下创建 views
目录

创建widgets
userWidget
class userWidget extends CWidget{
public $num = 20;
public function init() {
}
public function run() {
$users = $this->getUsers();
$this->render('userwidget',array(
'users'=>$users
));
}
protected function getUsers()
{
return Yii::app()->db->createCommand()->select('id,username,ctime')->from('user')->limit($this->num)->queryAll();
}
}
userWidget2
class userWidget2 extends CWidget{
public function init() {
echo CHtml::beginForm("", "POST");
}
public function input($name,$value = "",$label = "")
{
$label = $label===""?$name:$label;
echo CHtml::label($label.':', $name);
echo CHtml::textField($name,$value);
}
public function run() {
echo CHtml::endForm();
}
}
创建widgets视图
userwidget.php
<table>
<tr><th>id</th><th>username</th><th>date</th></tr>
<?php foreach($users as $v):?>
<tr><td><?=$v['id'];?></td><td><?=$v['username'];?></td><td><?=$v['ctime'];?></td></tr>
<?php endforeach;?>
</table>
使用
需要注意的是:
1、widget渲染的视图,$this指向当前widget,要想使用当前controller就要 Yii::App()->controller
2、传递的参数 可以初始化对应的 对应的公开属性
3、widgets的第二种执行方式,beginWidget对应init(),endWidget对应run()
<h1>调用当前controller测试</h1>
<?php echo Yii::app()->controller->createUrl("test");?>
<h1>第一种使用方式</h1>
<?php $this->widget('application.widgets.userWidget',array('num'=>6));?>
<h2>第二种使用方式</h2>
<?php $form = $this->beginWidget('application.widgets.userWidget2');?>
<?php $form->input('username');?>
<?php $this->endWidget();?>
转载于:https://www.cnblogs.com/kidstudy/archive/2009/11/06/1597220.html
先看官方文档对Widgets的解释
Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.也就是说Widgets就像一个大的窗口,可以在这个窗口中添加不同的小东西,我们在Qt中创建的GUI就叫Qt Widgets Application,其中又有三个基类对应着不同的窗口,这三个基类是QWidget,QMainWindow,QDialog。
那么这三个基类创建的窗口有什么不同呢?
其实也没什么不同。主要是MainWindow有自己的布局,可以添加菜单栏,工具条,状态栏什么的。
而对话框一般作为一个辅助窗口,让用户选择然后将选择传递到主窗口,例如什么文件对话框,颜色对话框,选字体的对话框什么的。
然后就是Widget,widget就是可以在窗口上添加不同的小部件,还可以使用不同的布局,小部件自己作为窗口又可以添加小部件.