Skip to content

Instantly share code, notes, and snippets.

@rafalrusin
Created December 17, 2011 23:02
Show Gist options
  • Save rafalrusin/1491712 to your computer and use it in GitHub Desktop.
Save rafalrusin/1491712 to your computer and use it in GitHub Desktop.
DataTables JEE
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));
}
}
}
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");
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"bPaginate": true,
"sAjaxSource": "datatablesjee"
} );
} );
ObjectMapper mapper = new ObjectMapper();
result.put("iTotalRecords", myList.size());
result.put("iTotalDisplayRecords", filteredList.size());
result.put("aaData", selectedList);
mapper.writeValue(response.getOutputStream(), result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment