layui监听复选框checkbox选中,分页选中处理

具体实现代码如下(需要关注三个位置:done 表格加载完毕方法,cols 中复选框栏定义,table.on 中对复选框选中或取消的状态定义)这三个地方的代码直接复制去用就行了

最终选中的数据id为:ids

java 复制代码
<script>

    layui.use(['table', 'form', 'upload','laydate'], function () {

        // 设置全局变量以保存选中行信息(仅需要id的话在你的业务位置调用ids即可,数据格式是int数组)
        let ids = new Array();

        // 存储所有选中的数据(需要行内全量数据在你的业务位置调用lists即可,数据格式是对象集合)
        var lists = new Array();

        // 保存当前页全部数据id,点击全选时使用
        let tableIds = new Array();

        //第一个实例
        table.render({
            elem: '#currentTableId'
            , method: "post"
            , dataType: "Json"
            , id: 'layuiReload'
            , url: '/page/severalquality/getshipPlan' //数据接口
            , toolbar: '#toolbarTem'
            , page: true //开启分页
            , done: function (res, curr, count) {

                // 设置当前页全部数据id到全局变量
                tableIds = res.data.map(function (value) {
                    return value.id;
                });
                // 设置当前页选中项
                $.each(res.data, function (idx, val) {
                    if (ids.indexOf(val.id) > -1) {
                        val["LAY_CHECKED"] = 'true';
                        //找到对应数据改变勾选样式,呈现出选中效果
                        let index = val['LAY_TABLE_INDEX'];
                        $('tr[data-index=' + index + '] input[type="checkbox"]').click();
                        form.render('checkbox'); //刷新checkbox选择框渲染
                    }
                });
                // 获取表格勾选状态,全选中时设置全选框选中
                let checkStatus = table.checkStatus('test');
                if (checkStatus.isAll) {
                    $('.layui-table-header th[data-field="0"] input[type="checkbox"]').prop('checked', true);
                    form.render('checkbox'); //刷新checkbox选择框渲染
                }

            }
            , cols: [[ //表头
                { field: 'id', title: '数据编号', sort: true, hide: true }
                , { field: 'id', sort: true, type: 'checkbox' }//在此声明表格复选框
                , { field: 'DataNumber', align: 'center', title: '序号', width: 60, type: 'numbers' }
                , { field: 'shiptitle', align: 'center', title: '船名' }
                , { field: 'carrierstitle', align: 'center', title: '承运商' }
                , { field: 'startPortName', align: 'center', title: '始发港' }
                , { field: 'destPortName', align: 'center', title: '到港' }
                , { field: 'arrivalTime', align: 'center', title: '运达时间' }
                , { field: 'shipmentTon', align: 'center', title: '装油量' }
                , { field: 'realTon', align: 'center', title: '卸油量' }
                , {
                    field: 'superConsumption', align: 'center', title: '是否超耗索赔', hide: true, templet: function (res) {
                        if (res.superConsumption == true) {
                            return "是";
                        } else {
                            return "否";
                        }
                    }
                }
                , { field: 'claimSum', align: 'center', title: '索赔量' }
                , { field: 'practicalprice', align: 'center', title: '实际索赔金额' }
                , { field: 'claimcompensationInfostateName', align: 'center', title: '索赔状态' }
                , { field: 'auditremark', align: 'center', title: '审核备注', hide: true }
            ]]
        });


        //使用on监听checkbox选中状态并进行处理(tableFilter为table的lay-filter值)
        table.on('checkbox(tableFilter)', function (obj) {

            if (obj.checked == true) {
                if (obj.type == 'one') {
                    ids.push(obj.data.id);
                    lists.push(obj.data);

                } else {
                    for (let i = 0; i < tableIds.length; i++) {
                        //当全选之前选中了部分行进行判断,避免重复
                        if (ids.indexOf(tableIds[i]) == -1) {
                            ids.push(tableIds[i]);
                            var checkStatus = table.checkStatus('layuiReload'); //layuiReload 为table声明的id
                            lists.push(checkStatus.data[i]);
                        }
                    }
                }
            } else {
                if (obj.type == 'one') {
                    let i = ids.length;
                    while (i--) {
                        if (ids[i] == obj.data.id) {
                            ids.splice(i, 1);
                            lists.splice(i, 1);
                        }
                    }
                } else {
                    let i = ids.length;
                    while (i--) {
                        if (tableIds.indexOf(ids[i]) != -1) {
                            ids.splice(i, 1);
                            lists.splice(i, 1);
                        }
                    }
                }
            }
        });

</script>
相关推荐
白猫不黑10 分钟前
Web 应用中“敏感信息泄露“的常见位置?
前端·网络·web安全·网络安全·信息安全·渗透测试
张风捷特烈16 分钟前
Flutter UI 解耦 - 天下大势,合久必分, 分久必合
android·前端·flutter
安冬的码畜日常21 分钟前
【工欲善其事】深入理解 Node.js 带并发上限的异步任务批量执行逻辑
javascript·设计模式·node.js·ai编程·异步编程·并发执行
meilindehuzi_a2 小时前
Vue3组件样式隔离与组件通信实战:从 scoped、props 校验到记事本应用
前端·javascript·vue.js
LaughingZhu2 小时前
Product Hunt 每日热榜 | 2026-07-28
前端·神经网络·react.js·搜索引擎·前端框架
陈随易9 小时前
在 VSCode 里,把项目一键部署到服务器
前端·后端·程序员
Highcharts.js9 小时前
教程:基于 React + Highcharts 构建一个单页应用程序、按需数据拉取与图表渲染
开发语言·前端·数据结构·react.js·前端框架·highcharts·页面应用
To_OC9 小时前
我被 useState 坑了两次之后,终于把它的脾气摸透了
前端·javascript·react.js
鱼樱前端9 小时前
我用 Claude Code 后,编码效率翻 3 倍。但更值钱的是别的。
前端·后端·ai编程
_lucas10 小时前
给知识库网站接入AI问答
前端·ai编程·全栈