实例一
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', ), ), )); ?>
实例二
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"> /*<![CDATA[*/ var GetCheckbox = function (){ var data=new Array(); $("input:checkbox[name='selectdel[]']").each(function (){ if($(this).attr("checked")==true){ data.push($(this).val()); } }); if(data.length > 0){ $.post('<?php echo CHtml::normalizeUrl(array('/admin/words/delall/'));?>',{'selectdel[]':data}, function (data) { var ret = $.parseJSON(data); if (ret != null && ret.success != null && ret.success) { $.fn.yiiGridView.update('yw1'); } }); }else{ alert("请选择要删除的关键字!"); } } /*]]>*/ </script>
3.Action
public function actionDelall() { if (Yii::app()->request->isPostRequest) { $criteria= new CDbCriteria; $criteria->addInCondition('id', $_POST['selectdel']); Words::model()->deleteAll($criteria);//Words换成你的模型 if(isset(Yii::app()->request->isAjaxRequest)) { echo CJSON::encode(array('success' => true)); } else { $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index')); } } else throw new CHttpException(400,'Invalid request. Please do not repeat this request again.'); }
实例三
第一步: 设置视图:admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'mz-pro-try-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns'=>array( array( 'htmlOptions'=>array('width'=>"30px"), 'class' => 'CCheckBoxColumn', 'name'=>'id', 'value'=>'$data->id', 'id'=>'ids', 'headerTemplate'=>'{item}', 'selectableRows'=>2, ), 'id', 'name', array( 'class'=>'CButtonColumn', ), ), )); ?>
第二步: 设置视图:admin.php JS,通过ajax完成操作
<!-- 对复选框的操作 JS -->
<div class="row buttons"> <script type="text/javascript"> var data = new Object(); //对象 data.YII_CSRF_TOKEN='<?php echo Yii::app()->getRequest()->getCsrfToken() ?>'; function submitAjax(state){ var state = state; data.state = state; //为对象添加state属性,属性值为state 等同于:data['state'] = state data.checkedValue=$('#mz-pro-try-apply-grid').yiiGridView('getChecked', 'ids'); // data.state= state; if (data.checkedValue.length==0){ alert("至少选择一项"); return; } if(state == 6){ url = '/XXXXXXX/delall'; }else { url = '/XXXXXXX/state'; } // $.each(data,function(key,val){ // alert('data数组中,索引:'+key+'对应的值为:'+val); // }); $.ajax({ url: url, type:'get',//必须使用,不知道为什么 dataType:'json', data:data, success:function(data){ // alert(data); jQuery('#mz-pro-try-apply-grid').yiiGridView('update'); } }) } </script> <?php echo CHtml::button("已审核",array('onClick'=>'submitAjax(1);')); ?> <?php echo CHtml::button("撤销审核",array('onClick'=>'submitAjax(2);')); ?> <?php echo CHtml::button("处理3",array('onClick'=>'submitAjax(3);')); ?> <?php echo CHtml::button("处理4",array('onClick'=>'submitAjax(4);')); ?> <?php echo '    ┃    ';?> <?php echo CHtml::button("处理5",array('onClick'=>'submitAjax(5);')); ?> <?php echo '    ┃    ';?> <?php echo CHtml::button("删除",array('onClick'=>'submitAjax(6);')); ?> </div>
第三部: 在控制器写处理函数
//复选框:批量删除 public function actionDelall(){ foreach($_GET['checkedValue'] as $value){ $this->loadModel($value)->delete(); // echo $value; } } //复选框:批量修改状态 public function actionState(){ $connection = Yii::app()->db; //$state 等于1(已审核),等于2(未审核),等于3(处理3),等于4(处理4),等于5(处理5), $state = $_GET['state']; foreach($_GET['checkedValue'] as $v){ $sql = "UPDATE 数据表名 SET state = $state WHERE id = $v "; $command = $connection->createcommand($sql); $command->query(); } }