视图层
<?php use yii\widgets\LinkPager; ?> <script src="jquery-1.9.1.min.js"></script> <table class="table"> <tr> <td></td> <td>标题</td> <td>作者</td> <td>添加时间</td> </tr> <?php foreach($res as $k=>$v):?> <tr> <td><input type="checkbox" id="<?php echo $v['id']?>" name="check[]" value="<?php echo $v['id']?>"></td> <td><?php echo $v['title'] ?></td> <td><?php echo $name ?></td> <td><?php echo $v['time']?></td> </tr> <?php endforeach;?> </table> <?php echo LinkPager::widget([ 'pagination'=>$pages, 'nextPageLabel'=>'下一页', 'firstPageLabel'=>'首页' ]) ?> <button id="check">全选</button> <button id="check_fx">反选</button> <button id="check_bx">全不选</button> <button id="check_del">批量删除</button> <script> $(function(){ //alert(0); //全选 $("#check").click(function(){ $("input[name='check[]']").each(function(){ $(this).prop('checked',true); }) }) //全不选 $("#check_bx").click(function(){ $("input[name='check[]']").each(function(){ $(this).prop('checked',false); }) }) //反选 $("#check_fx").click(function(){ var check=$("input[name='check[]']"); for(var i=0;i<check.length;i++){ if(check.eq(i).prop('checked')==true){ check.eq(i).prop('checked',false); }else { check.eq(i).prop('checked',true); } } }) //批量删除 $("#check_del").click(function(){ var str=''; $("input[name='check[]']:checked").each(function(){ str+=','+$(this).val(); //alert(str); $(this).parent().parent().remove(); }) str=str.substr(1); //alert(str); var url="index.php?r=show/del"; $.get(url,{str:str},function(){ }) }) }) </script>控制器
//展示 public function actionShowss(){ $model=new Title(); $name=Yii::$app->session->get("username"); $arr=$model::find(); //var_dump($arr);die; //print_r($arr);die; $pages = new Pagination(['totalCount' =>$arr->count(),'pageSize'=>'3']); //实例化分页类,带上参数(总条数,每页显示条数) $res = $arr->offset($pages->offset)->limit($pages->limit)->all(); return $this->render('showd',['res'=>$res,'pages'=>$pages,'name'=>$name]); } //批量删除 public function actionDel(){ $res= Yii::$app->request; $id= $res->get('str'); //echo $id; $model=new Title(); $model->del($id); } }MODEL层
<?php namespace frontend\models; use Yii; use yii\web\UploadedFile;
public function del($id){ return $this->deleteAll("id in($id)"); }