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

来源:互联网 发布:知乎百万英镑 编辑:程序博客网 时间:2024/06/05 15:17

我们在用表格展示数据并管理的时候,可能会需要用到按钮来操作某一行数据,比如查看,修改,删除!

Yii内置了3种按钮:查看,修改和删除,你可以自定义样式、事件。详细配置见类参考:CButtonColumn.

如果需要自定义按钮绑定指定的事件该怎么办呢?

幸运的是Yii提供了自定义按钮的办法.看代码:

在视图文件里面:

 

Php代码  收藏代码
  1. $this->widget('zii.widgets.grid.CGridView'array(  
  2.     'id'=>'xx-xx-grid',  
  3.     'dataProvider'=>$model->search(),  
  4.     'filter'=>$model,  
  5.     'pager'=>array(  
  6.             'class'=>'CLinkPager',  
  7.             'nextPageLabel'=>'下一页',  
  8.             'prevPageLabel'=>'上一页',  
  9.             'header'=>'',  
  10.     ),  
  11.     'summaryText'=>'<font color=#0066A4>显示{start}-{end}条.共{count}条记录,当前第{page}页</font>',  
  12.     'columns'=>array(  
  13.         array(  
  14.                 'name'=>'id',  
  15.                 'htmlOptions'=>array('width'=>'25'),  
  16.                 'sortable'=>false,  
  17.         ),  
  18.         array(  
  19.             'class'=>'CButtonColumn',  
  20.             'template'=>'{view} {update}',  
  21.             'viewButtonOptions'=>array('title'=>'查看'),  
  22.             'updateButtonOptions'=>array('title'=>'修改'),  
  23.         ),  
  24.         array(  
  25.             'class'=>'CButtonColumn',  
  26.             'header'=>'首页展示',  
  27.             'template'=>'{add} {del}',  
  28.             'buttons'=>array(  
  29.                     'add' => array(  
  30.                             'label'=>'展示',     // text label of the button  
  31.                             'url'=>'Yii::app()->controller->createUrl("focus/create",array("id"=>$data->primaryKey,"type"=>1))',       // a PHP expression for generating the URL of the button  
  32.                             'imageUrl'=>'http://s.maylou.com/common/images/ysh.jpg',  // image URL of the button. If not set or false, a text link is used  
  33.                             'options'=>array('style'=>'cursor:pointer;'), // HTML options for the button tag  
  34.                             'click'=>$click,     // a JS function to be invoked when the button is clicked  
  35.                             'visible'=>'SiteRecommend::isItemInTypeAndId(1, $data->id)?false:true',  
  36.                     ),  
  37.                     'del' => array(  
  38.                             'label'=>'取消展示',     // text label of the button  
  39.                             'url'=>'Yii::app()->controller->createUrl("focus/delete",array("id"=>$data->primaryKey,"type"=>1))',       // a PHP expression for generating the URL of the button  
  40.                             'imageUrl'=>'http://s.maylou.com/common/images/yzhu.jpg',  // image URL of the button. If not set or false, a text link is used  
  41.                             'options'=>array('style'=>'cursor:pointer;'), // HTML options for the button tag  
  42.                             'click'=>$click,     // a JS function to be invoked when the button is clicked  
  43.                             'visible'=>'SiteRecommend::isItemInTypeAndId(1, $data->id)?true:false',  
  44.                     )  
  45.             ),  
  46.         ),  
  47.     ),  
  48. ));  

 

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

 

Php代码  收藏代码
  1. $csrfTokenName = Yii::app()->request->csrfTokenName;  
  2.     $csrfToken = Yii::app()->request->csrfToken;  
  3.     $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";  
  4.     $Confirmation"你确定要这么做?";  
  5.     $afterDelete = 'function(link,success,data){ if(success) alert(data); }';  
  6.     $click=<<<EOD  
  7.     function() {  
  8.     if(!confirm("$Confirmation")) return false;;  
  9.     var th=this;  
  10.     var afterDelete=$afterDelete;  
  11.     $.fn.yiiGridView.update('build-oneprice-grid', {  
  12.     type:'POST',  
  13.     url:$(this).attr('href'),$csrf  
  14.     success:function(data) {  
  15.     $.fn.yiiGridView.update('build-oneprice-grid');  
  16.         afterDelete(th,true,data);  
  17.     },  
  18.     error:function(XHR) {  
  19.         return afterDelete(th,false,XHR);  
  20.     }  
  21.     });  
  22.     return false;  
  23.     }  
  24. EOD;  

 csrf不用管他,是安全验证,必须要有,否则会400报错.$click是js函数的字符窜,用了文档字符窜形式,注意结束的EOD前面必须没空格,也不能缩进。

 

这是Yii内置的yiiGridView Jquery插件,把请求提交到控制器的动作里面处理,然后返回结果并显示。最后还会更新一次gridview.
From:http://www.cnblogs.com/wuyunhua/archive/2011/12/25/2300900.html

  原文地址:http://hnlixf.iteye.com/blog/1553207

原创粉丝点击