导读

本篇着重介绍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. 前端学习【第一篇】: HTML内容

    内容概要: HTML介绍 常用标签介绍 一. HTML介绍 web服务的本质 #!/usr/bin/env python3 # _*_ coding:utf- _*_ import socket sk ...

  2. Centos7.3搭建DNS服务器--BIND

    1.系统环境说明 [root@dns-server etc]# cat /etc/redhat-release CentOS Linux release (Core) 防火墙和Selinux关闭 [r ...

  3. 简书全站爬取 mysql异步保存

    # 简书网 # 数据保存在mysql中; 将selenium+chromedriver集成到scrapy; 整个网站数据爬取 # 抓取ajax数据 #爬虫文件 # -*- coding: utf-8 ...

  4. MySQL login-path 本地快捷登陆

    目录 1.什么是 login-path 2. 配置 login-path 2.2.配置: 2.3.显示配置: 2.3.1.显示执行的login-path配置 2.3.2.显示所有的login-path ...

  5. C++学习书籍推荐《C++编程思想第二版第一卷》下载

    百度云及其他网盘下载地址:点我 编辑推荐 “经典原版书库”是响应教育部提出的使用原版国外教材的号召,为国内高校的计算机教学度身订造的.<C++编程思想>(英文版第2版)是书库中的一本,在广 ...

  6. 产品经理人的持续交付和DevOps实践

    如果你正处于下列情形中 ,那这篇文章是为你准备的: 你目前身处技术行业,你是产品经理,并且,你明白特性分支是什么,CD代表什么,DevOps文化是什么样子的. 或者,你已经在实施敏捷,团队每周都会与您 ...

  7. c++小游戏——扫雷

    #include<cstdio> #include<cstring> #include<algorithm> #include<conio.h> #in ...

  8. local class incompatible: stream classdesc serialVersionUID = 4125096758372084309, local class serialVersionUID = 7725746634795906143

    local class incompatible: stream classdesc serialVersionUID = 4125096758372084309, local class seria ...

  9. Baozi Leetcode Solution 290: Word Pattern

    Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...

  10. 小白开学Asp.Net Core《三》

    小白开学Asp.Net Core<三> ——界面 我胡汉三再次又回来了(距离上篇时间有点长),今天抽时间将最近对框架采用的后台界面做个记录 1.先上图 (图一) (图二) 2.界面说明 后 ...