Thymeleaf + Layui+快速分页模板(含前后端代码)

news2024/10/4 6:40:18

发现很多模块写法逻辑太多重复的,因此把分页方法抽取出来记录以下,以后想写分页直接拿来用即可:

1. 首先是queryQrEx.html:

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="/layui/css/layui.css">
    <title>数据查询</title>
</head>
<body>
<h1 style="text-align: center;">数据查询</h1>
<div class="layui-form layui-form-pane" style="margin:0" lay-filter="qrFormFilter"> <!--必须在表单中添加layui-form,否则提交的时候无法获取到任何值-->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label" style="width:200px">Tray No \ Box No \ 卡板No</label>
            <div class="layui-input-block" style="margin-left: 200px;">
                <input name="trayNo"  class="layui-input" th:value="${trayNo}">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">Tray日期</label>
            <div class="layui-input-inline"  style="width: 180px;">
                <input id="createTime" name="createTime" class="layui-input" />
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" id="searchBtn" lay-submit lay-filter="data-search-btn"><i class="layui-icon layui-icon-search">查询</i></button>
        </div>
    </div>
</div>
<script type="text/html" id="toolbarDemo"></script>
<table id="qrexTable"></table>
<script type="text/html" id="page-pagebar"><!--分页插件 + 导出全部-->
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-hei layui-btn-sm " lay-event="export" ><i class="layui-icon layui-icon-export">导出全部</i></button>
    </div>
</script>

