Yii框架zii.widgets.grid自定义按钮,ajax触发事件并提示
the configuration for additional buttons. Each array element specifies a single button which has the following format:
'buttonID' => array(
'label'=>'...', // text label of the button
'url'=>'...', // a PHP expression for generating the URL of the button
'imageUrl'=>'...', // image URL of the button. If not set or false, a text link is used
'options'=>array(...), // HTML options for the button tag
'click'=>'...', // a JS function to be invoked when the button is clicked
'visible'=>'...', // a PHP expression for determining whether the button is visible
)
In the PHP expression for the 'url' option and/or 'visible' option, the variable $row refers to the current row number (zero-based), and $data refers to the data model for the row.
Note that in order to display these additional buttons, the template property needs to be configured so that the corresponding button IDs appear as tokens in the template.
Yii框架zii.widgets.grid自定义按钮,ajax触发事件并提示
我们在用表格展示数据并管理的时候,可能会需要用到按钮来操作某一行数据,比如查看,修改,删除!
Yii内置了3种按钮:查看,修改和删除,你可以自定义样式、事件。详细配置见类参考:CButtonColumn.
如果需要自定义按钮绑定指定的事件该怎么办呢?
幸运的是Yii提供了自定义按钮的办法.看代码:
在视图文件里面:
$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内容:
$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;
srf不用管他,是安全验证,必须要有,否则会400报错.$click是js函数的字符窜,用了文档字符窜形式,注意结束的EOD前面必须没空格,也不能缩进。
这是Yii内置的yiiGridView Jquery插件,把请求提交到控制器的动作里面处理,然后返回结果并显示。最后还会更新一次gridvi
以下是本人测试代码:
Model中
public function isShow($id)
{
$id = (int)$id;
$record = $this->findByAttributes(array('id' => $id));
if ($record && $record->status == 1) {
return true;
}else{
return false;
}
}
Controller中
public function actionStatus($id, $status)
{
$id = (int)$id;
$status = (int)$status;
$record = User::model()->findAllByAttributes(array('id' => $id));
if ($record) {
User::model()->updateAll(array('status' => $status), 'id=:id', array(':id' => $id));
//$this->redirect('Yii::app()->request->urlReferrer');
if($status == 1){
echo '展示成功';
}elseif($status == 0){
echo '取消成功';
}else{
echo '操作失败';
}
}
}
View中
<?php
$csrfTokenName = Yii::app()->request->csrfTokenName;
$csrfToken = Yii::app()->request->csrfToken;
$csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
$Confirmation= "确定操作吗?";
$Confirmation_add= "确定展示吗?";
$Confirmation_del= "确定取消吗?";
$afterDelete = 'function(link,success,data){ if(success) alert(data); }';
$gridViewId = 'user-grid';
$click_test = <<<EOD
function() {
//if(!confirm("$Confirmation_add")) return false;
var th = this,
afterDelete = function(){};
jQuery('#$gridViewId').yiiGridView('update', {
type: 'POST',
url: jQuery(this).attr('href'),
success: function(data) {
jQuery('#$gridViewId').yiiGridView('update');
afterDelete(th, true, data);
//alert('操作成功');
},
error: function(XHR) {
return afterDelete(th, false, XHR);
}
});
return false;
}
EOD; $click = <<<EOD
function() {
if(!confirm("$Confirmation")) return false;
var th=this;
var afterDelete=$afterDelete;
$.fn.yiiGridView.update('$gridViewId', {
type:'POST',
url:$(this).attr('href'),
success:function(data) {
$.fn.yiiGridView.update('$gridViewId');
afterDelete(th,true,data);
},
error:function(XHR) {
return afterDelete(th,false,XHR);
}
});
return false;
}
EOD; ?> <?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
//'template'=>'{summary}{items}{pager}',
//'summaryText'=>'<font color=#0066A4>总计{count}条数据 共{pages}页 当前第{page}页</font>',
'pager'=>array(
//'class'=>'CLinkPager',
//'pageSize' => 5,
//'firstPageLabel' => '首页',
'prevPageLabel' => '上一页',
'nextPageLabel' => '下一页',
//'lastPageLabel' => '末页',
'header'=>'',
),
'columns'=>array(
'id',
'username',
//'password',
//'email',
//'profile',
'url',
'role',
'status',
// array(
// 'class'=>'CButtonColumn',
// ),
array(
'class'=>'CButtonColumn',
'template'=>'{view} {update} {delete}',
'viewButtonOptions'=>array('title'=>'查看'),
'updateButtonOptions'=>array('title'=>'修改'),
),
array(
'class'=>'CButtonColumn',
'header'=>'首页展示',
'template'=>'{add_status} {del_status}',
'buttons'=>array(
'add_status' => array(
'label'=>'展示', // text label of the button
'url'=>'Yii::app()->controller->createUrl("status",array("id"=>$data->primaryKey, "status"=>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'=>'User::model()->isShow($data->primaryKey)?false:true',
),
'del_status' => array(
'label'=>'取消', // text label of the button
'url'=>'Yii::app()->controller->createUrl("status",array("id"=>$data->primaryKey, "status"=>0))', // 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'=>'User::model()->isShow($data->primaryKey)?true:false',
)
),
),
),
)); ?>
备注:
其中$data->primaryKey表示主键值,如果主键名为id,也可以写成$data->id
相关类参考手册:http://www.yiichina.com/api/CActiveRecord
Yii框架zii.widgets.grid自定义按钮,ajax触发事件并提示的更多相关文章
- Yii框架里用grid.CGridView调用pager扩展不显示最后一页按钮的解决
有如下一例,调用zii.widgets.grid.CGridView显示Blog信息,代码如下: $this->widget('zii.widgets.grid.CGridView', arra ...
- yii中 columnszii.widgets.grid.CGridView
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'chapter-grid', 'dataProvide ...
- ASPxGridView中Command列自定义按钮点击事件概要
其中CustomButtonClick="ButtonClick",e.buttonID可以获取到自定义按钮的id e.visibleIndex获取到行的索引 grdList.Ge ...
- Yii zii.widgets.grid 隐藏列 方便js获取隐藏值
array( 'name' => $data->is_audit, 'value' => '$data->is_audit', 'headerHtmlOptions' => ...
- 使用Yii框架自带的CActiveForm实现ajax提交表单
Php代码: <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array ...
- Grid表格的js触发事件
没怎么接触过Grid插件: 解决的问题是:点击Grid表行里的内容触发js方法弹出模态框,用以显示选中内容的详细信息. 思路:给准备要触发的列加上一个css属性,通过这个css属性来获取元素并触发js ...
- Yii框架tips(转)
yii的一些小的技巧 http://www.yiichina.com/topic/151 db组件 'schemaCachingDuration'=>3600, 为什么不起做用?需要开缓存 如何 ...
- Yii框架tips
db组件 'schemaCachingDuration'=>3600, 为什么不起做用?需要开缓存 如何在页面下边显示sql的查询时间在log组件的routes中加入 array('class' ...
- Yii框架常见问题汇总
然用过Yii做了一个小项目了,但是过程中间解决的问题没有随手记下来,导致新项目开始后,以前碰到的问题还得在查一遍,干脆就记下来,以便不时之需. 有新的会随时更新. 1.如何显示ActiveRecord ...
随机推荐
- php5.3 不支持 session_register() 此函数已启用的解决方法
php从5.2.x升级到5.3.2.出来问题了.有些原来能用的程序报错了,Deprecated: Function session_register() is deprecated php从5.2.x ...
- Entity Framework Code First 数据迁移
需要在[工具 --> NuGet 程序包管理器 --> 程序包管理器控制台]中输入三个命令: Enable-Migrations (初次迁移时使用) Add-Migration [为本次迁 ...
- asp.net管道模型
查了很多资料,终于大概弄懂管道模型(注意并非指定是asp.net范畴)是个什么概念了,其实就是从Unix移植过来的一种概念,也可以说是一种模式吧(只允许一头读,一头写,并且读完了就会自动消失). as ...
- 预处理命令#define #undef #if #endif 的基本用法
C#的预处理命令其实还是蛮有用的,但是真正使用过得人不多,这个介绍一下平时用的比较多的预处理命令中的几个:#define,#undef ,#if,#endif.除此之外还有一些预处理命令#warnin ...
- 关于Java(Hello World程序)
详解 Hello World 应用程序 源码 class HelloWorldApp { public static void main(String[] args) { System.out.pri ...
- Ubuntu/Linux下7款轻量级编辑器 (转)
From http://www.feiyan.info/39.html 在Windows卧铺使用Zend Studio或者EditPlus写PHP,Zend Studio适合大项目,EditPlus配 ...
- SCOI2015题解 && 考试小结
Day1: 第一题:裸地二分+网络流:二分答案,连接将每行每列拆成点,对于满足答案的格子行列连边,看是否流量是否大于t即可,可惜第k大看成了第k小,然后100分就没了. 第二题:倍增,考虑贪心算法,就 ...
- bzoj 2402: 陶陶的难题II 二分答案维护凸包
2402: 陶陶的难题II Time Limit: 40 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 68 Solved: 45[Submi ...
- [OI笔记] 最长上升子序列与网络流建模
与最长上升子序列相关的网络流问题: 给定一个序列 A[1..n] ,求出 A 的最长上升子序列长度.并且回答下列询问: (1) 如果每个点只能用一次,能从 A 中取出几个最长上升子序列? (2) 如果 ...
- 关于路由、AP、交换机的小总结
整理自关于路由.AP.交换机的小总结 要将各种设备连成网络,一般运用网络中的两层,即第二层的数据链路层和第三层的网络层.而设备之间需要通信就需要各自的网络地址. 第二层设备有物理地址即MAC地址,这对 ...