Layui前后臺交互數(shù)據(jù)獲取java實例
Layui簡介
Layui是一款適用于后臺程序員的UI框架,學(xué)習(xí)成本低。Json數(shù)據(jù)格式交互前后臺,并且也相當(dāng)適用單頁面開發(fā)。有興趣的朋友可以看看layui官網(wǎng)。
Layui前后臺數(shù)據(jù)交互
layui有自己的一套特定的數(shù)據(jù)格式交互(這很重要),必須參數(shù)code:0,msg:“”,count:數(shù)據(jù)size(int),data:”數(shù)據(jù)List”。一般我們選擇封裝返回接收類。
Layui前臺js請求數(shù)據(jù)
其中 html代碼
<link rel="stylesheet" href="static/layui/css/layui.css" rel="external nofollow" media="all" /> <script type="text/javascript" src="static/layui/layui.js"></script> <table class="layui-hide" id="test" lay-filter="table"></table>
js代碼
layui.use(['form','layer','table'], function(){ var table = layui.table ,form = layui.form,$=layui.$; table.render({ elem: '#test' //綁定table id ,url:'sys/menu/list' //數(shù)據(jù)請求路徑 ,cellMinWidth: 80 ,cols: [[ {type:'numbers'} ,{field:'name', title:'菜單名稱'} ,{field:'parentName', title:'父菜單名稱',width:150} ,{field:'url', title: '菜單路徑'} ,{field:'perms', title: '菜單權(quán)限'} ,{field:'type', title:'類型'} ,{field:'icon', title:'圖標(biāo)'} ,{field:'orderNum', title:'排序'} ,{fixed: 'right',title: '操作', width:180, align:'center', toolbar: '#toolBar'}//一個工具欄 具體請查看layui官網(wǎng) ]] ,page: true //開啟分頁 ,limit:10 //默認十條數(shù)據(jù)一頁 ,limits:[10,20,30,50] //數(shù)據(jù)分頁條 ,id: 'testReload' }); });
java后臺代碼
@RequestMapping("/list") @ResponseBody @RequiresPermissions("sys:menu:list") public Layui list(@RequestParam Map<String, Object> params){ //查詢列表數(shù)據(jù) Query query = new Query(params); List<SysMenuEntity> 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()); }
Layui工具類代碼
public class Layui extends HashMap<String, Object> { 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; } }
PageUtils在這里可有可無,你們可以自行封裝
@Data public class PageUtils implements Serializable { private static final long serialVersionUID = -1202716581589799959L; //總記錄數(shù) private int totalCount; //每頁記錄數(shù) private int pageSize; //總頁數(shù) private int totalPage; //當(dāng)前頁數(shù) private int currPage; //列表數(shù)據(jù) private List<?> list; /** * 分頁 * @param list 列表數(shù)據(jù) * @param totalCount 總記錄數(shù) * @param pageSize 每頁記錄數(shù) * @param currPage 當(dāng)前頁數(shù) */ 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); } }
總之一句話,最后Layui接受到的數(shù)據(jù)格式要為。
以上這篇Layui前后臺交互數(shù)據(jù)獲取java實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot從Controller層進行單元測試的實現(xiàn)
這篇文章主要介紹了Spring Boot從Controller層進行單元測試的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04