Yii中ajaxLink的使用

 

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


效图如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值