<script src="/layui/layui.js"></script>
<script th:inline="javascript">
    layui.use(['layer', 'form','table'], function() {
        var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer,
        laydate = layui.laydate;

        laydate.render({
            elem: '#createTime'
            ,range: '~'
            ,value: [[${createTime}]] //初始化日期
            ,max:0 //最大日期只能选择当天
            ,rangeLinked: true // 开启日期范围选择时的区间联动标注模式 ---  layui2.8+ 新增
        });

      table.render({
            elem: '#qrexTable',
            url: '/getQrExcList',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            where: {createTime: [[${createTime}]] },
            pagebar: '#page-pagebar', // 表格中需要指定分页栏,否则会自动使用默认的分页模块
            cols: [
                [
                    {type:'numbers', title: '序号', width: 60, fixed:'left'},
			        { field: 'trayNo', width: 210, title: 'trayNo'},
			        { field: 'leadid', width:130, title: '用户' , sort: true},
			        { field: 'createTime', width:155, sort: true, title: '创建时间' , templet:function(d){return layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm')}}
                ]
            ],
                page: true,
                limit: 20,
                limits: [20, 50, 100],
        });

         //搜索
	    form.on('submit(data-search-btn)', function (data) {
             table.reload('qrexTable',{
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始,如果当前是第10页,再次根据其他条件查询可能无法查询到第10页的数据,翻页时并不会触发跳到第1页
                }
            });
        });

         // 底部分页栏事件
       table.on('pagebar(qrexTable)', function(obj){
          var eventValue = obj.event; // 获得按钮 lay-event 值
          if(eventValue == 'export'){
                 $.get('/getQrExcList', form.val('qrFormFilter'), function(res) {
                    if(res.code==0){
                        table.exportFile('qrexTable', res.data, 'xls'); //表名 数据  格式
                    }else{
                        layer.msg(res.msg, {icon: 5});
                    }
                })
          }
        });


    });
</script>
</body>
</html>

2. 1 分页类:

//首先写个分页参数类
public class Page {   //所有类可以继承整个类

    @TableField(exist=false)
    private int page=1;  //第几页,默认第一页,否则如果没传值默认会变成0 ,(0-1)*20会变成-20导致查询报错
    @TableField(exist=false)
    private Integer limit;//每页多少条
}

2.2 统一返回值类(统一返回值有助于编写操作日志时返回接口的返回值,且layui表格的返回值也是这个类的格式)

package com.epson.entity;
import lombok.Data;

import java.util.List;

/**
 * @author hewenjun
 * @version 1.0
 * @date 2022/06/23 16:08
 */
@Data
public class ReturnT {
    private Integer code; //默认值时0,因为Layui取table值时返回code不是0就会报错格式不正确
    private String msg;
    private Object data;
    private Integer count;
    private List result;

    public ReturnT(Integer code, String msg, Integer count, List result) { //返回code,msg,分页数据总条数, 分页数据
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = result;
    }
    public ReturnT(Integer code) {
        this.code = code;
    }
    public ReturnT(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ReturnT(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ReturnT error(){ return  new ReturnT(400); }
    public static ReturnT error(int code){ return  new ReturnT(code); }
    public static ReturnT error(String msg){
        return  new ReturnT(400, msg);
    }
    public static ReturnT error(int code, String msg){
        return  new ReturnT(code, msg, msg);
    }
    public static ReturnT error(String msg, Object data){
        return  new ReturnT(0, msg, data);
    }

    public static ReturnT success(int code){ return  new ReturnT(code);  }
    public static ReturnT success(String msg){ return  new ReturnT(0, msg);  }
    public static ReturnT success(String msg, Object data){  return  new ReturnT(0, msg, data);  }
    public static ReturnT success(int code, String msg){  return  new ReturnT(code, msg);  }

}

2.3 实体类

@Data
public class Qr extends Page implements Serializable {
	private String leadid;
	private String createTime;

    private String trayNo;
    private String trayDate;//Tray日期
}

3. Controller

@Resource
private QrService qrService;

@RequestMapping("/toQrEx") //跳转到demoHtm页面
    public ModelAndView toQrEx(Qr qrf, HttpServletRequest request){
        request.setAttribute("createTime", addDate(-1) + " ~ " + addDate(0)); //tray日期初始化
            return new ModelAndView("queryQrEx");
        
    }

 @GetMapping("/getQrExcList") //----异常数据查询
    ReturnT getQrExcList(Qr qrf) {
        return qrService.getQrExcList(qrf);
}




	/**
	 * 功能:指定日期加上指定天数
	 *
	 * @param date
	 *            日期
	 * @param day
	 *            天数
	 * @return 返回相加后的日期
	 */
public static Date addDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
}

//获取服务器当前时间 + 天数   ---》如果是当前日期之前的某天那就传 负数 例如前一天 -1
public static String addDate(int days) {
		Date d =  addDate(new Date(), days);
		DateFormat df = new SimpleDateFormat(STR_YYYY_MM_DD);
		return df.format(d.getTime());
}

4. serviceImpl

public ReturnT getQrExcList(Qr ex) {
        if(null != ex.getLimit()){ //如果页面没有传limit那就不是表格查询,应该是导出所有,这时就不需要分页
            int currentPage = ex.getPage(); //如果是从数据页面点击异常数量跳转的page=0,因此需要赋值1,否则limit -20会报错
            ex.setPage((currentPage - 1) * ex.getLimit());
        }

        List<Qr> list = qrMapper.getQrExcList(ex);
        int count = qrMapper.getQrExcListCount(ex);
        return !list.isEmpty() ? new ReturnT(0, "获取成功", count, list) : ReturnT.error("无数据");
    }

5. mapper

 String PUBLIC_IF=  "<if test='trayNo !=null and trayNo != \"\"'> and tcpno = #{trayNo} </if>" +
            " <if test='createTime !=null and createTime != \"\" '>"
            + "    AND to_char(createTime,'YYYY-MM-DD') &gt;= substring(#{createTime}, 0, 11)" //数据库中的时间10位之前即可,前台传的有空格所以是11
            + "    AND to_char(createTime,'YYYY-MM-DD') &lt;= substring(#{createTime}, 14)"
            + " </if>" 
		   +	 " <if test='limit !=null'>  limit #{limit} offset #{page} </if>";
    @Select({"<script>select trayNo,leadid,createTime from tableName where 1=1 " + PUBLIC_IF + "</script>"})
    List<Qr> getQrExcList(Qr ex);

    @Select({"<script> SELECT COUNT(*) from tableName where 1=1  " + PUBLIC_IF  + " </script>" })
    int getQrExcListCount(Qr qr);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/769637.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

专业信用修复!揭阳市企业信用修复办法,企业修复好处及失信危害,申请条件

本文小编将为大家介绍2023年揭阳市企业信用修复办法指导及信用修复意义、好处等内容&#xff0c;详情如下&#xff0c;如果有广州市、深圳市、江门市、佛山市、汕头市、湛江市、韶关市、中山市、珠海市、茂名市、肇庆市、阳江市、惠州市、潮州市、揭阳市、清远市、河源市、东莞…

Nautlius Chain主网正式上线,模块Layer3时代正式开启

Nautilus Chain是在Vitalik Buterin提出Layer3理念后&#xff0c; 对Layer3领域的全新探索。作为行业内首个模块化Layer3链&#xff0c;我们正在对Layer3架构进行早期的定义&#xff0c;并有望进一步打破公链赛道未来长期的发展格局。 在今年年初&#xff0c;经过我们一系列紧张…

RTD2555T RTD2556T(Typec) eDP屏显示介绍

RTD2555T是新一代HDMI2DP转eDP的IC&#xff0c;主要应该用Typec便携式显示器驱动芯片&#xff0c;搭配LDR6282等PD IC实现2个Typec口正反插&#xff0c;充电等&#xff0c;支持按键菜单操作&#xff0c;支持串口通信控制等功能定制。

【stable diffusion】保姆级入门课程01-Stable diffusion(SD)文生图究竟是怎么一回事

目录 学前视频 0.本章素材 1.什么是文生图 2.界面介绍 2.1切换模型的地方 2.2切换VAE 2.3功能栏 2.4提示词 1.提示词的词性 2.提示词的语法 3.提示词的组成 4.提示词的权重调整 2.5参数调整栏 1.采样方法 2.采样迭代步数 3.面部修复 4.平铺图 5.高清修复 6.…

数据中心机房建设,务必确定这13个关键点

下午好&#xff0c;我的网工朋友。 关于机房、机架的相关内容&#xff0c;给你们说了不少。 今天再给你补充个知识点&#xff0c;机房建设&#xff0c;要怎么做。 熟悉机房建设的网工朋友可能都知道&#xff0c;一个全面的数据中心机房建设工程一般包括&#xff1a; 综合布…

多线程——互斥和同步

多线程—互斥和同步 文章目录 多线程—互斥和同步多线程互斥互斥量mutex互斥量的接口初始化互斥量静态分配动态分配&#xff1a;pthread_mutex_init初始化互斥量 销毁互斥量int pthread_mutex_destroy销毁互斥量 互斥量加锁和解锁pthread_mutex_lock加锁pthread_mutex_trylock非…

IPv4 与 IPv6:网络性能和带宽的比较

网络连接已经成为我们生活中不可或缺的一部分&#xff0c;而IP地址是网络连接中最基本和最重要的部分。IPv4和IPv6是两种常用的IP地址协议&#xff0c;它们之间有着很大的差异。 首先&#xff0c;让我们了解一下IPv4和IPv6的基本概念。IPv4是互联网上使用最广泛的IP地址协议&am…

(栈队列堆) 剑指 Offer 30. 包含min函数的栈 ——【Leetcode每日一题】

❓ 剑指 Offer 30. 包含min函数的栈 难度&#xff1a;简单 定义栈的数据结构&#xff0c;请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中&#xff0c;调用 min、push 及 pop 的时间复杂度都是 O ( 1 ) O(1) O(1)。 示例: MinStack minStack new MinStack()…

Hadoop——HDFS的Java API操作(文件上传、下载、删除等)

1、创建Maven项目 2、修改pom.xml文件 <dependencies><!-- Hadoop所需依赖包 --><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.7.0</version></dependency&…

linux之Ubuntu系列(四)用户管理 用户和权限 chmod 超级用户root, R、W、X、T、S

r(Read&#xff0c;读取)&#xff1a;对文件而言&#xff0c;具有读取文件内容的权限&#xff1b;对目录来说&#xff0c;具有浏览目 录的权限。 w(Write,写入)&#xff1a;对文件而言&#xff0c;具有新增、修改文件内容的权限&#xff1b;对目录来说&#xff0c;具有删除、移…

DNS基础知识(前端工程师必备知识)

DNS 在工作中&#xff0c;经常切换本地和服务器&#xff0c;添加各种解析。遇到DNS引起的一些问题。发现网上资料很混乱&#xff0c;用心整理写了下&#xff0c;希望对大家有帮助。 DNS&#xff08;Domain Name System&#xff0c;域名系统&#xff09;&#xff0c;最初&…

ipad可以使用其他品牌的手写笔吗?平价ipad手写笔推荐

我是一个拥有多年数码经验的爱好者&#xff0c;我知道一些关于电容笔的知识。我认为&#xff0c;苹果原装的电容笔与普通的电容笔最大的不同之处&#xff0c;就是其所带来的压感不同。由于“重力压感”的特殊性&#xff0c;我们能很快地把色彩填充到画面中。除此之外&#xff0…

tql!红队批量脆弱点信息搜集工具

功能如下 指纹识别:调用“三米前有香蕉皮“前辈工具&#xff0c;他的工具比finger好用 寻找资产中404&#xff0c;403&#xff0c;以及网页中存在的其他薄弱点&#xff0c;以及需要特定路径访问的资产 后续会把nuclei加进来 目前只有windows可以用 关注【Hack分享吧】公众号&a…

6款好用的在线原型图设计工具推荐

在线原型图的核心功能是可视化需求&#xff0c;因此一个易于使用的在线原型图工具对原型图设计至关重要。对于熟悉的Photoshop和iIlustrator来说&#xff0c;虽然它们功能强大&#xff0c;但界面太复杂&#xff0c;初学者很难快速启动&#xff0c;面对批量调整的在线原型图&…

模型驱动的深度学习方法

本篇文章是博主在人工智能等领域学习时&#xff0c;用于个人学习、研究或者欣赏使用&#xff0c;并基于博主对人工智能等领域的一些理解而记录的学习摘录和笔记&#xff0c;若有不当和侵权之处&#xff0c;指出后将会立即改正&#xff0c;还望谅解。文章分类在学习摘录和笔记专…

MySQL高阶语句之一

目录 模版表 一、ORDER BY语法 1.1语法 1.2升序 1.3降序 1.4多高字段排序 二、AND/OR--且/或 2.1AND 2.2OR 2.3嵌套/多条件 三、distinct 查询不重复记录 3.1语法 四、GROUP BY语法 4.1语法 五、限制结果条目&#xff08;limit&#xff09; 5.1语法 5.2实验 五、设…

mac怎么转换音频格式为mp3

mac怎么转换音频格式为mp3&#xff1f;熟悉音频文件的人都知道&#xff0c;音频格式有许多不同种类。尽管有多种音频格式&#xff0c;但最受欢迎且最通用的是MP3格式。无论在哪个播放器上&#xff0c;MP3格式的音频都能轻松打开。而其他格式可能会出现不兼容无法打开的情况。因…

高斯误差线性单元激活ReLU以外的神经网络

高斯误差线性单位&#xff08;GELU&#xff09;激活函数由加州大学伯克利分校的Dan Hendrycks和芝加哥丰田技术研究所的Kevin Gimpel于2018年引入。激活函数是触发神经元输出的“开关”&#xff0c;随着网络的深入&#xff0c;其重要性也随之增加。最近几周&#xff0c;机器学习…

如何在Microsoft Excel中使用SORT函数

虽然 Microsoft Excel 提供了一个内置的数据排序工具,但你可能更喜欢函数和公式的灵活性。 使用 SORT 函数的好处是,你可以在不同的位置对数据进行排序。如果你想在不干扰原始数据集的情况下操作项目,你会喜欢 Excel 中的 SORT 函数。但是,如果你喜欢对项目进行原位排序,…