jquery<--json-->spring(3.0)之DataTables的服务器端翻页
本文是jquery<--json-->spring(3.0)系列的第三篇。
jquery与yui相比,感觉写法很简洁,但也有个问题是配套的jquery ui中的页面控件
不是很全,很多要借助plugin,而这些plugin良莠不齐,选用时要自己好好挑一挑。
列表是一个常用的页面控件,我选了半天发现DataTables这款用的人比较多,就试了
试服务器端翻页功能,现总结如下。
DataTables的主页是http://www.datatables.net/
先上个这个demo的截图,设想的操作是输入客户名称,按检索后进行模糊检索进行分
页显示,每页显示8条记录。
1 页面部分
使用DataTables时,html需要如下书写,其中tfoot部分是表表格的下部标题,可以不
要。
<table id="customerInfo"><thead><tr><th>ID</th> 略<th>身高</th></tr></thead><tbody><tr><td colspan="8"></td> </tr></tbody><tfoot><tr><th>ID</th> 略<th>身高</th></tr></tfoot></table>
$('#customerInfo').dataTable();
function retrieveData( sSource, aoData, fnCallback ) { //将客户名称加入参数数组aoData.push( { "name": "customerName", "value": $("#customerName").val() } );$.ajax( {"type": "POST", "contentType": "application/json","url": sSource, "dataType": "json","data": JSON.stringify(aoData), //以json格式传递"success": function(resp) {fnCallback(resp.returnObject); //服务器端返回的对象的returnObject部分是要求的格式}});}
var oTable = null;$(function() {$("#customerInfo").hide();} );//“检索”按钮的处理函数function search() {if (oTable == null) { //仅第一次检索时初始化Datatable$("#customerInfo").show();oTable = $('#customerInfo').dataTable( {"bAutoWidth": false,//不自动计算列宽度"aoColumns": [//设定各列宽度{"sWidth": "15px"},{"sWidth": "80px"},{"sWidth": "160px"},{"sWidth": "110px"},{"sWidth": "120px"},{"sWidth": "140px"},{"sWidth": "140px"},{"sWidth": "*"}],"bProcessing": true,//加载数据时显示正在加载信息"bServerSide": true,//指定从服务器端获取数据"bFilter": false,//不使用过滤功能"bLengthChange": false,//用户不可改变每页显示数量"iDisplayLength": 8,//每页显示8条数据"sAjaxSource": "customerInfo/search.do",//获取数据的url"fnServerData": retrieveData,//获取数据的处理函数"sPaginationType": "full_numbers",//翻页界面类型"oLanguage": {//汉化"sLengthMenu": "每页显示 _MENU_ 条记录","sZeroRecords": "没有检索到数据","sInfo": "当前数据为从第 _START_ 到第 _END_ 条数据;总共有 _TOTAL_ 条记录","sInfoEmtpy": "没有数据","sProcessing": "正在加载数据...","oPaginate": {"sFirst": "首页","sPrevious": "前页","sNext": "后页","sLast": "尾页"}}});}//刷新Datatable,会自动激发retrieveDataoTable.fnDraw();}
public class JSONParam {private String name;private String value;//略}
@RequestMapping(value = "/search", method = RequestMethod.POST)@ResponseBodypublic JSONResponse search(@RequestBody JSONParam[] params){//略}
public class DataTableReturnObject {private long iTotalRecords;private long iTotalDisplayRecords;private String sEcho;private String[][] aaData;public DataTableReturnObject(long totalRecords, long totalDisplayRecords, String echo, String[][] d) {//略}//略}
@RequestMapping(value = "/search", method = RequestMethod.POST)@ResponseBodypublic JSONResponse search(@RequestBody JSONParam[] params) throws IllegalAccessException, InvocationTargetException //convertToMap定义于父类,将参数数组中的所有元素加入一个HashMapHashMap<String, String> paramMap = convertToMap(params);String sEcho = paramMap.get("sEcho");String customerName = paramMap.get("customerName");int start = Integer.parseInt(paramMap.get("iDisplayStart"));int length = Integer.parseInt(paramMap.get("iDisplayLength"));//customerService.search返回的第一个元素是满足查询条件的记录总数,后面的是//页面当前页需要显示的记录数据List<Object> customerList = customerService.search(customerName, start, length);Long count = (Long)customerList.get(0);//将查询结果转换为一个二维数组int record = customerList.size() - 1;String[][] data = new String[record][];for(int i=0; i<record; i++) {Customer customer = (Customer)customerList.get(i+1);JSONCustomer jsonCustomer = new JSONCustomer();BeanUtils.copyProperties(jsonCustomer, customer);data[i] = jsonCustomer.toArray();}return successed(new DataTableReturnObject(count.longValue(), count.longValue(), sEcho, data));}