1.页面准备分页的表格与分页div

  同时需要在查询条件表单中准备隐藏当前页与页大小的文本框

            <div class="container-fluid">
<div class="panel panel-default">
<!--菜单连接标题-->
<div class="panel-heading">
<span>大修管理</span><span>>大修信息管理</span>
</div> <div class="el_main"> <!--内容-->
<div class="el_qlmContent"> <!--索引-->
<div class="row el_queryBox">
<form id="haulQueryForm">
<!--隐藏当前页与页大小 -->
<input type="hidden" name="currentPage" id="currentPage">
<input type="hidden" name="currentCount" id="currentCount"> <div class="row"> <div class="col-md-3 el_qlmQuery">
<div class="input-group" role="toolbar">
<span class="el_spans">大修名称:</span> <input type="text"
class="form-control" name="bigName" />
</div>
</div> <div class="col-md-3 el_qlmQuery">
<div class="input-group" role="toolbar">
<span class="el_spans">大修时间:</span> <input type="text"
name="startMonth" id="test" class="wicon form-control"
readonly />
</div>
</div> <div class="col-md-3 el_qlmQuery">
<div class="input-group" role="toolbar">
<span class="el_spans">大修状态:</span> <select
class="selectpicker form-control" name="bigStatus"
title="请选择">
<option value="">--请选择--</option>
<option value="未开始">未开始</option>
<option value="进行中">进行中</option>
<option value="已结束">已结束</option>
</select>
</div>
</div> </div> <!--提交查询按钮-->
<button type="reset"
class="btn btn-default el_queryButton0 btn-sm">清空</button>
<button type="button" id="haulQueryButton"
class="btn btn-default el_queryButton btn-sm">查询</button>
</form> </div>
<!--结束 查询表单提交--> <!--显示内容-->
<h4 class="el_mainHead">大修信息</h4>
<div class="panel panel-default el_Mainmain"> <!--按钮面板-->
<div class="panel-body"> <div class="panel panel-default">
<div class="panel-body el_MainxiaoMain"> <div class="el_topButton">
<button class="btn btn-primary" onclick="el_addOverhaul()">
创建大修</button>
</div>
</div>
</div> <!-- 表格 内容都提取到json里边 -->
<table class="table table-hover table-bordered" id="newsTable">
<thead>
<tr>
<th>序号</th>
<th>大修名称</th>
<th>时间</th>
<th>状态</th>
<th width="350">操作</th>
</tr>
</thead>
<tbody id="haulTbody">
</tbody>
</table> <!--分页-->
<div id="paginationIDU" class="paginationID"></div>
</div>
</div>

 2.JS代码:

  解释:点击查询的时候将页号清空(这个有时候容易忘),带着组合条件去后台查询,查询成功后将数据填充到表格之后显示分页组件。点击分页组件的页号与上下页的时候动态设置页面中隐藏域的值,同时调用查询方法。

/**
* 页面初始化函数
*/
$(function() {
// 页面初始化查询大修信息
queryHaulFun();
// 查询的点击事件
$("#haulQueryButton").click(function() {
$("#currentPage").val("");// 清空页数
queryHaulFun();
}); });
/** *S 分页查询大修信息*** */
// 查询大修
var queryHaulFun = function() {
$.ajax({
url : contextPath + "/findPageHaul.action",
data : $("#haulQueryForm").serialize(),
dataType : "JSON",
async : true,
type : "POST",
success : showHaulTable,
error : function() {
alert("查询大修失败!!!");
} });
}
// 显示大修分页信息到表格
function showHaulTable(response) {
$("#haulTbody").html("");// 清空表格
var haulinfos = response.pageBean.productList;// 获取到大修JSON对象
var currentCount = response.pageBean.currentCount;// 页大小
var totalCount = response.pageBean.totalCount;// 页总数
var currentPage = response.pageBean.currentPage;// 当前页
for (var i = 0, length_1 = haulinfos.length; i < length_1; i++) {
// 填充表格
$("#haulTbody")
.append(
'<tr><td>'
+ (parseInt(currentCount)
* parseInt(currentPage - 1) + (i + 1))
+ '</td><td>'
+ haulinfos[i].bigname
+ '</td><td>'
+ haulinfos[i].bigbegindate
+ ' 到 '
+ haulinfos[i].bigenddate
+ '</td><td>'
+ haulinfos[i].bigstatus
+ '</td><td>'
+ '<a href="<%=path%>/view/overhaul/overhaulInfo.jsp">详情</a>'
+ '<a href="javascript:void(0)" onclick="el_modifyOverhaul()">修改</a>'
+ '<a href="javascript:void(0)" onclick="delcfmOverhaul()">删除</a>'
+ '</td></tr>');
}
// 动态开启分页组件
page(currentPage, totalCount, currentCount);
}
// 显示分页
function page(currentPage, totalCount, currentCount) {
// 修改分页的基本属性
$('#paginationIDU').pagination(
{
// 组件属性
"total" : totalCount,// 数字 当分页建立时设置记录的总数量 1
"pageSize" : currentCount,// 数字 每一页显示的数量 10
"pageNumber" : currentPage,// 数字 当分页建立时,显示的页数 1
"pageList" : [ 8 ],// 数组 用户可以修改每一页的大小,
// 功能
"layout" : [ 'list', 'sep', 'first', 'prev', 'manual', 'next',
'last', 'links' ],
"onSelectPage" : function(pageNumber, pageSize) {
$("#currentPage").val(pageNumber);
$("#currentCount").val(pageSize);
// 查询大修
queryHaulFun();
}
});
}
/** *E 分页查询大修信息*** */

 后台代码

