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. HDU 3161 Iterated Difference 暴力

    Iterated Difference Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. jProfiler远程连接Linux监控jvm1运行状态

    第一步:下载软件官网地址:https://www.ej-technologies.com/download/jprofiler/files,下载一个linux服务端,一个windows客户端 GUI界 ...

  3. 嵌入式设备hacking(转)

    原帖地址:http://drops.wooyun.org/papers/5157 0x00 IPCAM hacking TOOLS github-binwalk firmware-mod-kit ID ...

  4. Windows Azure 系列-- Azure Queue的操作

    - Storage Account. 和之前介绍的Azure Table和AzureBlob一样.你须要一个StorageAccount,仅仅须要创建1次AzureStorageAccount就好了, ...

  5. JEECG第二期深入使用培训(报名截止2014-06-21)

    JEECG第二期深入使用培训(报名截止2014-06-21) JEECG深度研究-交流碰撞火花,你学会的不不过JEECG,很多其它的是软件架构思想 http://www.jeecg.org/forum ...

  6. PHP个人博客系统开发历程

    声明: Author:GenialX GenialX's QQ:2252065614 GenialX's URL:胡旭博客 - www.ihuxu.com 一年多曾经的某一天,我在上交实验报告时,偶然 ...

  7. Python中函数的参数传递与可变长参数

    转自旭东的博客原文 Python中函数的参数传递与可变长参数 Python中传递参数有以下几种类型: (1)像C++一样的默认缺省函数 (2)根据参数名传参数 (3)可变长度参数 示例如下: (1)默 ...

  8. Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。

    <configSections> <!-- For more information on Entity Framework configuration, visit http:// ...

  9. git 查看commit提交的内容

    在使用git的过程中,我们经常需要查看某次commit修改了哪些内容,与之相关的命令就是: git log git show 首先,需要通过git log打印所有commit hashID,之后的gi ...

  10. apache基金会项目及甲骨文项目汇总

    Apache软件基金会 顶级项目 ▪ ActiveMQ ▪ Ant ▪ Apache HTTP Server ▪ APR ▪ Beehive ▪ Camel ▪ Cassandra ▪ Cayenne ...