分页插件3-指定到某一页

前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)
html

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<title>分页</title>
	<style type="text/css">
		.pages{text-align: center;}
		.page_div {
				margin-top: 20px;
				margin-bottom: 20px;
				font-size: 15px;
				font-family: "microsoft yahei";
				color: #666666;
				margin-right: 10px;
				padding-left: 20px;
				box-sizing: border-box;
			}
			/*
			 * 页数按钮样式
			 */
			
			.page_div a {
				min-width: 40px;
				border: 1px solid #E3E3E3;
			    height: 40px;
			    text-align: center;
			    margin: 0 4px;
			    cursor: pointer;
			    line-height: 40px;
			    border-radius: 4px;
			    font-size: 13px;
			    display: inline-block;
			}
			.page_div a:hover{
				background: #008C5F;
				color: #fff;
			}
			
			#firstPage,
			#lastPage {
				width: 50px;
				/*color: #0073A9;*/
				/*border: 1px solid #0073A9!important;*/
			}
			
			#prePage,
			#nextPage {
				width: 70px;
				/*color: #0073A9;*/
				/*border: 1px solid #0073A9!important;*/
			}
			.page_div .pagegotos{
				width: 30px;
			    vertical-align: middle;
			    margin: 0 4px;
			    height: 20px;
			    line-height: 20px;
			    padding-left: 3px;
			    outline: none;
			    border-radius: 3px;
    			border: 1px solid #ddd;
			}
			.page_div .pagegotucur{
				font-weight: normal;
			    cursor: pointer;
			    background: #1A64FF;
			    color: #fff;
			    display: inline-block;
			    vertical-align: middle;
			    height: 24px;
			    line-height: 24px;
			    padding: 0 5px;
			    border-radius: 3px;
			}
			.page_div .pagegotucur:hover{opacity: .8;}
			.page_div .current {
				    background-color: #008C5F;
				    border-color: #0073A9;
				    color: #FFFFFF;
			}
			
			.totalPages {
				margin: 0 10px;
			}
			
			.totalPages span,
			.totalSize span {
				color: #0073A9;
				margin: 0 5px;
			}
			input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

input[type="number"] {
    -moz-appearance: textfield;
}
	</style>
</head>
<body>
	<p>分页</p>
	<!--分页-->
	<div class="pages">
		<div value="1 0"></div>
		<div id="pagesd" class="page_div"></div>
	</div>
	<script src="jquery.min.js"></script>
	<script src="paging.js"></script>
	<script type="text/javascript">
		
//		$("#page").paging({
//			pageNo:1,
//			totalPage: 1,
//			callback: function(num) {
//				console.log(num)
//			}
//		})
		$("#pagesd").paging2({
			pageNo:1,
			totalPage: 15,
			totalSize:500,
			callback: function(num) {
				console.log(num)
			}
		})
		
//		setTimeout(function(){
//			$("#page").paging({
//				pageNo:14,
//				totalPage: 30,
//				totalSize:500,
//				callback: function(num) {
//					console.log(num)
//				}
//			})
//		},2000)
		
		
		
		/*
		// 模拟ajax数据用以下方法
		//参数为当前页
		ajaxTest(1);
		
		function ajaxTest(num) {
			$.ajax({
				url: "table.json",
				type: "get",
				data: {},
				dataType: "json",
				success: function(data) {
					console.log(data);
					//分页
					$("#page").paging({
						pageNo: num,
						totalPage: data.totalPage,
						totalSize: data.totalSize,
						callback: function(num) {
							ajaxTest(num)
						}
					})
				}
			})
		}
		*/
	</script>
</body>
</html>

paging.js

