Layui前后台交互数据获取java

摘要:
Layui简介Layui是一个面向后台程序员的UI框架,学习成本低。感兴趣的朋友可以查看layui的官方网站。Layui有自己的一套用于数据交互的特定数据格式。参数必须为code:0,msg:“”,count:数据大小,data:“数据列表”。Layui前台js请求数据,其中html代码123js代码Layui。使用12345678910111213141516171819202122232425java背景代码@RequestMapping@ResponseBody@RequiresPermissionspublicLayuilist{//查询列表数据Queryquery=newQuery;listmenuList=sysMenuService.queryList;inttotal=sysMenuService.queryTotal;PageUtilspageUtil=newPageUtils;returnLayui.data;}1234567891011 Layui工具类代码publicclassLayuiextendsHashMap<String,Object>{publicstaticLayuidata(整数计数,列表<?>数据){Layuir=newLayui();r.put;r.put,r.put;return r;}}1234567891011此处PageUtils是可选的,您可以封装@DatapublicclassPageUtilsimplementsSerializable{privatestaticfinalongserialVersionUID=-1202716581589799959L;//总记录privatetotalCount;//每页记录privateintpageSize;//总页数privateinttotalPage;//当前页面privateintcurrPage;//列出数据privateList<?>列表;/*分页*@paramlist列表数据*@paramtotalCount总记录*@paramageSize每页记录*@pparamcurrPage当前页面*/publicPageUtils(列表<?

Layui简介

Layui是一款适用于后台程序员的UI框架,学习成本低。Json数据格式交互前后台,并且也相当适用单页面开发。有兴趣的朋友可以看看layui官网

Layui前后台数据交互

layui有自己的一套特定的数据格式交互(这很重要),必须参数code:0,msg:“”,count:数据size(int),data:”数据List”。一般我们选择封装返回接收类。 
Layui前台js请求数据 
其中 html代码

<link rel="stylesheet" href="static/layui/css/layui.css" media="all" />
<script type="text/javascript" src="static/layui/layui.js"></script>
<table class="layui-hide" id="test" lay-filter="table"></table>
  • 1
  • 2
  • 3

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'  //数据请求路径
            ,cellMinWidth: 80
            ,cols: [[
              {type:'numbers'}
              ,{field:'name', title:'菜单名称'}
              ,{field:'parentName', title:'父菜单名称',150}
              ,{field:'url', title: '菜单路径'}
              ,{field:'perms', title: '菜单权限'}
              ,{field:'type', title:'类型'}
              ,{field:'icon', title:'图标'}
              ,{field:'orderNum', title:'排序'}
              ,{fixed: 'right',title: '操作', 180,      align:'center', toolbar: '#toolBar'}//一个工具栏  具体请查看layui官网
            ]]
            ,page: true   //开启分页
            ,limit:10   //默认十条数据一页
            ,limits:[10,20,30,50]  //数据分页条
            ,id: 'testReload'  
          });
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

java后台代码

    @RequestMapping("/list")
        @ResponseBody
        @RequiresPermissions("sys:menu:list")
        public Layui list(@RequestParam Map<String, Object> params){
            //查询列表数据
            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());
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

PageUtils在这里可有可无,你们可以自行封装

@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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

总之一句话,最后Layui接受到的数据格式要为 
这里写图片描述

转自https://blog.csdn.net/qq_26118603/article/details/78944591

免责声明:文章转载自《Layui前后台交互数据获取java》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇LZOthinkphp6自定义指令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

H5手机开发锁定表头和首列(惯性滚动)解决方案

前端时间移动端在做表格的时候需要这个功能,由于还有实现类似原生的惯性滚动功能,于是使用了iscroll插件。 iscroll插件下载地址:iscroll5 该功能demo github地址:https://github.com/lyc152/front-special-effects/tree/master/table-fixed 下面看下代码结构: HT...

CentOS7 复制文件夹和移动文件夹

CentOS7 复制文件夹和移动文件夹 linux下文件的复制、移动与删除命令为:cp,mv,rm一、文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 source3 ... directory 参数说明:-a:是指archive的...

html使用代码大全

<DIV style="FONT-SIZE: 9pt">1)贴图:<img src="http://t.zoukankan.com/图片地址">1)首行缩进2格:<p style="TEXT-INDENT: 2em">html使用代码大全</p> 2)加入连接:<a href="http://t.zou...

高德地图的标记使用

<template>   <ui-container v-loading="sysLoading" :element-loading-text="syncLoadingText">     <ui-main>       <div class="row-wrap">         <ui-row :g...

DB2查询前100到后200之间的数据

select T.sicCd form Table T where T.sicCd not in ( select sicCd form Table fetch first 100 rows only ) fetch first 100 rows only...

Logical Databases逻辑数据库

主要组成部分... 300 结构(Structure)... 301 选择(Selections)... 302 数据库程序(Database program)... 305 LDB程序结构... 307 FORM PUT_XXX性能问题... 309 GET_EVENT内表... 309 报表程序的LDB事件... 310 GET事件... 310 中...