using datatables in java web application

using datatables in java web application

jkab016jkab016 Posts: 4Questions: 0Answers: 0
edited February 2014 in DataTables 1.9
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]

Replies

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    I can't say if this the solution, but you don't need both dataTables js files - use one or the other. Also I would always be loading jquery.js before dataTables js.
    Like I say, this might not be all, but it will at least give you cleaner code.
  • jkab016jkab016 Posts: 4Questions: 0Answers: 0
    thanks tangerine but unfortunately that aint the answer still. Is
    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?
  • ashiersashiers Posts: 101Questions: 8Answers: 7
    You might want to check out the JED website. It has many examples you can try while using DataTables on the Java platform. http://jed-datatables.ca/jed/
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    I think we'd probably need to see the page to be able to offer any help I'm afraid. With tangerines suggested changes, the code looks okay - but there is obviously something going wrong...

    Allan
  • jkab016jkab016 Posts: 4Questions: 0Answers: 0
    Alright I don't seem able to post a snapshot of the page here or maybe you could advise how to post a snapshot of the page.
    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?
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    You can use JSFiddle, CodePen or http://live.datatables.net to create a test case.

    > 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
  • jkab016jkab016 Posts: 4Questions: 0Answers: 0
    solved. Had two versions of jQuery, maybe there was a conflict somehow.
    also I did not close the element in the table header.

    I'm Thankful for all the help and suggestions
This discussion has been closed.