工具类: PageBean.java

package cn.xm.exam.utils;

/**
* 分页工具类
*/ import java.util.ArrayList;
import java.util.List; public class PageBean<T> { // 当前页
private int currentPage;
// 当前页显示的条数
private int currentCount;
// 总条数
private int totalCount;
// 总页数
private int totalPage;
// 每页显示的数据
private List<T> productList = new ArrayList<T>(); public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public int getCurrentCount() {
return currentCount;
} public void setCurrentCount(int currentCount) {
this.currentCount = currentCount;
} public int getTotalCount() {
return totalCount;
} public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
} public int getTotalPage() {
return totalPage;
} public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
} public List<T> getProductList() {
return productList;
} public void setProductList(List<T> productList) {
this.productList = productList;
} @Override
public String toString() {
return "PageBean [currentPage=" + currentPage + ", currentCount=" + currentCount + ", totalCount=" + totalCount
+ ", totalPage=" + totalPage + ", productList=" + productList + "]";
} }

3.Action代码:

  首先组装查询条件(对数据进行初始化处理),调用service层进行查询,返回的索引信息都封装在pageBean对象中,将PageBean装入map中转为JSON传到前台。

package cn.xm.exam.action.haul;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map; import javax.annotation.Resource; import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import cn.xm.exam.bean.haul.Haulinfo;
import cn.xm.exam.service.haul.HaulinfoService;
import cn.xm.exam.utils.PageBean;
import cn.xm.exam.utils.ValidateCheck; /**
* 查询大修的Action
*
* @author QiaoLiQiang
* @time 2017年11月10日下午7:45:09
*/
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class FindHaulAction extends ActionSupport {
private Logger logger = Logger.getLogger(FindHaulAction.class);// 日志记录器
private Map<String, Object> response;// 用于包装返回结果的map
@Resource
private HaulinfoService haulinfoService;
private String currentPage;// 当前页
private String currentCount;// 页大小
private String bigName;// 大修名称
private String bigStatus;// 大修状态
private String startMonth;// 创建月份 @Override
public String execute() {
response = new HashMap<String, Object>();
Map<String, Object> condition = generateQueryHaulCondition();
PageBean<Haulinfo> pageBean = null;
try {
pageBean = haulinfoService.getHaulinfoPageByCondition(Integer.valueOf(currentPage),
Integer.valueOf(currentCount), condition);
} catch (NumberFormatException e) {
logger.error("数字格式化异常", e);
} catch (SQLException e) {
logger.error("查询大修SQL异常", e);
}
response.put("pageBean", pageBean);
return SUCCESS;
} /**
* 组装查询条件
*
* @param condition
* @return
*/
private Map<String, Object> generateQueryHaulCondition() {
Map<String, Object> condition = new HashMap<String, Object>();
if (ValidateCheck.isNull(currentCount)) {
currentCount = "8";
}
if (ValidateCheck.isNull(currentPage)) {
currentPage = "1";
}
if (ValidateCheck.isNotNull(bigName)) {
condition.put("bigName", bigName);
}
if (ValidateCheck.isNotNull(bigStatus)) {
condition.put("bigStatus", bigStatus);
}
if (ValidateCheck.isNotNull(startMonth)) {
condition.put("startMonth", startMonth);
}
return condition;
} // get set
public Map<String, Object> getResponse() {
return response;
} public void setResponse(Map<String, Object> response) {
this.response = response;
} public String getCurrentPage() {
return currentPage;
} public void setCurrentPage(String currentPage) {
this.currentPage = currentPage;
} public String getCurrentCount() {
return currentCount;
} public void setCurrentCount(String currentCount) {
this.currentCount = currentCount;
} public String getBigName() {
return bigName;
} public void setBigName(String bigName) {
this.bigName = bigName;
} public String getBigStatus() {
return bigStatus;
} public void setBigStatus(String bigStatus) {
this.bigStatus = bigStatus;
} public String getStartMonth() {
return startMonth;
} public void setStartMonth(String startMonth) {
this.startMonth = startMonth;
} }

 4.Service层代码:

    将当前页,页大小,页总数,总记录数,数据集合装入PageBean返回给Action。(满足条件的总数需要根据条件查出,之后计算出总页数,并根据当前页与页大小计算起始值并装入条件map中传到mapper层查询数据集合)