(function($, window, document, undefined) {
	//定义分页类
	function Paging2(element, options) {
		this.element = element;
		//传入形参
		this.options = {
			pageNo: options.pageNo||1,
			totalPage: options.totalPage,
			totalSize:options.totalSize,
			callback:options.callback
		};
		//根据形参初始化分页html和css代码
		this.init();
	}
	//对Paging的实例对象添加公共的属性和方法
	Paging2.prototype = {
		constructor: Paging2,
		init: function() {
			this.creatHtml();
			this.bindEvent();
		},
		creatHtml: function() {
			var me = this;
			var content = "";
			var current = me.options.pageNo;
			var total = me.options.totalPage;
			var totalNum = me.options.totalSize;
			content += "<span class='totalSize'> 共<span>"+totalNum+"</span>条 </span>";
			content += "<a id=\"firstPage\">首页</a><a id='prePage'>上一页</a>";
			//总页数大于6时候
			if(total > 6) {
				//当前页数小于5时显示省略号
				if(current < 5) {
					for(var i = 1; i < 6; i++) {
						if(current == i) {
							content += "<a class='current'>" + i + "</a>";
						} else {
							content += "<a>" + i + "</a>";
						}
					}
					content += ". . .";
					content += "<a>"+total+"</a>";
				} else {
					 //判断页码在末尾的时候
					if(current < total - 3) {
						for(var i = current - 2; i < current + 3; i++) {
							if(current == i) {
								content += "<a class='current'>" + i + "</a>";
							} else {
								content += "<a>" + i + "</a>";
							}
						}
						content += ". . .";
						content += "<a>"+total+"</a>";
					//页码在中间部分时候	
					} else {
						content += "<a>1</a>";
						content += ". . .";
						for(var i = total - 4; i < total + 1; i++) {
							if(current == i) {
								content += "<a class='current'>" + i + "</a>";
							} else {
								content += "<a>" + i + "</a>";
							}
						}
					}
				}
				//页面总数小于6的时候
			} else {
				for(var i = 1; i < total + 1; i++) {
					if(current == i) {
						content += "<a class='current'>" + i + "</a>";
					} else {
						content += "<a>" + i + "</a>";
					}
				}
			}
			
			content += "<a id='nextPage'>下一页</a>";
			content += "<a id=\"lastPage\">尾页</a>";
			content += '<span>到</span><input class="pagegotos" type="number"/><span>页</span> <b class="pagegotucur">确定</b>';
			if(total>1){
				me.element.html(content);
			}
		},
		//添加页面操作事件
		bindEvent: function() {
			var me = this;
			me.element.off('click', 'a');
			me.element.on('click', 'a', function() {
				var num = $(this).html();
				var id=$(this).attr("id");
				if(id == "prePage") {
					if(me.options.pageNo == 1) {
						me.options.pageNo = 1;
					} else {
						me.options.pageNo = +me.options.pageNo - 1;
					}
				} else if(id == "nextPage") {
					if(me.options.pageNo == me.options.totalPage) {
						me.options.pageNo = me.options.totalPage
					} else {
						me.options.pageNo = +me.options.pageNo + 1;
					}
 
				} else if(id =="firstPage") {
					me.options.pageNo = 1;
				} else if(id =="lastPage") {
					me.options.pageNo = me.options.totalPage;
				}else{
					me.options.pageNo = +num;
				}
				me.creatHtml();
				if(me.options.callback) {
					me.options.callback(me.options.pageNo);
				}
			});
			me.element.on('click', '.pagegotucur', function() {
				var pageval =$('.pagegotos').val();
				if(pageval == 'NaN' || pageval<1 ){
					return false;
				}
				var num = pageval;
					me.options.pageNo = +num;
				me.creatHtml();
				if(me.options.callback) {
					me.options.callback(me.options.pageNo);
				}
			});
		}
	};
	//通过jQuery对象初始化分页对象
	$.fn.paging2 = function(options) {
		return new Paging2($(this), options);
	}
})(jQuery, window, document);

