上一篇
根据Layui官方2025年8月发布的v2.11.2稳定版,表格组件(Table)迎来重大更新:
layui.extend()
模块扩展机制<!-- 搜索区域 --> <div class="layui-form demoTable"> <div class="layui-inline"> <input class="layui-input" id="searchId" placeholder="请输入用户ID"> </div> <button class="layui-btn" data-type="reload">立即搜索</button> </div> <!-- 表格容器 --> <table id="userTable" lay-filter="userTable"></table>
layui.use(['table', 'form'], function(){ var table = layui.table; // 初始化表格 var tableIns = table.render({ elem: '#userTable', url: '/api/user/list', // 后端接口 cols: [[ {field:'id', title:'ID', width:80, sort: true}, {field:'username', title:'用户名'}, {field:'email', title:'邮箱'}, {field:'createTime', title:'注册时间', templet:'#dateTpl'} ]], page: true, limits: [10,20,50], id: 'testReload' }); // 监听搜索按钮 $('.layui-btn').on('click', function(){ tableIns.reload({ where: { id: $('#searchId').val() }, page: { curr: 1 } }); }); });
<script type="text/html" id="dateTpl"> {{ d.createTime.replace(/T/g,' ').substring(0,19) }} </script>
@Controller @RequestMapping("/api/user") public class UserController { @Autowired private UserService userService; @ResponseBody @RequestMapping("/list") public Map<String, Object> listUsers( @RequestParam(required = false) Integer id, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit ) { // 1. 构建查询条件 UserQuery query = new UserQuery(); if (id != null) query.setId(id); // 2. 执行分页查询 Page<User> pageData = userService.findPage(query, page, limit); // 3. 构造Layui标准响应 Map<String, Object> res = new HashMap<>(); res.put("code", 0); res.put("msg", ""); res.put("count", pageData.getTotal()); res.put("data", pageData.getRecords()); return res; } }
// 前端模板 <script type="text/html" id="filterTpl"> <select name="status" lay-filter="statusFilter"> <option value="">全部状态</option> <option value="1">激活</option> <option value="0">冻结</option> </select> </script> // JS监听 form.on('select(statusFilter)', function(data){ tableIns.reload({ where: { status: data.value } }); });
// 一次性加载全部数据 $.ajax({ url: '/api/user/all', success: function(res){ table.render({ data: res.data, page: true, limits: [10,20,50] }); } });
// 建立WebSocket连接 var sock = new SockJS('/ws'); sock.onmessage = function(e){ var data = JSON.parse(e.data); table.reload('testReload', { data: data.newRecords }); };
Q1:表格不显示数据?
code
是否为0data
字段是否为数组格式Q2:分页按钮无效?
page: true
配置存在limits
和limit
参数是否正确count
总条数Q3:模板不生效?
templet
选择器ID是否正确{{ d.fieldName }}
语法前端页面 → 用户操作 → 触发reload → 携带参数 → 后端接口 → 返回标准数据 → 表格刷新
(点击搜索) (where参数) (JSON格式)
通过本文的实战案例和最新优化方案,相信您已经掌握了Layui Table从查询到展示的完整流程,立即动手实践,构建高效的数据管理界面吧! 💪
本文由 业务大全 于2025-08-26发表在【云服务器提供商】,文中图片由(业务大全)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://vds.7tqx.com/wenda/732724.html
发表评论