您的位置:首页 > 编程语言 > PHP开发

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('*'),
),


效图如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: