dropDownList:

Yii中可以采用CHtml类来实现,也可以用CActiveForm来实现。
一、用CHtml来实现。
VIEW中实现:

<?php
echo CHtml::dropDownList('country_id','', array(1=>'USA',7=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>Yii::app()->createUrl('project/dynamiccities'),
'update'=>'#city_id',
'data'=>array(Yii::app()->request->csrfTokenName=>Yii::app()->request->getCsrfToken(),'country_id'=>'js $("#country_id").val()')
))); echo CHtml::dropDownList('city_id','', array()); ?>

controller中实现:

public function actionDynamiccities()
{
  $data=Parts::model()->findAll('prent_id=:prent_id',array(':prent_id'=>(int) $_POST['country_id']));   $data=CHtml::listData($data,'id','name');
  foreach($data as $value=>$name)
  {
    echo CHtml::tag('option',
    array('value'=>$value),CHtml::encode($name),true);
  }
}

二、用CActiveForm来实现
在VIEW中实现:

<?php echo $form->dropDownList($model,'province_id',$provinceList(这个值可以通过render传递到页面),array(
  'empty'=>'-请选择-',
  'ajax'=>array(
  //指定请求地址
  'url'=>Yii::app()->createUrl('site/dynamicCity'),
  //请求数据
  'data'=>array('pid'=>'js:this.value'),
  //操作元素
  'update'=>'#SosInfo_city_id',(注意这个update的值很容易弄错,它由两部分组成:模型+ID,模型->是指本CActiveForm所承载的model名称)
  ),
)); ?>
<?php
echo $form->dropDownList($model,'city_id',City::model()->getCityList($model->province_id),array('empty'=>'-请选择-'));
?>

如果是静态的:
$form->dropDownList($model, 'field_name', array(1=>'test1', 2=>'test2'))
如果是动态的:
$form->dropDownList( $model, 'name', CHtml::listData( modelname::model()->findAll(), 'id', 'name') );

$form是你view中生成的form, 根据你生成form名字不同,修改它,$model是你对应的模型的名字,也是你自己定, name是和你model数据表中字段的名字一致就行,modelname是模型对应的类名,比如说你的是User,那这里就是 UserRole::model()->findAll()。id和name是你要去列表的那个表中字段,第一个是将作为option的 value,第二个显示名字。

典型应用场景:
创建user时,需要选择user的角色,有个表专门定义user的角色,表名是UserRole,User这个表中有一个外键role,它的值是UserRole的主键,UserRole的主键是id,另外一个字段是name,我们将显示给用户。
那么上面的哪一行代码变成:

$form->dropDownList( $model, 'name', CHtml::listData( UserRole::model()->findAll(), 'id', 'name') );

yii jquery折叠、弹对话框、拖拽、滑动条、ol和ul列表、局部内容切换

 <?php Yii::app()->clientScript->registerCoreScript('jquery');?>

