java+springBoot+Thymeleaf+vue分页组件的定义
导读
本篇着重介绍java开发环境下,如何写一个vue分页组件,使用到的技术点有java、springBoot、Thymeleaf等;
分页效果图
名称为vuepagerbasic的分页组件,只包含上一页、下一页,本篇着重介绍vuepager分页组件
前台html,使用到Thymeleaf模板
在layout.html文件里定义与注册分页组件
<div th:fragment="vuepagerbasic">
<template id="vuePagerBasic">
<div>
<a href="javascript:;" class="bd0" v-on:click="go(1)" v-if="pages.pageIndex>1"><<</a>
<a href="javascript:;" v-on:click="go(pages.pageIndex-1)" v-if="pages.pageIndex>1"><</a>
<span v-if="pages.previousSpot" v-on:click="go(pages.firstNum-1)">...</span>
<a href="javascript:;" v-for="item in pages.pageList" v-bind:class="{cur:pages.pageIndex==item}"
v-on:click="go(item)">{{item}}</a>
<span v-if="pages.nextSpot" v-on:click="go(pages.lastNum+1)">...</span>
<a href="javascript:;" v-if="pages.pageIndex < pages.totalPages" v-on:click="go(pages.pageIndex+1)">></a>
<a href="javascript:;" class="bd0" v-if="pages.pageIndex < pages.totalPages"
v-on:click="go(pages.totalPages)">>></a>
</div>
</template>
<script type="text/javascript">
Vue.component('vuepagerbasic', {
template: '#vuePagerBasic',
props: ['url', 'prop'],
data: function () {
return {
pages: [],
}
},
mounted: function () {
this.go(1);
},
methods: {
go: function (n) {
this.getData(n);
},
getData: function (n) {
this.prop = this.prop || {};
this.prop.pageIndex = n;
var me = this;
axios.post(this.url, this.prop).then(function (res) {
me.pages = res.data.pages;
console.log("me.pages:" + me.pages);
me.$emit("getdata", res.data.rows);
});
}
}
});
</script>
</div> <div th:fragment="vuepager">
<template id="vuePager">
<div class="page tc">
<a href="javascript:;" v-on:click="go(1)" v-if="pages.pageIndex>1">首页</a>
<a href="javascript:;" v-on:click="go(pages.pageIndex-1)" v-if="pages.pageIndex>1">上一页</a>
<span v-if="pages.previousSpot" v-on:click="go(pages.firstNum-1)">...</span>
<span href="javascript:;" v-for="item in pages.pageList" v-bind:class="{current:pages.pageIndex==item}"
v-on:click="go(item)">{{item}}</span>
<span v-if="pages.nextSpot" v-on:click="go(pages.lastNum+1)">...</span>
<a href="javascript:;" v-if="pages.pageIndex < pages.totalPages" v-on:click="go(pages.pageIndex+1)">下一页</a>
<a href="javascript:;" v-if="pages.pageIndex < pages.totalPages" v-on:click="go(pages.totalPages)">尾页</a>
</div>
</template>
<script>
Vue.component('vuepager', {
template: '#vuePager',
props: ['url', 'prop'],
data: function () {
return {
pages: [],
}
},
mounted: function () {
this.go(1);
},
methods: {
go: function (n) {
this.getData(n);
},
getData: function (n) {
this.prop = this.prop || {};
this.prop.pageIndex = n;
var me = this;
axios.post(this.url, this.prop).then(function (res) {
me.pages = res.data.pages;
me.$emit("getdata", res.data.rows);
});
}
}
});
</script> </div>
后台PagedHelper.java
用于计算分页等,暂未优化;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; public class PagedHelper extends LinkedHashMap<String, Object> implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L; public boolean isPreviousSpot() {
return previousSpot;
} public void setPreviousSpot(boolean previousSpot) {
this.previousSpot = previousSpot;
} public boolean isNextSpot() {
return nextSpot;
} public void setNextSpot(boolean nextSpot) {
this.nextSpot = nextSpot;
} public int getFirstNum() {
return firstNum;
} public void setFirstNum(int firstNum) {
this.firstNum = firstNum;
} public int getLastNum() {
return lastNum;
} public void setLastNum(int lastNum) {
this.lastNum = lastNum;
} public int getShowPageNum() {
return showPageNum;
} public void setShowPageNum(int showPageNum) {
this.showPageNum = showPageNum;
} public long getTotal() {
return total;
} public void setTotal(long total) {
this.total = total;
} public int getTotalPages() {
this.totalPages=(int)Math.ceil((double)total / pageSize);
return totalPages;
} public int getPageIndex() {
return pageIndex;
} public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} /*
* public List<Integer> getPageList() { return pageList; }
*/ public void setPageList(List<Integer> pageList) {
this.pageList = pageList;
} // 前面的点,前面省略的页数用.来代表,
private boolean previousSpot;
// 后面的点,后面省略的页数用.来代表,
private boolean nextSpot;
// 第一个页数,一般都是1
private int firstNum;
// 最后一个页数,也就是最大页数
private int lastNum;
// 默认页面显示最多页号数目
private int showPageNum;
// 总记录数
private long total;
// 总页数
public int totalPages; // 当前页
private int pageIndex;
// 每页大小
private int pageSize; // 页数列表,此列表中包含第一页和最后一页
private List<Integer> pageList ; //返回的数据记录
private List<?> rows; public List<Integer> getPagelist()
{
List<Integer> p = new ArrayList<Integer>();
if (totalPages <= showPageNum)//全部显示
{
for (int i = 1; i <= totalPages; i++)
{
p.add(i);
}
}
else
{
int halfOfPageNum = ((int)((showPageNum + 1) / 2)) - 1;//前后保留页数大小
if (pageIndex - halfOfPageNum > 1 && pageIndex + halfOfPageNum < totalPages)
{
//两头都可取值
this.previousSpot = this.nextSpot = true;
for (int i = pageIndex - halfOfPageNum; i <= pageIndex + halfOfPageNum; i++)
{
p.add(i);
}
}
else if (pageIndex - halfOfPageNum > 1)
{
//右头值少
this.previousSpot = true;
for (int i = totalPages ; i >= totalPages - showPageNum + 1; i--)
{
p.add(i);
}
}
else if (pageIndex - halfOfPageNum <= 1)
{
//左头值少
this.nextSpot = true;
for (int i = 1; i <= showPageNum; i++)
{
p.add(i);
}
}
this.firstNum = pageIndex - halfOfPageNum <= 0 ? 1 : pageIndex - halfOfPageNum ;
this.lastNum = firstNum + showPageNum - 1;
}
p.sort(Comparator.naturalOrder());
this.pageList=p;
return this.pageList;
} public List<?> getRows() {
return rows;
} public void setRows(List<?> rows) {
this.rows = rows;
} public PagedHelper(Map<String, Object> params )
{
this.putAll(params);
// 分页参数
if(params.get("pageIndex") != null && params.get("pageSize") != null ){
this.pageIndex=Integer.parseInt(params.get("pageIndex").toString());
this.pageSize=Integer.parseInt(params.get("pageSize").toString());
if(this.pageIndex<=0) {
this.pageIndex=1;
this.put("pageIndex",1);
}
if(this.pageSize<=0) {
this.pageSize=10;
this.put("pageSize",10);
}
this.put("offset",(this.pageIndex-1)*this.pageSize);
this.put("limit",this.pageSize);
}
else { this.put("offset", params.get("offset") != null ? Integer.parseInt(params.get("offset").toString()) : 0);
this.put("limit", params.get("limit") != null ? Integer.parseInt(params.get("limit").toString()) : 10);
this.pageIndex=(Integer.parseInt(this.get("offset").toString())-1)/Integer.parseInt(this.get("limit").toString())+1;
this.pageSize=Integer.parseInt(this.get("limit").toString());
this.put("pageIndex", this.pageIndex);
this.put("pageSize", this.pageSize);
} this.firstNum=1;
if(params.get("showPageNum")==null)
{
this.showPageNum=8;
}
else {
this.showPageNum=Integer.parseInt(params.get("showPageNum").toString());
}
if(params.get("total")!=null)
{
this.total=Integer.parseInt(params.get("total").toString());
}
this.totalPages=this.getTotalPages();
this.lastNum = this.totalPages;
this.pageList = this.getPagelist(); this.put("firstNum", this.firstNum);
this.put("lastNum", this.lastNum);
this.put("pageList", this.pageList);
this.put("showPageNum", this.showPageNum);
this.put("previousSpot", this.previousSpot);
this.put("nextSpot", this.nextSpot);
this.put("totalPages",this.totalPages); } public PagedHelper(Map<String, Object> params,int total )
{
this.putAll(params);
this.put("total", total);
// 分页参数
if(params.get("pageIndex") != null && params.get("pageSize") != null ){
this.pageIndex=Integer.parseInt(params.get("pageIndex").toString());
this.pageSize=Integer.parseInt(params.get("pageSize").toString());
if(this.pageIndex<=0) {
this.pageIndex=1;
}
if(this.pageSize<=0) {
this.pageSize=10;
}
this.put("offset",(this.pageIndex-1)*this.pageSize);
this.put("limit",this.pageSize);
}
else { this.put("offset", params.get("offset") != null ? Integer.parseInt(params.get("offset").toString()) : 0);
this.put("limit", params.get("limit") != null ? Integer.parseInt(params.get("limit").toString()) : 10);
this.pageIndex=Integer.parseInt(this.get("offset").toString())/Integer.parseInt(this.get("limit").toString())+1;
this.pageSize=Integer.parseInt(this.get("limit").toString());
}
this.put("pageIndex", this.pageIndex);
this.put("pageSize", this.pageSize);
this.firstNum=1;
if(params.get("showPageNum")==null)
{
this.showPageNum=8;
}
if(params.get("total")!=null)
{
this.total=Integer.parseInt(params.get("total").toString());
}
if(total>=0)
{
this.total=total;
}
this.totalPages=this.getTotalPages();
this.lastNum = this.totalPages;
this.pageList = this.getPagelist(); this.put("firstNum", this.firstNum);
this.put("lastNum", this.lastNum);
this.put("pageList", this.pageList);
this.put("showPageNum", this.showPageNum);
}
}
后台查询分页数据的方法
@ResponseBody
@PostMapping("/getListVue")
public PageUtils getListVue(@RequestBody Map<String, Object> params) {
// @RequestBody接收axios传来的参数 ……
int total = orgCollectionService.getListCount(params);
List<TOrgCollectionDO> tOrgList = orgCollectionService.getList(params); params.put("total", total);
PagedHelper pagedHelper = new PagedHelper(params);
PageUtils pageUtils = new PageUtils(tOrgList, total, pagedHelper);//关于total处理暂时未优化
return pageUtils; }
import java.io.Serializable;
import java.util.List; public class PageUtils implements Serializable {
private static final long serialVersionUID = 1L;
private int total;
private List<?> rows;
private PagedHelper pages;
public PageUtils(List<?> list, int total) {
this.rows = list;
this.total = total;
}
public PageUtils(List<?> list, int total,PagedHelper pages) {
this.rows = list;
this.total = total;
this.pages=pages;
}
public int getTotal() {
return total;
} public void setTotal(int total) {
this.total = total;
} public List<?> getRows() {
return rows;
} public void setRows(List<?> rows) {
this.rows = rows;
} public PagedHelper getPages() {
return pages;
} public void setPages(PagedHelper pages) {
this.pages = pages;
} }
PageUtils 用于返回数据给前台;
demo.html使用vue分页组件vuepager
<table border="0" cellspacing="0" cellpadding="0" id="app">
<tr>
<th>序号</th>
<th><div>名称</div></th>
<th width="92">类型</th>
<th width="92">分类</th>
<th>状态</th>
<th>显示</th>
<th>更新时间</th>
<th class="last">操作</th> </tr>
<tr v-for="(row,index) in list" :data-id="row.id">
<td>{{index+1}}</td>
<!-- 其他列表项略 -->
</tr>
<tr v-if="list.length<=0">
<td colspan="8">暂无数据
<td>
</tr>
</table>
<div class="page">
<vuepager v-on:getdata="getData" url="/works/getListVue" v-bind:prop="prop" ref="myref"></vuepager>
</div>
<div th:include="layout :: vuepager"></div> <!-- 引入vuepager的themeleaf模板 -->
注:url属性为请求分页数据的地址,查询参数在prop里,通过axios发出请求,axios.post(this.url, this.prop)。
<script>
Vue.component('vuepager', {
template: '#vuePager',
props: ['url', 'prop'],
data: function () {
return {
pages: [],
}
},
mounted: function () {
this.go(1);
},
methods: {
go: function (n) {
this.getData(n);
},
getData: function (n) {
this.prop = this.prop || {};
this.prop.pageIndex = n;
var me = this;
axios.post(this.url, this.prop).then(function (res) {
me.pages = res.data.pages;
me.$emit("getdata", res.data.rows);
});
}
}
});
</script>
demo.html文件里javascript代码请求分页数据
<script>
var app = new Vue({
el : '#app',
data : {
list : [],
orgcateid : -1,
mediatype : '',
checkstatus : -1,
publishstatus : -1,
statuscode : -1,
title : '',
pageIndex : 1
},
filters : {
cutStr : function(str, maxLength) {
if (str == undefined || str == "") {
return "";
}
if (str.length > maxLength) {
return str.substr(0, maxLength - 1) + "...";
} else {
return str;
}
}
},
methods : {
getData : function(data) {
this.list = data;
}
},
computed : {
prop : function() {
return {
mediatype : this.mediatype,
orgcateid : this.orgcateid,
checkstatus : this.checkstatus,
publishstatus : this.publishstatus,
title : this.title,
sort : 'updatetime',
order : 'desc',
pageSize : 15,
showPageNum : 4
}
}
}
});
</script>
总结
本篇主要介绍了vue分页组件的定义及与后台分页数据的交互,涉及下拉列表框、文本框等搜索功能将单独介绍。
java+springBoot+Thymeleaf+vue分页组件的定义的更多相关文章
- 不要再学 JSP 了,学 SpringBoot + Thymeleaf + Vue吧
老读者就请肆无忌惮地点赞吧,微信搜索[沉默王二]关注这个在九朝古都洛阳苟且偷生的程序员.本文 GitHub github.com/itwanger 已收录,里面还有我精心为你准备的一线大厂面试题. 读 ...
- 基于 bootstrap 的 vue 分页组件
申手党点这里下载示例 基于 bootstrap 的 vue 分页组件,我想会有那么一部分同学,在使用Vue的时候不使用单文件组件,因为不架设 NodeJS 服务端.那么网上流传的 *.vue 的各种分 ...
- vue分页组件table-pagebar
之前一直接触都是原始的前端模型,jquery+bootstrap,冗杂的dom操作,繁琐的更新绑定.接触vue后,对前端MVVM框架有了全新的认识.本文是基于webpack+vue构建,由于之前的工作 ...
- SpringBoot + thymeleaf 实现分页
SpringBoot结合Thymeleaf实现分页,很方便. 效果如下 后台代码 项目结构 1. 数据库Config 由于hibernate自动建表字符集为latin不能插入中文,故需要在applic ...
- 打通前后端全栈开发node+vue进阶【课程学习系统项目实战详细讲解】(3):用户添加/修改/删除 vue表格组件 vue分页组件
第三章 建议学习时间8小时 总项目预计10章 学习方式:详细阅读,并手动实现相关代码(如果没有node和vue基础,请学习前面的vue和node基础博客[共10章] 演示地址:后台:demo ...
- vue学习—组件的定义注册
组件的定义注册 效果: 方法一: <div id="box"> <v-header></v-header> <hr /> <b ...
- vue分页组件重置到首页问题
分页组件,可以借用这个老哥的@暴脾气大大https://www.cnblogs.com/sebastian-tyd/p/7853188.html#4163272 但是有一个问题就是下面评论中@ Mrz ...
- VUE 分页组件
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- vue分页组件二次封装---每页请求特定数据
关键步骤: 1.传两个参数:pageCount (每页条数).pageIndex (页码数): 2.bind方法的调用 <!-- 这部分是分页 --> <div class=&quo ...
随机推荐
- spring源码深度解析— IOC 之 自定义标签解析
概述 之前我们已经介绍了spring中默认标签的解析,解析来我们将分析自定义标签的解析,我们先回顾下自定义标签解析所使用的方法,如下图所示: 我们看到自定义标签的解析是通过BeanDefinition ...
- python 基本数据类型之字符串功能
字符串常用功能: # name.upper() #全部大写变小写 # name.lower() #全部小写变大写 # name.split() #分割 # name.find() #找到指定子序列的索 ...
- MySQL metalock的一些技巧(写大于读的案例,以及获得锁的顺序)
前言:元数据锁不是锁定数据,而是锁定描述数据的元数据信息.就像很多装修工人(工作线程)在室内(对象上)装修(操作),不能有其他工人(线程)把屋子拆了(表删除了). MySQL 为了数据一致性使用元数据 ...
- 2018.11.2 2018NOIP冲刺之最短公共父串
很有意思的一个题 试题描述 给定字符串A和字符串B,要求找一个最短的字符串,使得字符串A和B均是它的子序列. 输入 输入包含两行,每行一个字符串,分别表示字符串A和字符串B.(串的长度不超过30) 输 ...
- ASP.NET、.NET和C#的关系是怎样的?
1..NET是什么?.Net全称.NET Framework是一个开发和运行环境,该战略是微软的一项全新创意,它将使得“互联网行业进入一个更先进的阶段”,.NET不是一种编程语言. 简单说就是一组类库 ...
- reportlab生成pdf
文档地址:https://www.reportlab.com/docs/reportlab-userguide.pdf 源码地址:https://bitbucket.org/rptlab/report ...
- Java volatile关键字小结
public class Test { public static void main(String[] args){ } } /* 12.3 Java内存模型 Java内存模型定义了线程与主内存之间 ...
- python 之 并发编程(守护进程、互斥锁、IPC通信机制)
9.5 守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就立即终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemonic process ...
- acm 模板
Index 分类细则 说起分类准则,我也是很头疼,毕竟对于很多算法,他并不是单调的,而是多方面的都挂得上钩.所以,从始至终,分类准则一直都是我很纠结的问题. 经过思量,首先分出比较主流的几类:Numb ...
- Excel催化剂开源第33波-Quick Bible For PPT插件项目全代码开源
很感恩,能够在上帝奇妙地带领下,经过多方的资源整合后,可以从我手中完成一款对教会内部制作PPT过程中,引用圣经的这个小环节能够发挥一些小小的作用的小插件.因制作本插件时,也大量用到VSTO开发的一些技 ...