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. 【10.29校内测试】【线段树】【DP】【二进制Trie树求最小值最大】

    Solution 标程太暴力惹QAQ 相当于是26棵线段树的说QAQ 不过我写了另一种写法,从大到小枚举每一个字母,标记字典序在这个字母之上的位置为1,每次都建一棵线段树,维护1的数量,即区间和. 修 ...

  2. 关于django Class-based views的理解

    django是mvt模式,其中v就是这个显示逻辑部分,简单来讲,view函数可以说是接收request,然后处理,返回response的主体函数. 对于一些简单的逻辑关系,可以用直接用函数模式来进行处 ...

  3. GIT(1)----更新代码和上传代码操作的步骤

    1.第一次下载代码 a.首先获得下载的地址,可从服务器,或者GitHut上获得.例如http://100.211.1.110:21/test/test.git b.终端里切换到想要将代码存放的目录,在 ...

  4. .net mvc控制器传递方法到视图

    很多人都是在视图里面定义方法,然后再使用.我个人也是这么干的.但是为了验证是否可以将方法从控制器传递到视图,所以做了个测试.结果真的可以.原理是利用了委托(delegate),因为委托本身就是一种类型 ...

  5. git服务器安装和配置

    http://www.cnblogs.com/dee0912/p/5815267.html

  6. 成为一名JAVA高级工程师你需要学什么【转】

    宏观上: 1.技术广度方面至少要精通多门开源技术吧,研究过struts\spring等的源码. 2.项目经验方面从头到尾跟过几个大项目,头是指需求阶段,包括需求调研.尾是指上线交付之后,包括维护阶段. ...

  7. 华为S5300系列升级固件S5300SI-V100R005C00SPC100.cc

    这个固件附带了web,注意,这个插件是升级V200的必经固件,所以必须升级为此固件之后才能往下升级. 升级小插曲: 1.升级的使用使用Windows,不要用Mac或者Linux,因为从Mac/Linu ...

  8. MVC借助MvcSiteMapProvider实现站点地图

    使用MvcSiteMapProvider可轻松实现站点地图,俗称"面包屑".如图: 通过NuGet,输入MvcSiteMapProvider搜索,并安装. 在Mvc.sitemap ...

  9. web开发常见bug汇总

    1.在做使用struts2进行文件上传时总是出现 java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOu ...

  10. Extjs DateField onchange

    1 开发思路: 在日期值变化的事件中获得选择后的日期值,传给后台,然后从后台加载相应的数据 2 问题:在查看extjs2.2 的api的官方说明文档,文档对datefield组件的change事件说明 ...