1.背景:
前端页面使用bootstrap分页,同时与搜索条件联动;
2. jsp页面由服务端返回后, 异步请求动态创建表格, 分页的数据由服务端第一次返回后初始化, 以后每次异步请求再更新。
jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>Apollo - 签入记录 签入记录
标题 节目ID 创建时间 媒体类型 标志 进度 供应商 操作
js代码
$(function(){ initTable(); $('.tcdPageCode').extendPagination({ pageId : pageNo, totalCount : totalCount, showPage : 5, limit : pageSize, callback : function(pageNo, limit, totalCount) { checkinRequestSearch(pageNo); } }); $("#searchButton").on('click',function(){ checkinRequestSearch(pageNo); });});function initTable(){ $.ajax({ url : basePath + "page/checkinRequest_list.action", type : "get", dataType : "json", success : function(dataMap){ createTBody(dataMap); createTFoot(); }, error : function(errorData){ } });}function createTBody(dataMap){ if(dataMap!=null){ var html = []; var checkinRequestList = dataMap.checkinRequestList; for(var i=0; i'); html.push(''+checkinRequest.title+''); html.push(''+checkinRequest.assetguid+''); html.push(''+checkinRequest.createtime+''); html.push(''+checkinRequest.mediatype+''); html.push(''+checkinRequest.flag+''); html.push(''+checkinRequest.progress+''); html.push(''+checkinRequest.provider+''); html.push(' 查看 '); html.push(''); } $("#checkinRequest_tbody").empty().html(html.join('')); }}function createTFoot(dataMap){ var reloadPagination = false; if(dataMap!=null){ startNum = dataMap.startNum; stopNum = dataMap.stopNum; reloadPagination = totalCount==dataMap.totalCount?false:true totalCount = dataMap.totalCount; pageNo = dataMap.pageNo; //pageSize = dataMap.pageSize; } var str = '显示 ' + startNum + '至' + stopNum + '项 , 共' + totalCount +' 项。'; $("#checkinRequest_showlines").empty().html(str); if(totalCount=='0'){ $('.tcdPageCode').empty(); }else if($('.tcdPageCode').html()=='' || reloadPagination){ // $('.tcdPageCode').extendPagination({ pageId : pageNo, totalCount : totalCount, showPage : 5, limit : pageSize, callback : function(pageNo, limit, totalCount) { checkinRequestSearch(pageNo); } }); }}function checkinRequestSearch(pageNo){ var terminalid = $("#terminalid").val(); var flag = $("#flag").val(); $.ajax({ url : basePath + "page/checkinRequest_search.action", type : "post", data : { "pageNo" : pageNo, "terminalid" : terminalid, "flag" : flag }, dataType : "json", success : function(dataMap){ createTBody(dataMap); createTFoot(dataMap); }, error : function(errorData){ } });}
bootStrapPager分页插件
/** * Created by Hope on 2014/12/28. */(function ($) { $.fn.extendPagination = function (options) { var defaults = { pageId:'', totalCount: '', showPage: '10', limit: '5', callback: function () { return false; } }; $.extend(defaults, options || {});// alert(defaults.pageId); if (defaults.totalCount == '') { //alert('总数不能为空!'); $(this).empty(); return false; } else if (Number(defaults.totalCount) <= 0) { //alert('总数要大于0!'); $(this).empty(); return false; } if (defaults.showPage == '') { defaults.showPage = '10'; } else if (Number(defaults.showPage) <= 0)defaults.showPage = '10'; if (defaults.limit == '') { defaults.limit = '5'; } else if (Number(defaults.limit) <= 0)defaults.limit = '5'; var totalCount = Number(defaults.totalCount), showPage = Number(defaults.showPage), limit = Number(defaults.limit), totalPage = Math.ceil(totalCount / limit); if (totalPage > 0) { var html = []; html.push('
- '); html.push('
- « '); html.push(' '); if (totalPage <= showPage) { for (var i = 1; i <= totalPage; i++) { if (i == defaults.pageId) html.push('
- ' + i + ' '); else html.push('
- ' + i + ' '); } } else { for (var j = 1; j <= showPage; j++) { if (j == defaults.pageId) html.push('
- ' + j + ' '); else html.push('
- ' + j + ' '); } } html.push(' '); html.push('
- »
action代码
package com.cdv.apollo.action;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.HttpSession;import org.apache.commons.lang.StringUtils;import org.apache.struts2.ServletActionContext;import com.aliyun.oss.common.comm.ServiceClient.Request;import com.cdv.apollo.model.CheckinRequest;import com.cdv.apollo.service.CheckInRequestService;import com.cdv.apollo.util.PageParameter;import com.cdv.msf.sdk.UserClient;import com.opensymphony.xwork2.ActionSupport;import net.sf.json.JSONObject;public class CheckinRequestAction extends ActionSupport { /** * */ private static final long serialVersionUID = -7230950957476388246L; private MapdataMap = new HashMap (); public Map getDataMap() { return dataMap; } @Resource private CheckInRequestService checkInRequestService; public String index(){ HttpServletRequest request = ServletActionContext.getRequest(); PageParameter page = new PageParameter(); int totalCount = checkInRequestService.countTotalRecords(); // int totalPage = totalCount%page.getPageSize()==0?totalCount/page.getPageSize():totalCount/page.getPageSize()+1; int startNum = 0, stopNum = 0; if((startNum+page.getPageSize())<=totalCount){ startNum = 1; stopNum = startNum+page.getPageSize()-1; }else if(totalCount>0){ startNum = 1; stopNum = totalCount; } request.setAttribute("startNum", startNum); request.setAttribute("stopNum", stopNum); request.setAttribute("totalCount", totalCount); request.setAttribute("pageNo", page.getPageNo()); request.setAttribute("pageSize", page.getPageSize()); return "index"; } public String list(){ dataMap.clear(); List checkinRequestList = checkInRequestService.list(-1, null, null, null, null, null, 0, 10); dataMap.put("checkinRequestList", checkinRequestList); return SUCCESS; } public String search(){ dataMap.clear(); PageParameter page = new PageParameter(); int s = 0, max = 10, pageNo = 1, flag=-1; int startNum = 0, stopNum = 0; HttpServletRequest request = ServletActionContext.getRequest(); String pageNoStr = (String) request.getParameter("pageNo"); String terminalid = (String) request.getParameter("terminalid"); String flagStr = (String) request.getParameter("flag"); if(StringUtils.isNotEmpty(flagStr)){ flag = Integer.parseInt(flagStr); } int totalCount = checkInRequestService.count2Terminal(terminalid, flag); int totalPage = totalCount%page.getPageSize()==0?totalCount/page.getPageSize():totalCount/page.getPageSize()+1; if(StringUtils.isNotEmpty(pageNoStr)){ pageNo = Integer.parseInt(pageNoStr); pageNo = pageNo<=totalPage?pageNo:1; s = (pageNo-1)*10; } List checkinRequestList = checkInRequestService.list2Terminal(terminalid, flag, s, max); if(totalCount>0){ startNum = (pageNo-1)*page.getPageSize() + 1; stopNum = (startNum-1+page.getPageSize())<=totalCount?(startNum-1+page.getPageSize()):totalCount; } dataMap.put("startNum", startNum); dataMap.put("stopNum", stopNum); dataMap.put("totalCount", totalCount); dataMap.put("pageNo", pageNo); dataMap.put("checkinRequestList", checkinRequestList); return SUCCESS; } }
struts配置
/page/checkinRequest.jsp dataMap