layui实现数据列表的复选框回显

layui版本2.8以上

实现效果如图:

html 复制代码
<input type="hidden" name="id" id="id" value="{:g_val( 'id',0)}">
<div id="tableDiv">
    <table class="layui-hide" id="table_list" lay-filter="table_list"></table>
</div>

<script src="layui/layui.js"></script>
javascript 复制代码
//方法级渲染
       table.render({
            elem: '#table_list'
            , url: "/admin/****/****_search"
            , cols: [[
                {type:'checkbox'}
                ,{field: 'name', title: '设备名称', width: 200}
                , {field: 'model_number', title: '设备型号', width: 200}
                , {field: 'merchant_name', title: '所属商户', width: 200}
                , {field: 'status', title: '设备状态', width: 120}
                , {field: 'remark', title: '备注', width: 200}
                , {field: 'create_time', title: '创建时间', width: 250}
                , {field: 'update_time', title: '更新时间', width: 250}
            ]]
            // , page: true
            // , limit: 30
            // , height: document.documentElement.offsetHeight - 100
            , height:  'full-180'  // 表格高度根据浏览器自适应
            , page: false// 取消分页时使用
            , limit: Number.MAX_VALUE// 取消分页时使用
            , done: function (res, curr, count) {
                $("[data-field='status']").children().each(function () {
                    if ($(this).text() == '1') {
                        $(this).text("播放中")
                    } else if ($(this).text() == '2') {
                        $(this).text("休息中")
                    }
                });
                $("[data-field='deleted']").children().each(function () {
                    if ($(this).text() == '1') {
                        $(this).text("正常")
                    } else if ($(this).text() == '0') {
                        $(this).text("禁用")
                    }
                });

                $.ajax({
                    url: "/admin/****/get****",
                    type: "post",
                    data: { id: $('#id').val()},
                    success: function (data) {
                        //遍历ajax返回的结果
                        //打印出data的格式为数组实例:[1,2,3,6]
                        for (var i = 0; i < data.length; i++) {
                            //遍历数据表格
                            $.each(res.data, function (index, row) {
                                //判断ajax返回的id是否与数据行中的id相等
                                if (row.id == data[i]) {
                                    // console.log(row.LAY_INDEX);
                                    // console.log(row.LAY_TABLE_INDEX);
                                    $('#tableDiv tr[data-index=' + row.LAY_INDEX + '] input[type="checkbox"]').next().eq(0).click();
                                    $('#tableDiv tr[data-index=' + row.LAY_INDEX + '] input[type="checkbox"]').next().eq(1).click();
                                }
                            })
                        }
                    }
                });
            }
        });
相关推荐
其实防守也摸鱼1 小时前
个人学习 Pwn 思路:从基础到漏洞利用的进阶路径
大数据·前端·人工智能·chrome·算法·安全·网络安全
朱涛的自习室2 小时前
212 个需求、22 万行代码:Agent 接管开源项目的 19 天
android·前端·人工智能
aZhe的全栈知识分享2 小时前
AI 还原 Figma 效果总达不到预期?先别急着让它写代码
前端·后端·面试
review445432 小时前
Codex Hooks 入门实战:生命周期事件、matcher 与5分钟跑通一个钩子
前端·agent·vibecoding
函数小陈2 小时前
阿里云部署DSH与RAM完成运维
前端·后端
用户499335025072 小时前
JS 作用域劫持的死穴:动态 this 穿透,以及 AST 怎么救
前端
IT_陈寒3 小时前
React的setState真是个演员,你以为它同步其实在装睡
前端·人工智能·后端
孟健4 小时前
Claude Code 同需求写两版对比:装不装 frontend-design 差多远?
前端·ai编程·claude
OpsEye5 小时前
大模型 Token 计量不准,是什么原因,怎么解决??
javascript·ai编程
梦de天空一米16886 小时前
【一个重复id让游戏卡在62%,聊聊配置表检查】
前端