Layui前后臺(tái)交互數(shù)據(jù)獲取java實(shí)例
Layui簡(jiǎn)介
Layui是一款適用于后臺(tái)程序員的UI框架,學(xué)習(xí)成本低。Json數(shù)據(jù)格式交互前后臺(tái),并且也相當(dāng)適用單頁面開發(fā)。有興趣的朋友可以看看layui官網(wǎng)。
Layui前后臺(tái)數(shù)據(jù)交互
layui有自己的一套特定的數(shù)據(jù)格式交互(這很重要),必須參數(shù)code:0,msg:“”,count:數(shù)據(jù)size(int),data:”數(shù)據(jù)List”。一般我們選擇封裝返回接收類。
Layui前臺(tái)js請(qǐng)求數(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ù)請(qǐng)求路徑 ,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'}//一個(gè)工具欄 具體請(qǐng)查看layui官網(wǎng) ]] ,page: true //開啟分頁 ,limit:10 //默認(rèn)十條數(shù)據(jù)一頁 ,limits:[10,20,30,50] //數(shù)據(jù)分頁條 ,id: 'testReload' }); });
java后臺(tái)代碼
@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前后臺(tái)交互數(shù)據(jù)獲取java實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot從Controller層進(jìn)行單元測(cè)試的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot從Controller層進(jìn)行單元測(cè)試的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04java 靜態(tài)鏈表實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了java 靜態(tài)鏈表實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06SpringCloud負(fù)載均衡實(shí)現(xiàn)定向路由詳情
這篇文章主要介紹了SpringCloud負(fù)載均衡實(shí)現(xiàn)定向路由詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08