YII2 jquery全选 反选 全不选 批量删除 分页

视图层

<?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)");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值