tp5+ajax+layui实现多删(包含图片)

  • A+
所属分类:PHP ThinkPHP
PHP代码

    /**
     * 多删,包括图片
     * @AuthorHTL
     * @param     [type]                   $arrId [input获取id的时候用/a强制转换成数组]
     * @return    [type]                          [description]
     */
    public function delAll($arrId)
    {
        
        $arrId = input('arrId');
        
        if(empty($arrId)){
            return $this->error("非法操作!",url('classify'));
        }
        // 将获取到的字符串分割为数组
            $aid = explode(",",$arrId);
            foreach ($aid as $id) {
                // 查询出对应id的logo数据
                $logo_data = LogoModel::get($id);

                // 将查出来的logo图片赋给新的变量
                $logo_image = $logo_data['image'];
                // 删除旧图片
                $filePath = $logo_image;

                $imageUrl='./uploads/'."$filePath";

                $info = @unlink($imageUrl);

                if($info>0){
                $message = "图片删除成功";
                }else{
                $message = "图片删除失败";
                }
            }
        $result = LogoModel::destroy($arrId);
        if ($result) {            
            $this ->delete($arrId);
            return $this->success('删除成功,'.$message);
        }else{
            return $this->error('删除失败,'.$message,url('classify'));
        }
        $this ->delete($arrId);

    }
HTML代码
<xblock><button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button><span class="x-right" style="line-height:40px">共有数据:{$count} 条</span></xblock>
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>
                            <input class="layui-unselect checkHeader layui-form-checkbox" type="checkbox">
                        </th>
                        <th>ID </th>
                    </tr>
                </thead>
                <tbody id="x-img">
                {volist name='logo' id='vo'}
                    <tr>
                        <td>
                            <input type="checkbox" class="layui-unselect checkFooter layui-form-checkbox" value="{$vo.aid}">
                        </td>
                        <td> {$vo.aid} </td>
                    </tr>
                    {/volist}
                </tbody>
            </table>
<script>
    // ajax 实现全选
            $(function () {
              var clickNum = 0;
              $('.checkHeader').click(function () {
                if (clickNum % 2 == 0) {
                  $('.layui-form-checkbox').each(function () {
                    this.checked = true;
                  });
                } else {
                  $('.layui-form-checkbox').each(function () {
                    this.checked = false;
                  });
                }
                clickNum++;
              })
            })

            //批量删除提交
             function delAll (obj,arrId) {
                layer.confirm('确认要删除吗?',function(index){
                    //捉到所有被选中的,发异步进行删除
                    var arrId = '';
                    var code = '';
                    $('.checkFooter:checked').each(function () {
                      arrId += $(this).val() + ',';
                    })
                    arrId = arrId.substr(0, arrId.length - 1);
                    $.ajax({
                      url:"{:url('delAll')}",
                      type:'post',
                      dataType:'json',
                      data:{"arrId":arrId},
                      success: function (data){
                        console.log(code);
                        console.log(data);
                        if (data.code == 1) {
                          layer.msg(data.msg, {icon: 1, time: 2000});
                          // 删除dom节点
                          $('.checkFooter:checked').each(function () {
                            ($(this)).parent().parent().remove();
                          })
                        }else{
                          layer.msg(data.msg, {icon: 2, time: 2000});
                        }
                      }
                    })
                });
             }
</script>
  • 免费领红包
  • 支付宝红包扫一扫
  • weinxin
  • 免费提现
  • 支付宝全额提现
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: