ServerSide processing with java
ServerSide processing with java
I'm working on writing java code to handle the server side processing of my datable and I'm making pretty good progress on it. One thing I don't understand is why paging does not work. When the table displays I see "Showing 1 to 10 of 10 entries (filtered from 221 total entries)", even though there is no filtering being applied, as far as I can tell. When I change the number of entries to display, via the pull down, it works fine, the table adds or removes the rows as needed. But the paging icons are still disabled and the footer stills shows the "filtered" text.
Here's my table header
[code]
Firm ID
Firm Name
[/code]
Here's the table definition
[code]
$('#firmtable').dataTable({
bJQueryUI: true,
"bProcessing": true,
"bDeferRender": true,
bAutoWidth: false,
"aaSorting": [[ 1, "asc" ]],
"aoColumns": [
{"mData": "column0", "bSearchable": false, "bVisible": true, "bSortable": false,"sWidth": "1%"},
{"mData": "firmid", "sWidth": "12%"},
{"mData": "firmname", "sWidth": "75%"},
{"mData": "column3", "bSearchable": false, "bVisible": true, "bSortable": false,"sWidth": "1%"}
],
"oLanguage": {
"sEmptyTable": "Firm table is loading, please wait. "
},
bServerSide: true,
sAjaxSource: "./FirmServlet?action=alltest",
sServerMethod: "POST"
} );
[/code]
And here's what I have in java so far, please note I'm just getting started on this so there is a lot left to do:
[code]
StringBuffer sql1 = new StringBuffer("select firm_id, firm_name from private_rates.firms order by firm_id");
PreparedStatement stmt = conn.prepareStatement(sql1.toString());
ResultSet result = stmt.executeQuery();
JSONObject returnObj = new JSONObject();
JSONArray array = new JSONArray();
int row = 0;
int idisplaylength = Integer.parseInt(request.getParameter("iDisplayLength"));
while (result.next()) {
if(row <= idisplaylength) {
JSONObject obj = new JSONObject();
int firmid=result.getInt("firm_id");
obj.put("column0","");
obj.put("firmid", firmid);
obj.put("firmname", result.getString("firm_name"));
obj.put("column3", "");
array.add(obj);
}
row++;
}
result.close();
stmt.close();
returnObj.put("iTotalRecords", row);
returnObj.put("iTotalDisplayRecords", idisplaylength);
returnObj.put("sEcho", Integer.parseInt(request.getParameter("sEcho")));
returnObj.put("aaData", array);
out.print(returnObj.toJSONString());
out.flush();
[/code]
Clearly I'm missing something, probably something obvious.
If anyone has some java code they would be willing to share that would be great. Java because I'm pretty clueless with php and that seems to be what the examples use.
Thanks a lot.
Here's my table header
[code]
Firm ID
Firm Name
[/code]
Here's the table definition
[code]
$('#firmtable').dataTable({
bJQueryUI: true,
"bProcessing": true,
"bDeferRender": true,
bAutoWidth: false,
"aaSorting": [[ 1, "asc" ]],
"aoColumns": [
{"mData": "column0", "bSearchable": false, "bVisible": true, "bSortable": false,"sWidth": "1%"},
{"mData": "firmid", "sWidth": "12%"},
{"mData": "firmname", "sWidth": "75%"},
{"mData": "column3", "bSearchable": false, "bVisible": true, "bSortable": false,"sWidth": "1%"}
],
"oLanguage": {
"sEmptyTable": "Firm table is loading, please wait. "
},
bServerSide: true,
sAjaxSource: "./FirmServlet?action=alltest",
sServerMethod: "POST"
} );
[/code]
And here's what I have in java so far, please note I'm just getting started on this so there is a lot left to do:
[code]
StringBuffer sql1 = new StringBuffer("select firm_id, firm_name from private_rates.firms order by firm_id");
PreparedStatement stmt = conn.prepareStatement(sql1.toString());
ResultSet result = stmt.executeQuery();
JSONObject returnObj = new JSONObject();
JSONArray array = new JSONArray();
int row = 0;
int idisplaylength = Integer.parseInt(request.getParameter("iDisplayLength"));
while (result.next()) {
if(row <= idisplaylength) {
JSONObject obj = new JSONObject();
int firmid=result.getInt("firm_id");
obj.put("column0","");
obj.put("firmid", firmid);
obj.put("firmname", result.getString("firm_name"));
obj.put("column3", "");
array.add(obj);
}
row++;
}
result.close();
stmt.close();
returnObj.put("iTotalRecords", row);
returnObj.put("iTotalDisplayRecords", idisplaylength);
returnObj.put("sEcho", Integer.parseInt(request.getParameter("sEcho")));
returnObj.put("aaData", array);
out.print(returnObj.toJSONString());
out.flush();
[/code]
Clearly I'm missing something, probably something obvious.
If anyone has some java code they would be willing to share that would be great. Java because I'm pretty clueless with php and that seems to be what the examples use.
Thanks a lot.
This discussion has been closed.
Replies
Here is my java code, in case anyone needs an example:
[code]
int displaystart = Integer.parseInt(request.getParameter("iDisplayStart"));
int displaylength = Integer.parseInt(request.getParameter("iDisplayLength"));
StringBuffer sql1 = new StringBuffer("select firm_id, firm_name from private_rates.firms ");
if(request.getParameter("sSearch") != null) {
String search = request.getParameter("sSearch");
sql1.append("where lower(firm_name) like('%");
sql1.append(search.toLowerCase());
sql1.append("%') or firm_id like('%");
sql1.append(search);
sql1.append("%') ");
}
if(request.getParameter("iSortCol_0") != null) {
int sortcol = Integer.parseInt(request.getParameter("iSortCol_0"));
if(sortcol == 1)
sql1.append("order by firm_id ");
else
sql1.append("order by firm_name ");
if(request.getParameter("sSortDir_0") != null)
sql1.append(request.getParameter("sSortDir_0"));
}
PreparedStatement stmt = conn.prepareStatement(sql1.toString());
ResultSet result = stmt.executeQuery();
JSONObject returnObj = new JSONObject();
JSONArray array = new JSONArray();
int row = 0;
while (result.next()) {
JSONObject obj = new JSONObject();
int firmid=result.getInt("firm_id");
obj.put("checkbox","");
obj.put("firmid", firmid);
obj.put("firmname", result.getString("firm_name"));
obj.put("icon", "");
row++;
if(row >= displaystart && row < (displaystart + displaylength))
array.add(obj);
}
result.close();
stmt.close();
returnObj.put("sEcho", Integer.parseInt(request.getParameter("sEcho")));
returnObj.put("iTotalRecords", row);
returnObj.put("iTotalDisplayRecords", row);
returnObj.put("aaData", array);
out.print(returnObj.toJSONString());
out.flush();
[/code]