layui 表格

用layui生成表格包括分页 按钮

前段js代码

layui.use(['element','table'], function(){//所有代码要放到layui.use下面,

var table = layui.table;
var $ = layui.jquery;
		//表格生成
        table.render({
            elem: '#gongaozhanshi'
            ,url:'/qiyewx/message/gonggaozhanshi/'
            ,cols: [[
                {type:'numbers'}
                // ,{field:'id', width:80, title: '序号', sort: true}
                ,{field:'title', width:150, title: '标题'}
                ,{field:'created_at', width:150, title: '消息时间', sort: true}
                ,{field:'content', width:1000, title: '公告内容'}
                ,{field:'receiver_department', width:180, title: '接收人'}
                ,{field:'is_read', width:85, title: '详情',templet:function (b) {
                    if (b.is_read == 1){
                        return "<span class='layui-badge layui-bg-green'>已读</span>";
                    }else{
                        return "<button name='id'  value="+b.id+" class='layui-badge gg gg"+b.id+" '><span>未读</span></button>" +
                            "<span class='layui-badge layui-bg-green ggyd"+b.id+"' style='display: none'>已读</span>";
                    }
                    }}
            ]]
            ,page: true
        });

//ajax点击事件
        $(document).on('click',".gg",function () {
            var id = this.value;
            $.ajax({
                type: "get",
                url: "/qiyewx/message/changestatusgg?id="+id,
                dataType: "json",
                success: function (res) {
                    if (res === true){
                        $("."+"gg"+id).hide();
                        $("."+"ggyd"+id).show();
                    }else{

                    }
                }
            });
        })

后端主要是 注意返回的数据格式

    public function gonggaozhanshi(){
        $data = input('param.');
        $userid = session('user.id');

        $department_list = WorkuserService::getcrmDepartment();
        $sql = "SELECT * FROM work_message_announcement INNER JOIN work_message_announcement_rel ON work_message_announcement_rel.announcement_id = work_message_announcement.id WHERE username=".$userid." ORDER BY work_message_announcement.id desc";
        $all_mes_re = Db::query($sql);
        foreach ($all_mes_re as $key => $val){
            $department = explode(',',$val['receiver']);
            foreach ($department as $k => $v){
                if(empty($all_mes_re[$key]['receiver_department'])){
                    $all_mes_re[$key]['receiver_department'] = $department_list[$v];
                }else{
                    $all_mes_re[$key]['receiver_department'] = $all_mes_re[$key]['receiver_department'].','.$department_list[$v];
                }
            }
        }
        $start=($data['page']-1)*$data['limit']; #计算每次分页的开始位置
        $pagedata=array_slice($all_mes_re,$start,$data['limit']);#用array_slice来进行数组分页
        $data = [
            'code'=>'0',
            'msg'=>'',
            'count'=>count($all_mes_re),
            'data'=>$pagedata
        ];
        return $data;

    }```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值