Yii中ajaxLink的使用
2014-04-25 17:04
281 查看
视图admin.php添加多选按钮
<?php $this->widget('bootstrap.widgets.TbGridView', array( 'id'=>'dh-webconfig-grid', 'dataProvider'=>$model->search(), //'filter'=>$model, 'type'=>'striped bordered condensed', 'columns'=>array( array( 'class'=>'CCheckBoxColumn', 'id'=>'example-check-boxes',//the columnID for getChecked 'selectableRows'=>2,//2表示多选 'headerTemplate'=>'{item}<span style="font-size:12px;color: #0088CC;"> 全选</span>', 'htmlOptions'=>array('style'=>'width: 50px'), ), 'web_id', 'web_title', 'web_keyword', 'web_desc', 'web_url', 'web_logo', /* 'web_copyright', 'com_name', 'com_address', 'com_mobile', 'com_tel', 'com_fax', 'com_email', 'com_contacts', */ array( 'class'=>'bootstrap.widgets.TbButtonColumn', 'htmlOptions'=>array('style'=>'width: 50px'), ), ), )); ?>
在多选控钮下设置一个处理ajax请求的链接
<?php $url=$this->createUrl('mydelete');//对应controller下的ActionMydelete(){}; echo CHtml::ajaxLink( '删除所选', $url, array( 'type'=>'POST', //theIds:表示POST到服务器的信息,接收时用$_POST['theIDS'];注意大小写要一致 //You may also call the JavaScript function $.fn.yiiGridView.getChecked(containerID,columnID) to retrieve the key values of the checked rows. 'data'=>'js:{theIds:$.fn.yiiGridView.getChecked("dh-admin-grid","example-check-boxes")}', 'success'=>'function(msg){ if(msg=="no"){ alert("没有选择要删除的项"); }else{ alert("删除成功"); window.location.reload(); } }' ), array( 'href'=>$url,//href=$url这一项一定要加上去,不然整个ajaxLink不能使用。 //这里的class的值可以参考http://www.cniska.net/yii-bootstrap/#tbButton里的样式,只要将对应的btn btn-small赋值到class即可。 //可选参数:btn btn-large,btn,btn btn-small,btn btn-mini,btn btn-primary btn-large,btn btn-primary,btn btn-primary btn-small,btn btn-primary btn-mini等。 'class'=>'btn btn-small', ) ); ?>
控制器:DhAdminController.php增加如下方法
public function actionMydelete() { if(isset($_POST['theIds'])){ foreach ($_POST['theIds'] as $id){ // $model = $this->loadModel($id); // echo $model->username; $this->loadModel($id)->delete(); } }else{ echo 'no'; } }
别记了访问控制也要设置:
array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=>array('index','view','Mydelete'), 'users'=>array('*'), ),
效图如下:
相关文章推荐
- yii_wiki_394_javascript-and-ajax-with-yii (在yii 中使用 javascript 和ajax)
- PHP的Yii框架中View视图的使用进阶
- YII COOKIE 使用
- yii中使用phpexcel
- yii框架表单模型使用及以数组形式提交表单数据示例
- 6 yii2.0使用migrate创建后台登陆
- 3:Yii使用表单(Yii权威指南)
- yii 中CHttpSession的使用
- Yii终止运行,进行跳转,不使用exit进行终止程序。
- YII Framework学习教程-YII的V-view的深入了解和使用-2011-11-16
- 4:使用数据库的DAO和AR(Yii权威指南)
- Yii框架使用魔术方法实现跨文件调用功能示例
- YII框架之soap协议的配置跟使用(Webservice)
- Yii的relations方法的使用
- 详解PHP的Yii框架中自带的前端资源包的使用
- yii使用activeFileField控件实现上传文件与图片的方法
- 使用 BEGINCONTENT() 和 ENDCONTENT() 设定 YII 的 LAYOUTS
- YII框架使用SRBAC实现RBAC权限访问控制
- YII 局部CSRF、令牌使用sessio存储
- Yii框架官方指南系列17——使用表单:创建模型