@Override
public PageBean<Haulinfo> getHaulinfoPageByCondition(int currentPage, int currentCount,
Map<String, Object> condition) throws SQLException {
PageBean<Haulinfo> pageBean = new PageBean<Haulinfo>();
pageBean.setCurrentCount(currentCount);// 设置页大小
pageBean.setCurrentPage(currentPage);// 设置当前页
int total = 0;
int totalCount = haulinfoCustomMapper.getHaulinfoTotalByCondition(condition);// 查询满足条件的总数
pageBean.setTotalCount(totalCount);// 设置总记录数
int totalPage = (int) Math.ceil(1.0 * totalCount / currentCount);
pageBean.setTotalPage(totalPage);// 设置总页数 /******
* 计算起始值
* 页数 起始值 页大小
* 1 0 8
* 2 8 16
*******/
int index = (currentPage - 1) * currentCount;// 起始值
condition.put("index", index);
condition.put("currentCount", currentCount);
List<Haulinfo> haulinfos = haulinfoCustomMapper.getHaulinfoslByCondition(condition);
pageBean.setProductList(haulinfos);//设置数据集合
return pageBean;
}

5.Mapper层代码

  mapper接口:

package cn.xm.exam.mapper.haul.custom;

import java.sql.SQLException;
import java.util.List;
import java.util.Map; import cn.xm.exam.bean.haul.Haulinfo; /**
* 大修基本信息mapper
*
* @author QiaoLiQiang
* @time 2017年11月10日下午12:23:15
*/
public interface HaulinfoCustomMapper { /**
* 查询满足条件的大修总数
*
* @param condition
* 条件
* @return
* @throws SQLException
*/
public int getHaulinfoTotalByCondition(Map<String, Object> condition) throws SQLException; /**
* 组合条件查询大修信息用于分页显示大修
*
* @param condition
* @return
* @throws SQLException
*/
public List<Haulinfo> getHaulinfoslByCondition(Map<String, Object> condition) throws SQLException;
}

mapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.xm.exam.mapper.haul.custom.HaulinfoCustomMapper"> <!--S 查询分页总数 -->
<select id="getHaulinfoTotalByCondition" parameterType="hashmap"
resultType="_int">
SELECT
COUNT(bigId) FROM haulinfo
<where>
<include refid="queryHaulWhere"></include>
</where>
</select>
<!--E 查询分页总数 -->
<!--S 分页查询大修 -->
<select id="getHaulinfoslByCondition" resultType="cn.xm.exam.bean.haul.Haulinfo"
parameterType="hashmap">
SELECT * FROM haulinfo
<where>
<include refid="queryHaulWhere"></include>
</where>
<include refid="queryHaulLimit"></include>
</select>
<!--E 分页查询大修 --> <!--S 组装查询条件 -->
<sql id="queryHaulWhere">
<if test="bigName!=null">
and bigName LIKE '%${bigName}%'
</if>
<if test="bigStatus!=null">
and bigStatus=#{bigStatus}
</if>
<if test="startMonth!=null">
and DATE_FORMAT(bigCreateDate,'%Y-%m')=#{startMonth}
</if>
</sql>
<!--E 组装查询条件 -->
<!--S 分页条件 -->
<sql id="queryHaulLimit">
<if test="index!=null">
LIMIT #{index},#{currentCount}
</if>
</sql>
<!--E 组装分页条件 --> </mapper>

