17 06 2020

html

<table class="layui-table" lay-data="{literal}{url:'/index/admin/page/newsInfo', id:'table1', text:{ none:'<div><i class=\'layui-icon layui-icon-face-smile-fine\' style=\'font-size: 30px;\' ></i></div>您的文章为空哦,点击左上角添加吧!'}, page: { layout: ['prev', 'page', 'next', 'count'] }, limit: 10,  }{/literal}" lay-filter="table1">

    <thead>
    <tr>
        <th lay-data="{type:'checkbox'}">ID</th>
        <th lay-data="{field:'title', minWidth: 200,}">名称</th>
        <th lay-data="{field:'img', templet: '#picTpl',  minWidth: 100,}">图片</th>
        <th lay-data="{field:'name',}">分类</th>
        <th lay-data="{field:'hits',width: 65}">点击</th>
        <th lay-data="{field:'order',width: 65}">排序</th>
        <th lay-data="{toolbar: '#tbOperate',  minWidth: 170, align: 'center'}">操作</th>
    </tr>
    </thead>

</table>

php

public function newsInfo(Request $request)
{
    $userInfo = Session::get('index_user');

    //产品类别大类id
    $cateinfo = Db::table('index_cate')->where('u_id',$userInfo['id'])->where('hz','=','news')->find();

    //该类下的小类
    $catesmall = Db::table('index_cate')->where('f_id',$cateinfo['id'])->order('order asc')->order('id asc')->select();

    $where = [];

    foreach($catesmall as $k => $v){
        array_push($where,$v['id']);
    }
    array_push($where,$cateinfo['id']);


    //获取每页显示的条数
    $limit= $request->param('limit');
    //获取当前页数
    $page= $request->param('page');


    //产品信息
    $cpInfo = Db::table('index_content')->where('c_id','in',$where)->order('order desc')->order('id desc')->paginate($limit,false,['page'=>$page])->toArray();
    $cpInfo = $cpInfo['data'];
    foreach($cpInfo as $k => $v){
        if ($v['c_id'] == $cateinfo['id']){
            $cpInfo[$k]['name'] = $cateinfo['name'];
        }else{
            foreach($catesmall as $a => $b){
                if ($v['c_id'] == $b['id']){
                    $cpInfo[$k]['name'] = $b['name'];
                }
            }
        }
    }
    //总条数
    $count = Db::table('index_content')->where('c_id','in',$where)->count();

    $data['code'] = 0;
    $data['data'] = $cpInfo;
    $data['count'] = $count;
    return json($data);


}

js

<script>
    layui.use(['jquery', 'form', 'table'], function() { //使用layui引入jq
        var $ = jQuery = layui.$,
            form = layui.form,
            table = layui.table,
            upload = layui.upload;

        $('.company-tree dd a').eq(2+7).addClass('active'); //样式选中

        //搜索
        $('#searchBtn').on('click', function(){
            var searchInput = $('#searchInput');
            //执行重载
            table.reload('table1', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                        title: searchInput.val()
                }
                , url: '/index/admin/page/newsSearch'//后台做模糊搜索接口路径
                , method: 'get'
            });
        });







        //删除选中
        $('#delAll').on('click', function(){
            var checkStatus = table.checkStatus('table1')
                , checkData = checkStatus.data
                , ids=[];

            if(checkData.length === 0){
                return layer.msg('请选择数据', function(){});
            }else {
                layer.confirm('真的删除选中项么', function(index){
                    layer.close(index);
                    checkData.forEach(function(n,i){
                        ids.push(n.id);
                    });
                    $.post("/index/admin/page/newsDel", { 'id': ids, batchFlag: 1 }, function(res){
                        //console.log(res);
                        if(res.code) {
                            layer.msg(res.msg);
                            table.reload('table1');
                        }else {
                            layer.msg(res.msg, {icon:5, shift: 6, shade: [0.2, '#000']});
                        }
                    });
                });
            }

        });

        //监听工具条
        table.on('tool(table1)', function(obj) {
            var data = obj.data;
            //console.log(obj);
            if (obj.event === 'del'){
                layer.confirm('真的删除么', function(index){
                    obj.del();
                    layer.close(index);
                    $.post("/index/admin/page/newsDel", { 'id': data.id }, function(res){
                        if(res.code) {
                            layer.msg(res.msg);
                        }else {
                            layer.msg(res.msg, {icon:5, shift: 6, shade: [0.2, '#000']});
                        }
                    });
                });
            }
        });



        //…
    });

</script>


延伸阅读
  1. 用threejs展示stl格式3D模型
  2. Layui简单图片上传及预览