<?php
//yii折叠效果(CJuiAccordion)
$this->widget('zii.widgets.jui.CJuiAccordion', array(
'panels'=>array(
'分类1'=>'分类1的内容',
'分类2'=>'分类2的内容',
// 分类可以渲染一个页面,例如分类3
//'分类3'=>$this->renderPartial('_partial',null,true),
),
'options'=>array(
'animated'=>'bounceslide',
),
));
?> <?php
//按钮加js弹框提示
$this->widget('zii.widgets.jui.CJuiButton',
array(
'name'=>'button',
'caption'=>'提交',
'value'=>'asd',
'onclick'=>'js:function(){alert("提交成功!"); this.blur(); return false;}',
)
);
?> <?php
//谈对话框
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog',
// additional javascript options for the dialog plugin
'options'=>array(
'title'=>'对话框',
'autoOpen'=>false,
),
));
//输出弹出框的内容
//echo $this->renderPartial('_form',null,true); $this->endWidget('zii.widget.jui.CJuiDialog'); //打开对话框的链接
echo CHtml::link('登录', '#', array(
'onclick'=>'$("#mydialog").dialog("open"); return false;',
));
?> <?php
//拖拽
$this->beginWidget('zii.widgets.jui.CJuiDraggable', array(
// additional javascript options for the draggable plugin
'options'=>array(
'scope'=>'myScope',
),
));
echo '拖拽的内容!';
$this->endWidget();
?> <?php
//ol列表
$this->widget('zii.widgets.jui.CJuiSelectable', array(
'items'=>array(
'id1'=>'Item 1',
'id2'=>'Item 2',
'id3'=>'Item 3',
),
// additional javascript options for the selectable plugin
'options'=>array(
'delay'=>'300',
),
));
//ul列表
$this->widget('zii.widgets.jui.CJuiSortable', array(
'items'=>array(
'id1'=>'Item 1',
'id2'=>'Item 2',
'id3'=>'Item 3',
),
// additional javascript options for the accordion plugin
'options'=>array(
'delay'=>'300',
),
));
?> <?php
//滑动条
$this->widget('zii.widgets.jui.CJuiSlider', array(
'value'=>37,
// additional javascript options for the slider plugin
'options'=>array(
'min'=>10,
'max'=>50,
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?> <?php
//局部内容切换
$this->widget('zii.widgets.jui.CJuiTabs', array(
'tabs'=>array(
'分类1'=>'分类1',
'分类2'=>array('content'=>'分类2', 'id'=>'tab2'),
//'分类3'=>$this->render('_form',null,true),//渲染一个页面
// panel 3 contains the content rendered by a partial view
//'AjaxTab'=>array('ajax'=>$ajaxUrl),
),
// additional javascript options for the tabs plugin
'options'=>array(
'collapsible'=>true,
),
));
?>

 zii.widgets.grid自定义按钮,ajax触发事件并提示

我们在用表格展示数据并管理的时候,可能会需要用到按钮来操作某一行数据,比如查看,修改,删除!
Yii内置了3种按钮:查看,修改和删除,你可以自定义样式、事件。详细配置见类参考:CButtonColumn.
如果需要自定义按钮绑定指定的事件该怎么办呢?
幸运的是Yii提供了自定义按钮的办法.看代码:
在视图文件里面:

<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'xx-xx-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'pager'=>array(
'class'=>'CLinkPager',
'nextPageLabel'=>'下一页',
'prevPageLabel'=>'上一页',
'header'=>'',
),
'summaryText'=>'<font color=#0066A4>显示{start}-{end}条.共{count}条记录,当前第{page}页</font>',
'columns'=>array(
array(
'name'=>'id',
'htmlOptions'=>array('width'=>'25'),
'sortable'=>false,
),
array(
'class'=>'CButtonColumn',
'template'=>'{view} {update}',
'viewButtonOptions'=>array('title'=>'查看'),
'updateButtonOptions'=>array('title'=>'修改'),
),
array(
'class'=>'CButtonColumn',
'header'=>'首页展示',
'template'=>'{add} {del}',
'buttons'=>array(
'add' => array(
'label'=>'展示', // text label of the button
'url'=>'Yii::app()->controller->createUrl("focus/create",array("id"=>$data->primaryKey,"type"=>1))', // a PHP expression for generating the URL of the button
'imageUrl'=>'http://s.maylou.com/common/images/ysh.jpg', // image URL of the button. If not set or false, a text link is used
'options'=>array('style'=>'cursor:pointer;'), // HTML options for the button tag
'click'=>$click, // a JS function to be invoked when the button is clicked
'visible'=>'SiteRecommend::isItemInTypeAndId(1, $data->id)?false:true',
),
'del' => array(
'label'=>'取消展示', // text label of the button
'url'=>'Yii::app()->controller->createUrl("focus/delete",array("id"=>$data->primaryKey,"type"=>1))', // a PHP expression for generating the URL of the button
'imageUrl'=>'http://s.maylou.com/common/images/yzhu.jpg', // image URL of the button. If not set or false, a text link is used
'options'=>array('style'=>'cursor:pointer;'), // HTML options for the button tag
'click'=>$click, // a JS function to be invoked when the button is clicked
'visible'=>'SiteRecommend::isItemInTypeAndId(1, $data->id)?true:false',
)
),
),
),
));
?>

buttons选项提供了创建按钮的方法,上面创建了2个按钮:add和del,并注册到template里面。其中最主要的是click选项,决定了你的触发条件。这里用ajax触发。在上面的代码前面加上$click内容:

