Example is deployed on Google App Engine, datatablesjee.appspot.com. Code is available on GitHub github.com/rafalrusin/datatablesjee.
First, we need to instantiate DataTables plugin within a html page. This is extremely easy, by using code below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$('#example').dataTable( { | |
"bProcessing": true, | |
"bServerSide": true, | |
"bPaginate": true, | |
"sAjaxSource": "datatablesjee" | |
} ); | |
} ); |
Now, we need to implement backend. Servlet requires a doGet method, which needs to retrieve parameters sent from client as an Ajax request. Those parameters describe search keyword, starting row and page size of a table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
int iDisplayStart = Integer.parseInt(request.getParameter("iDisplayStart")); | |
int iDisplayLength = Integer.parseInt(request.getParameter("iDisplayLength")); | |
String sSearch = request.getParameter("sSearch"); | |
final int iSortCol_0 = Integer.parseInt(request.getParameter("iSortCol_0")); | |
final boolean sSortDir_0 = request.getParameter("sSortDir_0").equals("asc"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean filter(String sSearch, List<Object> list) { | |
if (sSearch.equals("")) { | |
return true; | |
} | |
return list.toString().contains(sSearch); | |
} | |
... | |
List<List<Object>> filteredList = new ArrayList<List<Object>>(); | |
for (int i = 0; i < myList.size(); i++) { | |
if (filter(sSearch, myList.get(i))) { | |
filteredList.add(myList.get(i)); | |
} | |
} | |
Collections.sort(filteredList, new Comparator<List<Object>>() { | |
@Override | |
public int compare(List<Object> o1, List<Object> o2) { | |
if (sSortDir_0) { | |
return o1.get(iSortCol_0).toString().compareTo(o2.get(iSortCol_0).toString()); | |
} else { | |
return o2.get(iSortCol_0).toString().compareTo(o1.get(iSortCol_0).toString()); | |
} | |
} | |
}); | |
List<List<Object>> selectedList = new ArrayList<List<Object>>(); | |
for (int i = iDisplayStart; i < iDisplayStart + iDisplayLength; i++) { | |
if (i < filteredList.size()) { | |
if (filter(sSearch, filteredList.get(i))) { | |
selectedList.add(filteredList.get(i)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ObjectMapper mapper = new ObjectMapper(); | |
result.put("iTotalRecords", myList.size()); | |
result.put("iTotalDisplayRecords", filteredList.size()); | |
result.put("aaData", selectedList); | |
mapper.writeValue(response.getOutputStream(), result); |
Summing up, I like the idea of Ajax in this form, because client side is not rendered directly by backend (no jsp, etc.). This makes it a detached view, which could be served as static content, from Apache Web Server for example, which is very performant.
I am happy to find this website very useful for me, as it contains lot of information.
ReplyDelete