layui数据表格分页失败

主要错误是表格直接在第一页上就把所有数据显示了,分页不起作用

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>table模块快速使用</title>
    <link rel="stylesheet" href="http://localhost:8001/layui/css/layui.css">
    <script src="http://localhost:8001/layui/layui.js"></script>
</head>
<body>

<table id="demo" lay-filter="test"></table>


<script>
    layui.use('table', function () {
        var table = layui.table;

        //第一个实例
        table.render({
            elem: '#demo'
            , height: 465
            , url: '/list' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {field: 'username', title: '用户名', sort: true, width: 100, fixed: 'left'}
                , {field: 'name', title: '姓名', width: 100}
                , {field: 'sex', title: '性别', width: 80, sort: true}
                , {field: 'birthday', title: '出生日期', width: 120}
                , {field: 'email', title: '邮箱', width: 177}
            ]]
            ,limits: [3,5,10,20]
            , limit: 10
            , parseData: function (res) {
                var result;
                console.log(this);
                console.log(JSON.stringify(res));
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {"code": res.code, "msg": res.msg, "count": res.count, "data": result};
            }
        });
    });
</script>
</body>
</html>

在这里插入图片描述

参考:https://blog.youkuaiyun.com/qq_35077107/article/details/101168088

您可以使用 layui表格渲染方法 `table.render()` 来渲染数据表格,并通过该方法的回调函数 `done` 来动态改变表格状态。 以下是一个示例代码,其中数据表格初始化时默认显示“未审核”状态的数据点击“审核”按钮后,将数据状态改为“已审核”,并重新渲染表格。 ```html <div class="layui-btn-group"> <button class="layui-btn layui-btn-sm" id="audit-btn">审核</button> </div> <table id="data-table" lay-filter="data-table"></table> <script> layui.use(['table', 'jquery'], function(){ var table = layui.table; var $ = layui.jquery; // 初始化数据表格 table.render({ elem: '#data-table', url: '/api/getData', cols: [[ {field: 'id', title: 'ID'}, {field: 'name', title: '名称'}, {field: 'status', title: '状态'} ]], where: {status: 'unaudited'}, // 默认显示未审核状态的数据 done: function(res, curr, count) { // 渲染完表格后的回调函数 if (res.code === 0) { // 数据加载成功 if (curr === 1) { // 第一页的数据已加载完成,可以进行状态处理 handleStatus(); } } else { // 数据加载失败 layer.msg(res.msg, {icon: 2}); } } }); // 处理数据状态 function handleStatus() { $('#data-table tbody tr').each(function(){ var tr = $(this); var rowData = table.cache['data-table'][tr.data('index')]; if (rowData.status === 'unaudited') { tr.addClass('unaudited'); } else { tr.addClass('audited'); } }); } // 监听审核按钮点击事件 $('#audit-btn').on('click', function(){ // 手动修改数据状态 $('#data-table tbody tr.unaudited').each(function(){ var tr = $(this); var rowData = table.cache['data-table'][tr.data('index')]; rowData.status = 'audited'; }); // 重新渲染表格 table.reload('data-table', {where: {status: 'audited'}}); }); }); </script> <style> .audited td {color: #aaa; text-decoration: line-through;} .unaudited td {background-color: #f2dede;} </style> ``` 在上述示例中,我们通过添加 `.unaudited` 和 `.audited` 两个 CSS 类来改变表格行的样式,以便区分已审核和未审核数据。同时,我们还使用了 layui分页功能,只对当前页的数据进行状态处理,以避免性能问题。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值