效果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Mybatis-Plus分页件可以通过以下步骤进行配置: 1. 在pom.xml文件中添加依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 在Mybatis的配置文件中添加分页件: ```xml <plugins> <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"> <property name="dialectType" value="mysql"/> </plugin> </plugins> ``` 其中,dialectType属性指定了数据库的方言类型,例如mysql、oracle等。 3. 在Mapper接口中添加分页方法: ```java List<User> selectUserPage(Page<User> page, @Param("username") String username); ``` 其中,Page是Mybatis-Plus提供的分页对象,可以通过它设置分页参数,例如当前页码、每页显示数量等。 4. 在Service层中调用分页方法: ```java Page<User> page = new Page<>(1, 10); List<User> userList = userService.selectUserPage(page, "张三"); ``` 其中,第一个参数是当前页码,第二个参数是每页显示数量。 以上就是Mybatis-Plus分页件的配置方法。 ### 回答2: MyBatis-Plus是一个优秀的ORM框架,提供了丰富的功能件。其中,分页件是使用MyBatis-Plus开发项目的常用功能之一。本文将介绍如何配置MyBatis-Plus分页件。 1. 引入MyBatis-Plus分页件依赖 在Maven中,我们可以通过添加以下依赖来引入MyBatis-Plus分页件: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>latest-version</version> </dependency> ``` 2. 配置分页件 Mybatis-Plus分页件的配置一般是通过在配置文件中进行定义的。我们可以在`application.properties`中添加以下配置项来开启分页件: ``` # 开启分页件 mybatis-plus.pagehelper.enable=true ``` 当然,如果你不喜欢默认的分页件,你也可以通过以下方法自定义自己的分页件: ```java @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } } ``` 3. 执行分页查询 完成以上配置后,我们就可以在代码中使用分页查询了。在Mapper中,我们可以通过继承`BaseMapper`接口来使用MyBatis-Plus的通用Mapper,其中包含`selectPage`方法: ``` public interface UserMapper extends BaseMapper<User> { /** * 分页查询用户列表 * * @param page 分页信息 * @param user 查询条件 * @return 用户列表 */ IPage<User> selectUserPage(Page<User> page, User user); } ``` 在Service中,我们可以通过调用Mapper中的`selectPage`方法来执行分页查询: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUserPage(Page<User> page, User user) { return userMapper.selectUserPage(page, user); } } ``` 以上就是MyBatis-Plus分页件的基本配置和使用方法。我们可以根据自己的需求进行配置和使用,以满足项目的实际需要。 ### 回答3: Mybatis-Plus是一款基于Mybatis的增强工具包,其提供了一些实用的件,比如分页件。这个分页件功能强大、易用且功能齐全,是许多企业得心应手的利器。下面我们就具体讲一下Mybatis-Plus分页件的配置方法。 1. 引入依赖 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> ``` 2. 配置分页件 在Spring Boot的配置文件application.yml或application.properties中添加以下配置: ```yaml mybatis-plus: # 配置分页件 configuration: # 数据库方言 db-config: id: db2 dialect: mysql db-type: mysql pageNumType: int pageSizeType: int global-config: db-config: id-type: auto page-helper: auto-dialect: false ``` - dialect:配置数据库方言,Mybatis-Plus提供了多种方言,如MySQL、Oracle、SQL Server等,分别对应的dialect为mysql、oracle、sqlserver等。 - id-type:配置ID的生成策略,Mybatis-Plus提供了多种策略,如自增主键、UUID、雪花算法等。 - auto-dialect:自动检测数据库方言,设置为false则需要手动配置dialect。 3. 使用分页件 在查询方法中使用Page对象进行分页查询: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Autowired private UserMapper userMapper; public Page<User> getUserList(int pageNum, int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); return userMapper.getUserList(page); } } ``` 注意:需要将Page对象作为参数传入查询方法中,并在查询方法中使用Page对象的相关方法进行分页查询。 4. 分页查询结果封装 使用分页件后,查询结果会自动封装为带分页信息的Page对象,我们可以在对应的Controller中将查询结果返回给前端。 ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public R getUserList(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize) { Page<User> pageData = userService.getUserList(pageNum, pageSize); return R.ok().put("page", pageData); } } ``` 其中,R.ok()表示成功返回,put()方法中可以加入其它需要返回的参数。最后将查询结果封装到Page对象中返回给前端。 根据以上四个步骤,我们就可以成功地配置并使用Mybatis-Plus分页件实现分页查询了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值