基于bootstrap的简单分页

本文展示了如何在Bootstrap框架中实现分页,并通过简单的JavaScript代码使其功能更加完善,包括分页导航的正确使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bootstrap本身的分页只具备外形,现在我们为它能够更好的使用添加了一些简单的JavaScript代码。

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="UTF-8">
	<!-- Bootstrap核心CSS文件 -->
	<link rel="stylesheet" href="http://cdn.bootcss.com/twitter-bootstrap/3.3.5/css/bootstrap.min.css">
	<!-- 可选的Bootstrap主题文件(一般不用引入) -->
	<link rel="stylesheet" href="http://cdn.bootcss.com/twitter-bootstrap/3.3.5/css/bootstrap-theme.min.css">
	<!-- jQuery文件,在bootstrap.min.js之前引入 -->
	<script src="http://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
	<!-- Bootstrap核心JavaScript文件 -->
	<script src="http://cdn.bootcss.com/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script> 
	<!-- 使得分页导航能够正确的使用 -->
	<script type="text/javascript">
	function action(id)
	{
		var totalPage = 20;//分页的总页数
		var movePage = 0;
		var newRelativePage = document.getElementById(id).name; 
		var newAbsolutePage = document.getElementById(id).innerHTML;
		var array = new Array(4,3,2,1,0);
		if(newRelativePage == "previous" )
			{
					for(var i = 1;i <=5;i ++)
					{
						document.getElementById('li'+i).innerHTML = "<a href=\"#\" id=\"page"+i+"\" name=\""+i+"\" onclick=\"action(this.id);\">"+i+"</a>";
					}
			}else if (newRelativePage == "next")
			{
					for(var i = 1;i <=5;i ++)
					{
							var resultPage = parseInt(totalPage) - parseInt(array[i-1]);
							document.getElementById('li'+i).innerHTML = "<a href=\"#\" id=\"page"+resultPage+"\" name=\""+resultPage+"\" onclick=\"action(this.id);\">"+resultPage+"</a>";
					}
			}else
			{
				if(newRelativePage == 1)
				{

				}else
				{
					movePage = newRelativePage - 1;
					for(var i = 1;i <=5;i ++)
					{

						var resultPage =  parseInt(movePage) + parseInt(i);
						if(resultPage <= 20)
						{
							document.getElementById('li'+i).innerHTML = "<a href=\"#\" id=\"page"+resultPage+"\" name=\""+resultPage+"\" onclick=\"action(this.id);\">"+resultPage+"</a>";
						}else
						{
							 document.getElementById('li'+i).innerHTML = "<a href=\"#\" id=\"page"+resultPage+"\" name=\""+resultPage+"\" onclick=\"action(this.id);\""+" class=\"btn btn-link disabled\">"+resultPage+"</a>";
						}

					}
	
		
				}
			};
	}
	</script>
</head>
<body>
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-12">
				<nav>
					<ul class="pagination pagination-lg">
						<li>
							<a href="#" aria-label="Previous" name="previous" id="previous" onclick="action(this.id);">
								<span aria-hidden="true">«</span>
							</a>
						</li>
						<li id="li1">
							<a href="#" id="page1" name="1" onclick="action(this.id);">1</a>
						</li>
						<li id="li2">
							<a href="#" id="page2" name="2" onclick="action(this.id);">2</a>
						</li>
						<li id="li3">
							<a href="#" id="page3" name="3" onclick="action(this.id);">3</a>
						</li>
						<li id="li4">
							<a href="#" id="page4" name="4" onclick="action(this.id);">4</a>
						</li>
						<li id="li5">
							<a href="#" id="page5" name="5" onclick="action(this.id);">5</a>
						</li>
						<li>
							<a href="#" aria-label="Next" name="next" id="next" onclick="action(this.id);">
								<span aria-hidden="true">»</span>
							</a>
						</li>
					</ul>
				</nav>
			</div>
		</div>
	</div>
</body>
</html>


package com.org.controller; import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.org.BaseController; import com.org.model.User; import com.org.service.IUserService; /** * @Author:liangjilong * @Date:2014-2-25 * @Version:1.0 * @Description: */ @Controller public class UserController extends BaseController { @Resource private IUserService userService; /*** * 方法一请求使用String * * 请求@RequestMapping匹配的URL request * * @param response * @return * @throws Exception */ @RequestMapping(value = "/userList1.do") public String userList1(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); } // userList指跳转到userList.jsp页面 return "userList"; } /** * 方法二请求使用ModelAndView * * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "/userList2.do") public ModelAndView userList2(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); } // userList指跳转到userList.jsp页面 return new ModelAndView("userList"); } /*** * 自定义标签实现分页 * * @param request * @param response * @param @ResponseBody ajax响应 * @param method={RequestMethod.POST,RequestMethod.GET}表单请求 * @param consumes="application/json; charset=UTF-8"请求格式是json * @return * @throws UnsupportedEncodingException * @throws Exception */ @RequestMapping(value = "/pageList.do" ,method={RequestMethod.POST,RequestMethod.GET}) public @ResponseBody ModelAndView getUserInfo(Model model, @RequestParam(required = false) String username, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) { try { String userName = new String(username.getBytes("ISO-8859-1"),"UTF-8");//处理乱码 Map<String, Object> map = new HashMap<String, Object>(); username=(username==null)?"":username; map.put("username", username);//username必须要和ibatis配置的property=username一致 Integer totalCount = this.userService.getUserCount(map); this.initPage(map, pageNum, pageSize, totalCount); List list = this.userService.getUserLists(map); this.initResult(model, list, map); return new ModelAndView("pagerList"); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 添加用户 * @param type * @param model * @return */ @RequestMapping("/addUser.do") public ModelAndView addUser(@RequestParam String username, Model model) { User user = new User(); user.setUsername(username); this.userService.addUser(user); return this.getUserInfo(model, null, null, null); } /*** * 删除用户 * @param id * @param pageNum * @param pageSize * @param model * @return */ @RequestMapping(value="/delUser.do",method={RequestMethod.POST,RequestMethod.GET},consumes="application/json; charset=UTF-8") @ResponseBody public ModelAndView delUser(@RequestParam(required = true) Integer id, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, Model model, HttpServletRequest request,HttpServletResponse response) { PrintWriter out=null; JSONObject result=new JSONObject(); try { out=response.getWriter(); this.userService.delUserById(id); result.put("flag", true); out.write(result.toString()); } catch (Exception e) { try { result.put("flag", false); out.write(result.toString()); } catch (JSONException e1) { e1.printStackTrace(); } } return null; //return this.getUserInfo(model, null, pageNum, pageSize); } /*** * 编辑用户 * @param id * @param model * @return */ @RequestMapping("/getUserById.do") public ModelAndView getUserById(@RequestParam(required = true) Integer id, Model model) { User u = this.userService.getUserById(id); model.addAttribute("user", u); return new ModelAndView("editUser"); } /*** * 编辑用户 * @param id * @param model * @return */ @RequestMapping("/editUser.do") public ModelAndView editUser(@RequestParam(required = true) Integer id, @RequestParam String username, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, Model model) { User u = new User(); u.setUsername(username); this.userService.editUser(u); return this.getUserInfo(model, null, pageNum, pageNum); } }
基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统基于bootstrap的后台管理系统
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值