导读

本篇着重介绍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">&lt;&lt;</a>
<a href="javascript:;" v-on:click="go(pages.pageIndex-1)" v-if="pages.pageIndex>1">&lt;</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)">&gt;</a>
<a href="javascript:;" class="bd0" v-if="pages.pageIndex < pages.totalPages"
v-on:click="go(pages.totalPages)">&gt;&gt;</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分页组件的定义的更多相关文章

  1. 不要再学 JSP 了,学 SpringBoot + Thymeleaf + Vue吧

    老读者就请肆无忌惮地点赞吧,微信搜索[沉默王二]关注这个在九朝古都洛阳苟且偷生的程序员.本文 GitHub github.com/itwanger 已收录,里面还有我精心为你准备的一线大厂面试题. 读 ...

  2. 基于 bootstrap 的 vue 分页组件

    申手党点这里下载示例 基于 bootstrap 的 vue 分页组件,我想会有那么一部分同学,在使用Vue的时候不使用单文件组件,因为不架设 NodeJS 服务端.那么网上流传的 *.vue 的各种分 ...

  3. vue分页组件table-pagebar

    之前一直接触都是原始的前端模型,jquery+bootstrap,冗杂的dom操作,繁琐的更新绑定.接触vue后,对前端MVVM框架有了全新的认识.本文是基于webpack+vue构建,由于之前的工作 ...

  4. SpringBoot + thymeleaf 实现分页

    SpringBoot结合Thymeleaf实现分页,很方便. 效果如下 后台代码 项目结构 1. 数据库Config 由于hibernate自动建表字符集为latin不能插入中文,故需要在applic ...

  5. 打通前后端全栈开发node+vue进阶【课程学习系统项目实战详细讲解】(3):用户添加/修改/删除 vue表格组件 vue分页组件

    第三章 建议学习时间8小时      总项目预计10章 学习方式:详细阅读,并手动实现相关代码(如果没有node和vue基础,请学习前面的vue和node基础博客[共10章] 演示地址:后台:demo ...

  6. vue学习—组件的定义注册

    组件的定义注册 效果: 方法一: <div id="box"> <v-header></v-header> <hr /> <b ...

  7. vue分页组件重置到首页问题

    分页组件,可以借用这个老哥的@暴脾气大大https://www.cnblogs.com/sebastian-tyd/p/7853188.html#4163272 但是有一个问题就是下面评论中@ Mrz ...

  8. VUE 分页组件

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  9. vue分页组件二次封装---每页请求特定数据

    关键步骤: 1.传两个参数:pageCount (每页条数).pageIndex (页码数): 2.bind方法的调用 <!-- 这部分是分页 --> <div class=&quo ...

随机推荐

  1. 西安7月21日「拥抱开源,又见.NET:壹周年Party」线下交流活动

    本次活动既是.NET西安社区的第四次线下交流活动,也是.NET西安社区成立一周年庆活动..NET西安社区2018年7月20日成立,经过一年时间的发展,社区共举办过3次大型线下交流活动,社区人数由最初的 ...

  2. 5分钟快速部署ownCloud私有云盘存储系统

    ownCloud 是一个开源免费专业的私有云存储项目,它能帮你快速在个人电脑或服务器上架设一套专属的私有云文件同步网盘,可以像 Dropbox 那样实现文件跨平台同步.共享.版本控制.团队协作等等.o ...

  3. 并发编程-concurrent指南-阻塞队列-延迟队列DelayQueue

    DelayQueue是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走.这种队列是有序的,即队头对象的延迟到期时间最长.注意:不能 ...

  4. Go - 循环

    目录 概述 循环 array 循环 slice 循环 map break continue goto switch 推荐阅读 概述 前几篇文章分享了 array 数组.slice 切片.map 集合, ...

  5. 成功入职ByteDance,分享我的八面面经心得!

    今天正式入职了字节跳动.办公环境也很好,这边一栋楼都是办公区域.公司内部配备各种小零食.饮料,还有免费的咖啡.15楼还有健身房.而且公司包三餐来着.下午三点半左右还会有阿姨推着小车给大家送下午茶.听说 ...

  6. OSPF 单区域实验

    实验拓扑 实验需求 按照图示配置 IP 地址 按照图示分区域配置 OSPF ,实现全网互通 为了路由结构稳定,要求路由器使用环回口作为 Router-id 实验步骤 每台路由器都要将本地的所有直连网段 ...

  7. Java内存模型以及线程安全的可见性问题

    Java内存模型 VS JVM运行时数据区 首先Java内存模型(JMM)和JVM运行时数据区并不是一个东西,许多介绍Java内存模型的文章描述的堆,方法区,Java虚拟机栈,本地方法栈,程序计数器这 ...

  8. JAVA接口的继承与集合

    复习 20190701 接口补充 一. java是单继承多实现 单继承: 一个类只能有一个父类 public class D extends D1 { } 2. 多实现 一个类可以同时实现多个接口 当 ...

  9. Bzoj 2281 [Sdoi2011]黑白棋 题解

    2281: [Sdoi2011]黑白棋 Time Limit: 3 Sec  Memory Limit: 512 MBSubmit: 592  Solved: 362[Submit][Status][ ...

  10. C++小游戏——井字棋

    #include<cstdio> #include<windows.h> #include<ctime> int main() { srand(time(NULL) ...