前言

基于SpringMVC+Bootstrap+DataTables实现数据表格服务端分页、模糊查询(非DataTables Search),页面异步刷新。

说明:sp:message标签是使用了SpringMVC国际化

效果

DataTable表格

关键字查询

自定义关键字查询,非DataTable Search

代码

HTML代码

查询条件代码

<!-- 查询、添加、批量删除、导出、刷新 -->

<div class="row-fluid">

<div class="pull-right">

<div class="btn-group">

<button type="button" class="btn btn-primary btn-sm" id="btn-add">

<i class="fa fa-plus"></i> <sp:message code="sys.add"/>

</button>

<button type="button" class="btn btn-primary btn-sm" id="btn-delAll">

<i class="fa fa-remove"></i> <sp:message code="sys.delAll"/>

</button>

<button type="button" class="btn btn-primary btn-sm" id="btn-export">

<i class="fa fa-sign-in"></i> <sp:message code="sys.export"/>

</button>

<button type="button" class="btn btn-primary btn-sm" id="btn-re">

<i class="fa fa-refresh"></i> <sp:message code="sys.refresh"/>

</button>

</div>

</div>

<div class="row">

<form id="queryForm" action="<%=path%>/goodsType/list" method="post">

<div class="col-xs-2">

<input type="text" id="keyword" name="keyword" class="form-control input-sm"

placeholder="<sp:message code="sys.keyword"/>">

</div>

<button type="button" class="btn btn-primary btn-sm" id="btn-query">

<i class="fa fa-search"></i> <sp:message code="sys.query"/>

</button>

</form>

</div>

</div>

数据table代码

<table id="dataTable" class="table table-striped table-bordered table-hover table-condensed" align="center">

<thead>

<tr class="info">

<td><input type="checkbox" id="checkAll"></td>

<th><sp:message code="sys.no"/></th>

<th><sp:message code="goods.type.name.cn"/></th>

<th><sp:message code="goods.type.name.en"/></th>

<th><sp:message code="sys.create.time"/></th>

<th><sp:message code="sys.update.time"/></th>

<th><sp:message code="sys.oper"/></th>

</tr>

</thead>

</table>

JS代码

DataTables初始化、服务端数据请求、查询条件封装

<!-- page script -->

<script>

$(function () {

//添加、修改异步提交地址

var url = "";

var tables = $("#dataTable").dataTable({

serverSide: true,//分页,取数据等等的都放到服务端去

processing: true,//载入数据的时候是否显示“载入中”

pageLength: 10, //首次加载的数据条数

ordering: false, //排序操作在服务端进行,所以可以关了。

pagingType: "full_numbers",

autoWidth: false,

stateSave: true,//保持翻页状态,和comTable.fnDraw(false);结合使用

searching: false,//禁用datatables搜索


ajax: {

type: "post",

url: "<%=path%>/goodsType/getData",

dataSrc: "data",

data: function (d) {

var param = {};

param.draw = d.draw;

param.start = d.start;

param.length = d.length;

var formData = $("#queryForm").serializeArray();//把form里面的数据序列化成数组

formData.forEach(function (e) {

param[e.name] = e.value;

});

return param;//自定义需要传递的参数。

},

},

columns: [//对应上面thead里面的序列

{"data": null,"width":"10px"},

{"data": null},

{"data": 'typeNameCn' },

{"data": 'typeNameEn' },

{"data": 'createTime',

"render":function(data,type,full,callback) {

return data.substr(0,19)

}

},

{"data": 'updateTime', defaultContent: "",

"render":function(data,type,full,callback) {

if(data != null && data != ""){

return data.substr(0,19)

}else{

return data;

}

}

},

{"data": null,"width":"60px"}

],

//操作按钮

columnDefs: [

{

targets: 0,

defaultContent: "<input type='checkbox' name='checkList'>"

},

{

targets: -1,

defaultContent: "<div class='btn-group'>"+

"<button id='editRow' class='btn btn-primary btn-sm' type='button'><i class='fa fa-edit'></i></button>"+

"<button id='delRow' class='btn btn-primary btn-sm' type='button'><i class='fa fa-trash-o'></i></button>"+

"</div>"

}

],

language: {

lengthMenu: "",

processing: "<sp:message code='sys.load'/>",

paginate: {

previous: "<",

next: ">",

first: "<<",

last: ">>"

},

zeroRecords: "<sp:message code='sys.nodata'/>",

info: "<sp:message code='sys.pages'/>",

infoEmpty: "",

infoFiltered: "",

sSearch: "<sp:message code='sys.keyword'/>:",

},

//在每次table被draw完后回调函数

fnDrawCallback: function(){

var api = this.api();

//获取到本页开始的条数

   var startIndex= api.context[0]._iDisplayStart;

   api.column(1).nodes().each(function(cell, i) {

     cell.innerHTML = startIndex + i + 1;
   });

}

});

//查询按钮

$("#btn-query").on("click", function () {

tables.fnDraw();//查询后不需要保持分页状态,回首页

});

//添加

$("#btn-add").on("click", function () {

url = "<%=path%>/goodsType/add";

$("input[name=typeId]").val(0);

$("input[name=typeNameCn]").val("");

$("input[name=typeNameEn]").val("");

$("#editModal").modal("show");

});

//批量删除

$("#btn-delAll").on("click", function () {
});

//导出

$("#btn-export").on("click", function () {

});

//刷新

$("#btn-re").on("click", function () {

tables.fnDraw(false);//刷新保持分页状态
});

//checkbox全选

$("#checkAll").on("click", function () {

if ($(this).prop("checked") === true) {

$("input[name='checkList']").prop("checked", $(this).prop("checked"));

//$("#dataTable tbody tr").addClass('selected');

$(this).hasClass('selected')

} else {

$("input[name='checkList']").prop("checked", false);

$("#dataTable tbody tr").removeClass('selected');

}

});



//修改

$("#dataTable tbody").on("click", "#editRow", function ()
{

var data = tables.api().row($(this).parents("tr")).data();

$("input[name=typeId]").val(data.typeIdStr);

$("input[name=typeNameCn]").val(data.typeNameCn);

$("input[name=typeNameEn]").val(data.typeNameEn);

url = "<%=path%>/goodsType/update";

$("#editModal").modal("show");

});

$("#btn-submit").on("click", function(){

$.ajax({

cache: false,

type: "POST",

url: url,

data:$("#editForm").serialize(),

async: false,

error: function(request) {

showFail("Server Connection Error...");

},

success: function(data) {

if(data.status == 1){

$("#editModal").modal("hide");

showSuccess("<sp:message code='sys.oper.success'/>");

tables.fnDraw();

}else{

showFail("<sp:message code='sys.oper.fail'/>");

}

}

});

});

//删除

$("#dataTable tbody").on("click", "#delRow", function () {

var data = tables.api().row($(this).parents("tr")).data();

if(confirm("是否确认删除这条信息?")){

$.ajax({

url:'<%=path%>/goodsType/del/'+data.typeIdStr,

type:'delete',

dataType: "json",

cache: "false",

success:function(data){

if(data.status == 1){

showSuccess("<sp:message code='sys.oper.success'/>");

tables.api().row().remove().draw(false);

}else{

showFail("<sp:message code='sys.oper.fail'/>");

}

},

error:function(err){

showFail("Server Connection Error...");

}

});

}

});

});

