yii框架全选反选批量删除

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\data\Pagination;
use app\models\Shop;

class LoginController extends Controller
{
    public $enableCsrfValidation = false;
    function actionIndex(){
        //$test=new TestForm();    //实例化model模型
       // $arr=$test->find();
        $arr = Shop::find();
        //$countQuery = clone $arr;
        $pages = new Pagination([
            //'totalCount' => $countQuery->count(),
            'totalCount' => $arr->count(),
            'pageSize'   => 2   //每页显示条数
        ]);
        $models = $arr->offset($pages->offset)
            ->limit($pages->limit)
            ->all();
        return $this->render('show', [
            'models' => $models,
            'pages'  => $pages
        ]);

    }
    function actionDel(){
       $id=\yii::$app->request->post('id');
       // print_r($id);die;
        $sql=\yii::$app->db->createCommand("delete from shop where u_id in ($id)")->execute();
        if($sql){
        return $this->redirect("?r=login/index");
        }else{
            return $this->redirect("?r=login/index");
        }
    }

}



v

<?php
use yii\widgets\LinkPager;
?>
<center>
    <table border="1">
        <tr>
            <td>ID</td>
            <td>名称</td>
            <td>内容</td>
        </tr>
        <?php foreach($models as $k=>$v){ ?>
        <tr id="list">
            <td><input type="checkbox" class="everycb"  name="run"  value="<?php echo $v['u_id']?>"/><?php echo $v['u_id']?></span></td>
            <td><?php echo $v['u_name']?></td>
            <td><?php echo $v['u_son']?></td>
        </tr>
        <?php } ?>

        <input type="checkbox" class="checkall2"/>全选
        <input type="button"id="but" value="反选"/>
        <input type="button" value="批量删除" id="btn3"/>
    </table>
    <?php  echo LinkPager::widget([
        'pagination' => $pages,
    ]);
    ?>
</center>
<script src="jquery.js"></script>
<script>
//全选
    $(".checkall2").click(function(){
        var isChecked = $(this).prop("checked");
        $(".everycb").prop("checked",isChecked);
    })
    //反选
$("#but").click(function(){
    $('input:checkbox').each(function(){
        this.checked=!this.checked;
    })
})
    $("#btn3").click(function(){
        var str='';
        $("input[name='run']:checked").each(function(){
            str+=$(this).val()+",";
        })
        var length=str.length-1;
        var id=str.substr(0,length);
        $.ajax({
            type:"POST",
            url:"?r=login/del",
            data:{id:id},
            success:function(result)
            {
                alert(result);
            }
        })
    })
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值