zoukankan      html  css  js  c++  java
  • PHP 批量删除的实现

    布局效果

    布局代码

    <button type="button" class="btn btn-sm btn-danger btn-erbi-danger" id="batchDel" style="margin-right:20px;">批量删除</button>
    
    <tr>
        <th><input id="checkAll" type="checkbox"></th>
        <th>ID</th>
        <th>所属公司</th>
        <th>姓名</th>
        <th>性别</th>
        <th>身份证号</th>
        <th>手机号</th>
        <th>住址</th>
        <th>备注</th>
        <th>标签</th>
        <th>创建时间</th>
        <th>操作</th>
    </tr>
    
    <volist name="result" id="vo">
        <tr data-id="{$vo.id}" data-table="company">
            <td><input class="checkOne" type="checkbox" data-id="{$vo.id}"></td>
            <td>{$vo.id}</td>
            <td>{$vo.company_name}</td>
            <td>{$vo.name}</td>
            <td>{$vo.sex_str}</td>
            <td>{$vo.id_card}</td>
            <td>{$vo.telephone}</td>
            <td>{$vo.address}</td>
            <td>{$vo.remark}</td>
            <td>{$vo.tag_str}</td>
            <td>{$vo.create_time|date='Y-m-d H:i',###}</td>
            <td>
                <?php if (!$_SESSION['_admin_is_company']) { ?>
                <a href="javascript:;"  class="info_tag">标签</a>
                <?php }?>
                <a href="javascript:;" data-id="{$vo.id}" class="info_edit">编辑</a>
                <a href="javascript:;" data-id="{$vo.id}" class="info_del">删除</a>
            </td>
        </tr>
    </volist>
    

    一个checkAll,一个checkOne。一个ID,一个Class。

    增加全选反选事件

    // 全选,反选
    $("#checkAll").on('change', function () {
        if ($(this).is(":checked")) { // 全选
            $(".checkOne").prop("checked",true);
        } else { // 反选
            $(".checkOne").prop("checked",false);
        }
    });
    

    增加删除事件,获取id

    // 批量删除
    $("#batchDel").on('click', function () {
        var ids = [];
    
        // 获取选中的id
        $('tbody input.checkOne').each(function (index, el) {
            if ($(this).prop('checked')) {
                ids.push($(this).data('id'))
            }
        });
    
        layer.confirm('确认要删除吗?' + ids.toString(), function (index) {
            //捉到所有被选中的,发异步进行删除
            ajaxBatchDel(ids.toString());
        });
    });
    
    // ajax批量删除
    function ajaxBatchDel(ids) {
        // ajax设置不通过
        $.ajax({
            type: 'POST',
            url: 'ajaxBatchDel',
            data: {ids: ids},
            dataType: 'json',
            success: function (data) {
                if (data.errno == 0) {
                    layer.msg('删除成功', {icon: 1});
                    $(".checkOne:checked").parents('tr').remove();
                } else {
                    layer.msg(data.errdesc, {icon: 5});
                    return false;
                }
            }
        });
    }
    

    批量软删除

    public function ajaxBatchDel() {
        $ids = $_POST['ids'];
        if (!$ids){
            $this->json->setErr(10001,'请选择要删除的内容');
            $this->json->Send();
        }
    
        $employee = M('employee');
        $flag = $employee->where(['id'=>['in',$ids]])->save(['status'=>0]);
        if($flag){
            $this->json->setErr(0, '删除成功');
            $this->json->Send();
        }else{
            $this->json->setErr(10099, '删除失败');
            $this->json->Send();
        }
    }
    
    
  • 相关阅读:
    Spring的AOP深入理解
    枚举和注解学习笔记
    单例模式
    工厂设计模式
    网络编程
    多线程笔记
    IOI2021集训队作业
    计蒜客 mark
    51nod mark
    关于此博客
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/11359143.html
Copyright © 2011-2022 走看看