</script>

Java代码

Controller处理方法,负责查询页面需要表格数据,格式化Json后返回。

@RequestMapping(value="/goodsType/getData", produces = "text/json;charset=UTF-8")

@ResponseBody

public String getData(HttpServletRequest request, QueryCondition query) {

DatatablesView<GoodsType> dataTable = goodsTypeService.getGoodsTypeByCondition(query);

dataTable.setDraw(query.getDraw());

String data = JSON.toJSONString(dataTable);

return data;

}

返回Json数据格式

{

"data": [{

"createTime": "2016-10-27 09:42:28.0",

"typeId": 96824775296417986,

"typeIdStr": "96824775296417986",

"typeNameCn": "食品",

"typeNameEn": "Foods",

"updateTime": "2016-10-28 13:00:24.0"

},

{

"createTime": "2016-10-27 09:42:27.0",

"typeId": 96824775296417979,

"typeIdStr": "96824775296417979",

"typeNameCn": "汽车",

"typeNameEn": "Cars123",

"updateTime": "2016-10-27 09:51:24.0"

}],

"draw": 1,

"recordsFiltered": 17,

"recordsTotal": 17

}

DatatablesView,根据DataTables需要格式定义

public class DatatablesView<T> {  

    private List<T> data; //data 与datatales 加载的“dataSrc"对应  

private int recordsTotal;

private int recordsFiltered;

private int draw;

public DatatablesView() {

}

public int getDraw() {

return draw;

}
public void setDraw(int draw) {

this.draw = draw;

}

public List<T> getData() {

return data;

}

public void setData(List<T> data) {

this.data = data;

}

public int getRecordsTotal() {

return recordsTotal;

}

public void setRecordsTotal(int recordsTotal) {

this.recordsTotal = recordsTotal;

this.recordsFiltered = recordsTotal;

}

public int getRecordsFiltered() {

return recordsFiltered;

}

public void setRecordsFiltered(int recordsFiltered) {

this.recordsFiltered = recordsFiltered;

}

}

Service业务处理类,主要根据查询条件统计记录数量,查询当前页数据列表

public DatatablesView<GoodsType> getGoodsTypeByCondition(QueryCondition query) {

DatatablesView<GoodsType> dataView = new DatatablesView<GoodsType>();

//构建查询条件

WherePrams where = goodsTypeDao.structureConditon(query);

Long count = goodsTypeDao.count(where);

List<GoodsType> list = goodsTypeDao.list(where);

dataView.setRecordsTotal(count.intValue());

dataView.setData(list);
return dataView;

}

Dao层就是基本的数据库查询操作,这里省略…

结尾

