using datatables in java web application
using datatables in java web application
jkab016
Posts: 4Questions: 0Answers: 0
It is my first time I am using datatables although I am not new to jQuery.
Now I have tried in vain to implement paging and sorting of data in my table although I seem to be doing everything as stated in your documentation. My references to the scripts is also right. what could be the problem?
[code]
<!--this is my reference to the scripts both the JS and CSS-->
Test
<!--Styles -->
<!--javascript-->
test
<!--this is my javaSrcipt code calling a servlet method below-->
function getCounties() {
var action = "getCounties";
searchAlert("Alert", "Loading. Please wait.....");
$.ajax({
url: "County",
type: "post",
data: {
action: action
},
dataType: 'json',
success: function(msg) {
if (msg.success) {
clearAdmin();
successAlert("Alert", msg.success);
clearAdmin();
$("#views").html(msg.payload).show('normal');
$("#county_table").dataTable();//county_edit is a table fetched using a servlet method
}
else {
$("#views").hide('fast');
errorAlert("Alert", msg.error);
}
},
error: function(msg) {
}
});
}
<!--this is a servlet method-->
protected void getCounties(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
String payload = "";
try {
CountyManager cm = new CountyManager();
List cnt = cm.getCounties();
if (cnt.size() > 0) {
payload += ""
+ ""
+ ""
+ "Counties"
+ "Edit"
+ "Divisions"
+ ""
+ ""
+ "";
for (Counties c : cnt) {
payload += ""
+ "" + c.getName() + ""
+ ""
+ ""
+ ""
+ "";
}
payload += "";
json.put("success", "Counties retrieved successfully");
json.put("payload", payload);
out.println(json);
} else {
json.put("error", "No counties were found");
out.println(json);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
[/code]
Now I have tried in vain to implement paging and sorting of data in my table although I seem to be doing everything as stated in your documentation. My references to the scripts is also right. what could be the problem?
[code]
<!--this is my reference to the scripts both the JS and CSS-->
Test
<!--Styles -->
<!--javascript-->
test
<!--this is my javaSrcipt code calling a servlet method below-->
function getCounties() {
var action = "getCounties";
searchAlert("Alert", "Loading. Please wait.....");
$.ajax({
url: "County",
type: "post",
data: {
action: action
},
dataType: 'json',
success: function(msg) {
if (msg.success) {
clearAdmin();
successAlert("Alert", msg.success);
clearAdmin();
$("#views").html(msg.payload).show('normal');
$("#county_table").dataTable();//county_edit is a table fetched using a servlet method
}
else {
$("#views").hide('fast');
errorAlert("Alert", msg.error);
}
},
error: function(msg) {
}
});
}
<!--this is a servlet method-->
protected void getCounties(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
String payload = "";
try {
CountyManager cm = new CountyManager();
List cnt = cm.getCounties();
if (cnt.size() > 0) {
payload += ""
+ ""
+ ""
+ "Counties"
+ "Edit"
+ "Divisions"
+ ""
+ ""
+ "";
for (Counties c : cnt) {
payload += ""
+ "" + c.getName() + ""
+ ""
+ ""
+ ""
+ "";
}
payload += "";
json.put("success", "Counties retrieved successfully");
json.put("payload", payload);
out.println(json);
} else {
json.put("error", "No counties were found");
out.println(json);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
[/code]
This discussion has been closed.
Replies
Like I say, this might not be all, but it will at least give you cleaner code.
there any other initialization i have to make for it to start working other than calling the .datTable(); method on the table i want to implement the datatable?
Allan
Is there any other initialization that I have to make besides the .datatable(); method on the Javascript and ensuring the references to the scripts is right?
> Is there any other initialization that I have to make besides the .datatable(); method on the Javascript and ensuring the references to the scripts is right?
No. I'd check your browser's console to see if there are any errors as the first step.
Allan
also I did not close the element in the table header.
I'm Thankful for all the help and suggestions