Yii CGridView 中实现批量删除

来源:互联网 发布:淘宝手机端链接 编辑:程序博客网 时间:2024/05/21 16:54

1.在视图中 CGridView中的columns添加

              array(                            'selectableRows' => 2,                            'footer' => '<button type="button" onclick="GetCheckbox();" style="width:76px">批量删除</button>',                            'class' => 'CCheckBoxColumn',                            'headerHtmlOptions' => array('width'=>'33px'),                            'checkBoxHtmlOptions' => array('name' => 'selectdel[]'),                    ),

作用是添加多选框


2.js代码



<script type="text/javascript">    function GetCheckbox(){            var data=new Array();            $("input:checkbox[name='selectdel[]']").each(function (){                    if($(this).attr("checked")==true){                            data.push($(this).val());                    }            });            if(data.length > 0){                    $.post("index.php?r=ip/delall",{'selectdel[]':data}, function (data) {                            if (data=='ok') {                                    alert('删除成功!');         window.open('index.php?r=ip/admin','indexFrame');;                          }                    });            }else{                    alert("请选择要删除的选项!");            }    }</script>


3.Action

/* * 作用:批量删除 */  public function actionDelall() {   if (Yii::app()->request->isPostRequest)   {     $criteria= new CDbCriteria;     $criteria->addInCondition('ip_id', $_POST['selectdel']);     Ip::model()->deleteAll($criteria);//Words换成你的模型      if(isset(Yii::app()->request->isAjaxRequest)) {       echo 'ok';     } else       $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));   }   else     throw new CHttpException(400,'Invalid request. Please do not repeat this request again.'); }


顺便列出一些常用的类型:

<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'chapter-grid', 'dataProvider'=>$model->search(),  //数据结果集 'filter'=>$model,      'columns'=>array(      'id',      //锚点<a href="http://www.gulianqiang.com/"></a>       array(              'name'=>'name',              'type'=>'raw',              'value'=>'CHtml::link($data->name,"/book/$data->id")',         ),      //图片       array(              'name'=>'image',              'type'=>'image',              'value'=>'LImages::getPath("book").$data->image',//图片相对路径         ),       //下拉列表        array(              'name'=>'type',              'value'=>'Lookup::item("chapterType",$data->type)',              'filter'=>Lookup::items('chapterType'),         ),       //内容截取        array(              'name'=>'content',              'type'=>'html',              'value'=>'mb_substr(htmlspecialchars_decode($data->content),0,100,"utf-8")',         ),               //时间        array(              'name'=>'create_time',              'type'=>'datetime',         ),       // 根据相关信息读数据库        array(              'name'=>'user_id',              'value'=>'User::model()->findbyPk($data->user_id)->username',              'filter'=>false,         ),  array(   'class'=>'CButtonColumn',  ), ),)); ?>


原创粉丝点击