<?php
$csrfTokenName = Yii::app()->request->csrfTokenName;
$csrfToken = Yii::app()->request->csrfToken;
$csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
$Confirmation= "你确定要这么做?";
$afterDelete = 'function(link,success,data){ if(success) alert(data); }';
$click=<<<EOD
function() {
if(!confirm("$Confirmation")) return false;;
var th=this;
var afterDelete=$afterDelete;
$.fn.yiiGridView.update('build-oneprice-grid', {
type:'POST',
url:$(this).attr('href'),$csrf
success:function(data) {
$.fn.yiiGridView.update('build-oneprice-grid');
afterDelete(th,true,data);
},
error:function(XHR) {
return afterDelete(th,false,XHR);
}
});
return false;
}
EOD;
?>

csrf不用管他,是安全验证,必须要有,否则会400报错.$click是js函数的字符窜,用了文档字符窜形式,注意结束的EOD前面必须没空格,也不能缩进。
这是Yii内置的yiiGridView Jquery插件,把请求提交到控制器的动作里面处理,然后返回结果并显示。

YII插件的更多相关文章

  1. composer 安装yii插件 fontawesome

    国外站点 http://fontawesome.io/ 国内站点 http://fontawesome.dashgame.com/ Installation The preferred way to ...

  2. Yii系列教程(三):集成Redis

    1安装Redis 切换至/usr/local/src下,下载并安装redis: $ wgethttp://redis.googlecode.com/files/redis-2.6.12.tar.gz ...

  3. yii2

    yii2框架官方说明文档 http://www.yiiframework.com/doc/guide/2.0/zh_cn/caching.page yii2创建您的第一个application应用 h ...

  4. 安装使用yii-debug-toolbar,yii框架的调试插件

    1github 下载yii-debug-toolbar 插件 地址:https://github.com/malyshev/yii-debug-toolbar 2. 假设yii的项目创建在了/expo ...

  5. Yii笔记:打印sql、Form表单、时间插件、Mysql的 FIND_IN_SET函数使用、是否是post/ajax请求

    语句部分: yii1版本打印最后一条执行的SQL: $this->getDbConnection()->createCommand()->select()->from()-&g ...

  6. yii 日期插件

    ——controller     public $defaultAction = "income";    public function actionIncome(){      ...

  7. yii第三方插件snoopy配置

    首先.把snoopy类放到protected\extensions\snoopy\目录下. 其次.在yii配置文件main.php里配置import扩展进来. 'import'=>array( ...

  8. Yii 添加Input时间插件

    1.首先引入时间组件的JS文件,组件可以在网上下载到没有的可以到网上去下载 <script language="javascript" type="text/jav ...

  9. yii框架中应用jquery表单验证插件

    效果图: 视图层: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

随机推荐

  1. weighttp 使用

    Weighttp 地址 http://redmine.lighttpd.net/projects/weighttp/wiki Weighttp的介绍:weighttp  is a lightweigh ...

  2. ssh 远程链接时出现错误提示:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

    ssh 远程链接时出现错误提示 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST I ...

  3. Git_git的诞生

    很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与 ...

  4. 什么叫NAT,设置NAT的两个方法

    NAT是网络地址翻译就是把公网IP翻译成私有地址, 又叫端口映射或端口转发. 采用路由方式是指ADSL拥有一个动态或固定的公网IP,ADSL直接接在HUB或交换机上,所有的电脑共享上网.这时ADSL的 ...

  5. 浅析OpenGL光照

    浅析OpenGL光照 之前从来都没有涉及光照的内容,心想只要能通过常规的方法渲染出几何体甚至是模型就可以了,然而没有光照的日子注定是苦涩的,因为仅凭几何体和模型的颜色无法达到真是渲染的效果,在实际中有 ...

  6. javascript中的2个感叹号的用法

    !!是逻辑"非非",即是在逻辑"非"的基础上再"非"一次.通过!或!!可以将很多类型转换成bool类型,再做其它判断.     应用场景:判 ...

  7. 一种基于ES5的JavaScript继承

    关于JavaScript继承,方式非常多,包含compile-to-javascript的语言TypeScript, CoffeeScript以及站点MDN, GitHub, Modernizr各种p ...

  8. SVN环境

    SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...

  9. 追MM和Java的23种设计模式

    我在Java论坛看到这篇文章,作者以轻松的语言比喻了java的32种模式,有很好的启发作用,但可惜没有给出具体的意思,我就在后边加上了.这些都是最简单的介绍,要学习的话建议你看一下阎宏博士的<J ...

  10. UVC调试

    USB video class(又称为USB video device class or UVC)就是USB device class视频产品在不需要安装任何的驱动程序下即插即用,包括摄像头.数字摄影 ...