查询条件这里只使用了关键字模糊查询,根据业务需要,可以动态加入其他查询条件,后台需要做相应处理。

基于SpringMVC+Bootstrap+DataTables实现表格服务端分页、模糊查询的更多相关文章

  1. jquery.dataTables的探索之路-服务端分页配置

    最近闲来无事想研究下数据表格,因为之前接触过layui和bootstrap的数据表格,本着能学多少学多少的学习态度,学习下dataTables的服务端分页配置.特与同学们一块分享下从中遇到的问题和解决 ...

  2. BootStrap table服务端分页

    涉及到的内容: 1.bootstrap-table插件: 2.mybatisplus分页查询: 3.spring封装对象匹配bootstrap-table插件格式: 4.sql查询隐藏手机号中间四位. ...

  3. asp.net mvc bootstrap datatable 服务端分页

    datatable 服务端分页 因项目需求变动,需处理大量数据,更改成服务端分页,自己两天的学习笔记 先上图[ jqueryui风格] 前端代码: @{ Layout = null;} <!DO ...

  4. asp.net mvc bootstrap datatable 服务端分页 更新槽糕的代码【1】

    datatable 服务端分页 因项目需求变动,需处理大量数据,更改成服务端分页,自己两天的学习笔记 datatable 1.10.7 百度云下载  密码:0ea1 先上图[ jqueryui风格] ...

  5. BootStrap Table和Mybatis Plus实现服务端分页

    一.后台java代码(Mybatis Plus分页) (1)Mybatis Plus分页的配置,在mybatis的xml文件中增加如下配置(Mybatis Plus官方文档:http://baomid ...

  6. 基于开源SuperSocket实现客户端和服务端通信项目实战

    一.课程介绍 本期带给大家分享的是基于SuperSocket的项目实战,阿笨在实际工作中遇到的真实业务场景,请跟随阿笨的视角去如何实现打通B/S与C/S网络通讯,如果您对本期的<基于开源Supe ...

  7. .net平台 基于 XMPP协议的即时消息服务端简单实现

    .net平台 基于 XMPP协议的即时消息服务端简单实现 昨天抽空学习了一下XMPP,在网上找了好久,中文的资料太少了所以做这个简单的例子,今天才完成.公司也正在准备开发基于XMPP协议的即时通讯工具 ...

  8. 基于TCP协议套接字,服务端实现接收客户端的连接并发

    基于TCP协议套接字,服务端实现接收客户端的连接并发 服务端 import socket from multiprocessing import Process server=socket.socke ...

  9. 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发

    ''' 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发 ''' client import socket import time client = socket.socket() clie ...

随机推荐

  1. 紫书 例题8-3 UVa 1152(中途相遇法)

    这道题要逆向思维, 就是求出答案的一部分, 然后反过去去寻找答案存不存在. 其实很多其他题都用了这道题目的方法, 自己以前都没有发现, 这道题专门考这个方法.这个方法可以没有一直往下求, 可以省去很多 ...

  2. Codecademy网站安利 及 javaScript学习

    今天发现一个Code教学网站,号称可以利用零碎时间来学习些代码. codecademy (https://www.codecademy.com)

  3. STM32 软件模拟 IIC 代码,标准库、HAL库可用

    #ifndef _IIC_H #define _IIC_H #include "stdio.h" #include "stm32f1xx_hal.h" /* 定 ...

  4. strlen()函数对一个未初始化数组的处理

    今天使用strlen时 ,发现一个问题,demo代码如下: #include <stdio.h> #include <stdlib.h> #include <string ...

  5. 【iOS】UICollectionView自己定义Layout之蜂窝布局

    网上的UICollectionView的Layout布局,其cell的形状多为矩形和圆形. 本篇博文将正六边形作为cell的基本形状,为您展现独特的蜂窝布局效果及实现源代码. 帮助您让自己的App脱颖 ...

  6. 16、sockect

    一.局域网因特网 服务器是指提供信息的计算机或程序,客户机是指请求信息的计算机或程序,而网络用于连接服务器与客户机,实现两者之间的通信.但有时在某个网络中很难将服务器和客户机区分开.我们通常说的“局域 ...

  7. 闭包(closure)与协程共用时要注意的事情

    闭包是一种能够让你用非常舒服的方式来编程的小技巧,Go也支持闭包. 假设从来没有接触过闭包,想在一開始就弄懂什么是闭包(closure)是非常困难的,就像递归一样,直到你真正写过.用过它,你才干真正的 ...

  8. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  9. bzoj3993: [SDOI2015]星际战争(网络流)

    3993: [SDOI2015]星际战争 题目:传送门 题解: 洛谷AC了,但是因为bzoj的spj有问题所以暂时没A 一道老题目了,二分时间然后网络流判断. 每次st-->武器连时间*攻击力 ...

  10. m_Orchestrate learning system---十五、如何快速查错

    m_Orchestrate learning system---十五.如何快速查错 一.总结 一句话总结: a.删除代码法 b.添加提示代码法 c.仔细看错误信息 1.评论板块和论坛板块的实时更新? ...