so refer to http://datatables.net/usage/server-side, there are some para we must pass from client to server.
1) on the client side,
oTBExample2 = $("#example2").dataTable({
"bProcesing" : true,
"bServerSide" : true,
"bLenthChange" : false,
"iDisplayLength" : 10,
"sAjaxSource" : "users2.do",
"aoColumns" : [
{
"sTitle" : "User ID",
"mData" : "userid"
},
{
"sTitle" : "Password",
"mData" : "password"
},
{
"sTitle" : "Date & Time",
"mData" : "timeStamp"
},
{
"mData" : "checked",
"fnRender" : function(obj) {
if (obj.aData.checked == "1")
return '<input type="checkbox" name="aCheckBox" class="dt_checked" checked value="'+obj.aData.checked+'\"/>';
return '<input type="checkbox" name="aCheckBox" class="dt_checked" value="'+obj.aData.checked+'\"/>';
}
},
{
"mData" : null,
"sClass" : "center",
"sDefaultContent" : '<a href="" class="editor_remove" >Delete</a>'
} ],
"fnServerData" : function(sSource, aoData, fnCallback) {
$.ajax({
"dataType" : 'json',
"type" : "GET",
"url" : sSource,
"data" : aoData,
"success" : fnCallback
});
},
"sPaginationType" : "full_numbers"
});// dataTable
<table id="example2" cellpadding="0" cellspacing="0" border="0"
class="display datatable">
<thead>
<tr>
<th width="20%">userid</th>
<th width="20%">password</th>
<th width="20%">time</th>
<th width="10%">selected</th>
<th width="10%">Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
2) on server side,
@RequestMapping(value = "/users2", produces = "application/json")
public @ResponseBody
String showUser(@RequestParam int iDisplayStart,
@RequestParam int iDisplayLength, @RequestParam int sEcho) {
String method="showUser";
info(method,"para0---"+iDisplayStart);
info(method,"para1---"+iDisplayLength);
info(method,"para2---"+sEcho);
DataTablesTO<Account> dt = new DataTablesTO<Account>();
List<Account> accts = accountService.getAccounts(iDisplayStart,iDisplayLength);
List<Account> accts2 = accountService.getAccounts();
dt.setAaData(accts); // this is the dataset reponse to client
dt.setiTotalDisplayRecords(accts2.size()); // // the total data in db for datatables to calculate page no. and position
dt.setiTotalRecords(accts2.size()); // the total data in db for datatables to calculate page no.
dt.setsEcho(sEcho);
return toJson(dt);
}
private String toJson(DataTablesTO<?> dt){
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(dt);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
need jackson jar files to support json conversion,
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class DataTablesTO<T> implements java.io.Serializable{
private static final long serialVersionUID = -8220588043068200705L;
private List<T> aaData;
private int sEcho;
private Integer iTotalRecords;
private Integer iTotalDisplayRecords;
...... getter and setter.......
}
3) hibernate query
public List<Account> getAccounts(int currPosition, int pageSize) {
return sessionFactory.getCurrentSession().createQuery("from Account").setMaxResults(pageSize).setFirstResult(currPosition).list();
}
current position which means the first row to be shown on the page in the table.
4) actual result on page
5) this is the request data when call ajax
6) right now, the datatables does not support order by column, if click the second column to order by it,let's check the request data.
so, we also need to change the hql as follows
from Account order by ${iSortCol_0}+1 ${sSortDir_0}
Thank you for good post. Can you please post code as well.
ReplyDeletewhich part?
DeletePlz post it as a downloadable project
Deletei am working on spring, hibernate, datatables integration, so my database contains lots of records so while accessing it staking time to load, so what i thought is like get 10 pages...if i click on next then the request should go to controller and get the remaining 10 pages... i stuck up here... plz help me.......
ReplyDeleteMy table is like 3 tabs each tab contains one table...
yes, what the post describe is to resolve your issues, pls take note
Deletepublic List getAccounts(int currPosition, int pageSize)
this is specially for pagination.
How to pass pagination info from jDataTables to servlet, pls take note this bean,
public class DataTablesTO implements java.io.Serializable{
private static final long serialVersionUID = -8220588043068200705L;
private List aaData;
private int sEcho;
private Integer iTotalRecords;
private Integer iTotalDisplayRecords;
...... getter and setter.......
}
I am trying to call my Spring MVC controller from a JSP and getting following error on browser
ReplyDelete"The request sent by the client was syntactically incorrect ()"
please help me.
Thanks in advance.
Do not have enough info, can you refer to this post
Deletehttp://forum.springsource.org/showthread.php?98200-Spring-The-request-sent-by-the-client-was-syntactically-incorrect
Hi, I have integrated Spring, Hibernate and DataTables in several projects. I have written a howto specific for Spring Roo but it can be easily applied to a normal Spring MVC app. There is also a link to the whole project in github.
ReplyDeletehttp://www.pabloguerrero.org/cgblog/2/15/DataTables-server-side-processing-integration-with-Spring-Roo-and-Hibernate
Hope it helps somebody
Hi,
ReplyDeleteCan you please post the source code of this tutorial ?
Thanks
Hi Wang Xiang's
ReplyDeleteit is good tutorial , I want to get source code of you this example ,
can you put your code to every can get it ?
thanks
hi ,can you post the example with all source code means js,css (with downloaded option) with an spring and hibernate with jquery pagination data table I want to call ajax with every page request,means 10 record fetch from db with select 10 and with next . plz help me
ReplyDeleteIt does not work as described.
ReplyDeleteplease download the source code
ReplyDeleteWhile running this app - Required int parameter 'iDisplayStart' is not present
ReplyDeleteThanks a lot , it worked as described , have had few minor issues but after reading the whole article i fixed the issues.
ReplyDeletenote : to fix the above program , just add sEcho : 1 , iDisplayStart : 0 parameters along with provided parameters as below .
"bProcesing" : true,
"bServerSide" : true,
"bLenthChange" : false,
"iDisplayLength" : 10,
"iDisplayStart": 0,
"sEcho":1,
"sAjaxSource" : "users2.do",
i am not able to search records. can you help me?
Deletei am not able to search the data. can you please help me?
ReplyDeleteI found that the json data getting from page is string type, String showUser() -> DataTablesTO showUser()
ReplyDeleteThis comment has been removed by the author.
ReplyDelete