layui渲染表格数据

好文章需要耐心阅读…茶凡—Matrix

请添加图片描述

layui 的渲染的数据有方法渲染和自动渲染。

🌿方法渲染:所有数据都写在js代码里面,html 只需要引用它,推荐采用这种方式

🌿自动渲染:所有都写在html 里面包括请求(注意这种写法必须是get请求,而且返回的状态码必须为0,不然会导致数据渲染不上去)需要写的代码重复率高,不推荐。

官方文档:table 数据表格文档 - Layui

1、layui 渲染数据

注意

本文中采用的是 thymeleaf + springboot + java + layui 来创建的项目,如果单纯写html可能会有一些区别。

方法渲染
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>首页</title>
    <link rel="stylesheet" th:href="@{//unpkg.com/layui@2.6.8/dist/css/layui.css}">
</head>

<body class="layui-layout-body">

<!--头-->
<!--在html的head元素中通过th:replace属性引入,值为模板的路径,
不需要模板的后缀后面::跟的是模板的ID(即公共header页面中th:fragment定义的值)-->
<div th:replace="head :: header"></div>

<div class="layui-body layui-container" style="margin:70px 0px 0px 20px; ">
    <table class="layui-hide" id="test" lay-filter='test'></table>
</div>

<script type="text/javascript" th:inline="none" th:src="@{//unpkg.com/layui@2.6.8/dist/layui.js}"></script>
<script>
    layui.use(['table', 'layer','laypage','form'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            laypage = layui.laypage,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#test',
            id: 'tableId',
            url: 'parent/getParentList',
            method:'get',
            title: '用户信息表',
            totalRow: true,
            height: 520,
            width:550,
            cols: [
                [
                {type: "numbers", fixed: 'aa'}
                ,{type: "checkbox", fixed: 'aa'}
                , {field: 'studentName', title: '学生姓名', width: 100}
                , {field: 'personNo', title: '学号', sort:true ,width: 120}
                , {field: 'departmentId', title: '班级', width: 80 }
                , {field: 'parentDeptId', title: '年级', width: 80}

            ]
            ],
            page: true , // 分页
            elem: '#test',
            limit:10,
            parseData: function (res) {  // 分页效果必须要加这个函数只加 page:true 不起作用。
                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.data.length, "data": result};
            }

        });
    });
    
</script>
</body>
</html>
自动渲染
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>首页</title>
    <link rel="stylesheet" th:href="@{//unpkg.com/layui@2.6.8/dist/css/layui.css}">
</head>

<body class="layui-layout-body">

<!--头-->
<!--在html的head元素中通过th:replace属性引入,值为模板的路径,
不需要模板的后缀后面::跟的是模板的ID(即公共header页面中th:fragment定义的值)-->
<div th:replace="head :: header"></div>


<div class="layui-body layui-container" style="margin:70px 0px 0px 20px; ">

    <table class="layui-table"
           lay-data="{height:500,width:1260, url:'/parent/getParentList', page:true, id:'test'}"
           lay-filter="test">
        <thead>
        <tr>
            <th lay-data="{checkbox:true}"></th>
            <th lay-data="{field:'departmentId', width:80, sort: true}">班级</th>
            <th lay-data="{field:'parentDeptId', width:80, }">年级</th>
            <th lay-data="{field:'personNo',width:120,}">学号</th>
            <th lay-data="{field:'studentName', width:80  }">学生</th>
        </tr>
        </thead>
    </table>
    
</div>

<script type="text/javascript" th:inline="none" th:src="@{//unpkg.com/layui@2.6.8/dist/layui.js}"></script>
<script>
    layui.use(['table', 'layer','laypage','form'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            laypage = layui.laypage,
            form = layui.form,
            table = layui.table;
    });

</script>
</body>
</html>
2、后台接口数据格式

后台的就不贴代码了,文章末尾可以获取

请添加图片描述

{
  "msg": "success",
  "code": 0,
  "data": [
    {
      "departmentId": 201505,
      "parentDeptId": 2015,
      "personNo": "201505019",
      "studentName": "何金贝"
    },
    {
      "departmentId": 201508,
      "parentDeptId": 2015,
      "personNo": "201508003",
      "studentName": "卿屹昊"
    },
    {
      "departmentId": 201601,
      "parentDeptId": 2016,
      "personNo": "201601001",
      "studentName": "黄星云"
    }
    ]
  }  

茶凡_Matrix仓亏地址:(CommuteRate: 通勤率后台初版 (gitee.com))

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Layui 表单数据渲染可以使用 layui.form 的 render 方法,该方法支持以下参数: - elem: 指定渲染的表单元素,可以是 DOM 对象,也可以是 CSS 选择器。 - data: 指定渲染数据,可以是 JSON 对象或数组。 - filter: 指定表单的过滤器,用于监听表单元素的变化。 示例代码如下: HTML 代码: ```html <form class="layui-form" lay-filter="example-form"> <div class="layui-form-item"> <label class="layui-form-label">用户名</label> <div class="layui-input-block"> <input type="text" name="username" lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">密码</label> <div class="layui-input-block"> <input type="password" name="password" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" lay-submit lay-filter="example">提交</button> <button type="reset" class="layui-btn layui-btn-primary">重置</button> </div> </div> </form> ``` JavaScript 代码: ```javascript var data = { username: '张三', password: '123456' }; layui.use(['form'], function () { var form = layui.form; // 渲染表单数据 form.render(null, 'example-form'); // 监听表单提交事件 form.on('submit(example)', function (data) { console.log(data.field); return false; }); }); ``` 在以上代码中,我们首先定义了一个 JSON 对象 data,其中包含了用户名和密码的值。然后使用 layui.form 的 render 方法渲染表单数据,其中 elem 参数指定渲染的表单元素为 example-form,也就是我们定义的表单元素的 lay-filter 属性值。最后,使用 form.on 监听表单的提交事件,当用户点击提交按钮时,输出表单的数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值