How to connect layui and backend

Release: 2019-07-30 14:20:44
Original
14404 people have browsed it

How to connect layui and backend

Layui front and backend data interaction:

layui has its own set of specific data format interactions (this is very important), and the code must be parameter :0, msg: "", count: data size (int), data: "Data List". Generally we choose to encapsulate and return the receiving class.
Layui frontend js request data
html code

  
Copy after login

js code

layui.use(['form','layer','table'], function(){ var table = layui.table ,form = layui.form,$=layui.$; table.render({ elem: '#test' //绑定table id ,url:'sys/menu/list' //数据请求路径 ,cellMinWidth: 80 ,cols: [[ {type:'numbers'} ,{field:'name', title:'菜单名称'} ,{field:'parentName', title:'父菜单名称',width:150} ,{field:'url', title: '菜单路径'} ,{field:'perms', title: '菜单权限'} ,{field:'type', title:'类型'} ,{field:'icon', title:'图标'} ,{field:'orderNum', title:'排序'} ,{fixed: 'right',title: '操作', width:180, align:'center', toolbar: '#toolBar'}//一个工具栏 具体请查看layui官网 ]] ,page: true //开启分页 ,limit:10 //默认十条数据一页 ,limits:[10,20,30,50] //数据分页条 ,id: 'testReload' }); });
Copy after login

java backend code

@RequestMapping("/list") @ResponseBody @RequiresPermissions("sys:menu:list") public Layui list(@RequestParam Map params){ //查询列表数据 Query query = new Query(params); List menuList = sysMenuService.queryList(query); int total = sysMenuService.queryTotal(query); PageUtils pageUtil = new PageUtils(menuList, total, query.getLimit(), query.getPage()); return Layui.data(pageUtil.getTotalCount(), pageUtil.getList()); }
Copy after login

Layui tool class code

public class Layui extends HashMap { public static Layui data(Integer count,List data){ Layui r = new Layui(); r.put("code", 0); r.put("msg", ""); r.put("count", count); r.put("data", data); return r; } }
Copy after login

PageUtils is optional here, you can encapsulate it yourself

@Data public class PageUtils implements Serializable { private static final long serialVersionUID = -1202716581589799959L; //总记录数 private int totalCount; //每页记录数 private int pageSize; //总页数 private int totalPage; //当前页数 private int currPage; //列表数据 private List list; /** * 分页 * @param list 列表数据 * @param totalCount 总记录数 * @param pageSize 每页记录数 * @param currPage 当前页数 */ public PageUtils(List list, int totalCount, int pageSize, int currPage) { this.list = list; this.totalCount = totalCount; this.pageSize = pageSize; this.currPage = currPage; this.totalPage = (int)Math.ceil((double)totalCount/pageSize); } }
Copy after login

Recommendation:layui framework tutorial

The above is the detailed content of How to connect layui and backend. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!