结果:

  后台传到前台的JSON:

   效果:

ajax分页查询信息的通用方法的更多相关文章

  1. 分页查询信息(使用jdbc连接mysql数据库实现分页查询任务)

             分页查询信息       使用jdbc连接mysql数据库实现分页查询任务 通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上. 本项目 ...

  2. 【SQL】Oracle分页查询的三种方法

    [SQL]Oracle分页查询的三种方法 采用伪列 rownum 查询前10条记录 ? 1 2 3 4 5 6 7 8 9 10 11 [sql] select * from t_user t whe ...

  3. Oracle 数据库分页查询的三种方法

    一.Oracle 数据库分页查询的三种方法 1.简介 不能对 rownum 使用 >(大于或等于 1 的数值).>=(大于 1 的数值).=(不等于 1 的数值),否则无结果.所以直接用 ...

  4. ajax分页查询

    (1)先写个显示数据的页面,分页查询需要那几个部分呢? 1.首先是查询的文本框输入,还有查询按钮,那么就开始写代码吧 1 2 3 4 <div> <input type=" ...

  5. mongodb多条件分页查询的三种方法(转)

    一.使用limit和skip进行分页查询 public List<User> pageList(int pageNum ,int pageSize){ List<User> u ...

  6. Hibernate分页查询的两个方法

    Hibernate中HQL查询语句有一个分页查询, session.setMaxResult(参数)和session.setFirstResult(参数) 如果只设置session.setMaxRes ...

  7. SQL 分页查询的四种方法

    方法一 假设现在有这样的一张表: CREATE TABLE test ( id int primary key not null identity, names ) ) 然后向里面插入大约100条数据 ...

  8. springMVC+ajax分页查询

    项目用到ajax技术的查询,查询结果很多时候要分页展示.这两天摸索了一下,在这里做一总结,方便自己随时查看, 也方便后人参考. 这里的顺序遵从从前台页面到后台控制器,业务层,Dao层,Mapper 下 ...

  9. 使用ajax分页查询

    controller: /** * 查询所有用户/查找指定用户 * 分页+搜索 * */@RequestMapping("/findClientBySize")@ResponseB ...

随机推荐

  1. 209 Minimum Size Subarray Sum 大于给定和最短子数组

    给定一个含有 n 个正整数的数组和一个正整数 s , 找到一个最小的连续子数组的长度,使得这个子数组的数字和 ≥  s .如果不存在符合条件的子数组,返回 0.举个例子,给定数组 [2,3,1,2,4 ...

  2. jq星星评分

    html代码 <div class="make_mark"> <h5>请为这次服务打分</h5> <div class="mar ...

  3. html5移动端适配- media query

    iPad部分css适配 - media query 代码如下图:   注:  @media要放在css最下方,防止被覆盖.  

  4. Android组件化开发(注意事项)

    1.Manifest合并 在Android studio编译项目时,无论你使用了几个Module都会把所有Manifest最终合并成一个,需要我们注意的是application标签下这个几个属性引用的 ...

  5. iOS Programming UISplitViewController

    iOS Programming UISplitViewController  The iPad, on the other hand, has plenty of screen space to pr ...

  6. 11 Hash tables

    11 Hash tables    Many applications require a dynamic set that supports only the dictionary operatio ...

  7. DLL线程中坑爹的Synchronize?

    1, 缘起 某次开发语音对讲windows程序,采用delphi语言,及delphix的TDXSound控件. DXSound提供了TSoundCaptureStream类,可以实现指定频率.位数.声 ...

  8. 企业面试之LeetCode刷题心得

    谈起刷LeetCode的心得,想要先扯点别的,说实话我是比较自虐的人,大学时候本专业从来不好好上,一直觊觎着别人的专业,因为自己文科生,总觉得没有项技术在身出门找工作都没有底气,然后看什么炫学什么,简 ...

  9. MySQL for Mac 终端操作说明

    mysql for mac 终端操作说明MySQL服务开启Mac版mysql可以从设置里启动服务: 如果想要在终端(Terminal)中操作mysql,需要先添加mysql路径,在此以zsh为例: # ...

  10. QTreeWidgetItem和QTreeWidgetItemIterator

    1.{ ui->treeWidget->setHeaderHidden(true); ui->treeWidget->clear(); QTreeWidgetItem *ima ...