can't generate tadatables on client side
can't generate tadatables on client side
Hello,
I have trouble to generate a datatables
I retrieve my data from mysql on a servlet and send it to client side with gson like this:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
try (PrintWriter out = response.getWriter()) {
DataService ds = new DataService();
Collection licencesList = null;
try {
licencesList = ds.getLicences()
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(licencesList );
response.getWriter().write(json);
request.setAttribute("json", json);
in debug mode I can see that the json wich is send to the client contain the data and I can see it with the console of firefox on the client side.
The probleme is that I the client side with firefox & chrome shows something like this:
pk
serialNumber "sn11111"
software "mysoftwareName"...
instead of a datatables.
Here is my jsp:
<!DOCTYPE html>
<html>
<head>
<title>Redlog Server</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/r/bs-3.3.5/jqc-1.11.3,dt-1.10.8/datatables.min.js"></script>
</head>
<body> ~~~~
<br /><br />
<div class="container">
<table id="data-table" class="table table-bordered">
<thead>
<tr>
<th>serial Number</th>
<th>soft</th>
</tr>
</thead>
</table>
</div>
</body>
</html>
<script>
$(document).ready(function() {
$('#data-table').DataTable( {
"bProcessing": false,
"bServerSide": false,
"sAjaxSource": "../LicenceServlet",
"bJQueryUI": true,
"aoColumns": [
{ "data" : "serialNumber"},
{ "data" : "software"},
]
});
});
}
</script>
I don't understand why the client does't generate a Datatables...
I'm new on Java and JS, sorry if my question may sound stupid.
This question has an accepted answers - jump to answer
Answers
Can you post the actual JSON that is returned?
Or use the debugger and post the debug link generated?
The JSON is expected to be returned in a specific format described here:
https://datatables.net/manual/data/
Kevin
http://debug.datatables.net/iveqim
Go to the Tables tab and click on Server Interaction. You will see
HTTP 404 – Not Found
imbedded within the message. Maybe the url "../LicenceServlet" is not valid.Kevin
Thanks for that! I change the url and It seems like the HTTP 404 not found disapears but the Datatables still does't appear.
Here is the new debug link if you want:
http://debug.datatables.net/awafus
Some records have "exeDownloadedScript", but others don't. That field is not mentioned in your "columns" definition.
DataTables expects each row's columns to be consistently the same.
Looks like you have this for your column config:
In general the JSON for each row has this data structure:
It looks like some of your records don't have all the fields. This particular one is missing
installPath
. You need to provide all the fields even if blank in the JSON.You are not returning the JSON in a
data
object as described here:https://datatables.net/manual/ajax
Since you are using DT 1.10.8 you will need to change your ajax config to allow you to use the JSON without a
data
object. You will need to change to something like the 3rd option here:https://datatables.net/manual/ajax#Data-array-location
Maybe this:
Look up
ajax.dataSrc
.Since
serialNumber
andsoftware
are in thepk
object you will need to change your column config to this:Here is an example:
https://datatables.net/examples/ajax/deep.html
Click on the AJAX tab to see the JSON data structure.
This should get you going in the right direction.
@allan I noticed the debug output shows DT 1.10.8 is up to date.
Kevin
Thanks! I'll look into what is causing that.
Allan
Thanks a lot for these answers!
I made the changes from kthorngren's suggestions and it seems to work!
The datatables appears and is populated!
I still have the trouble with some of the records wich don't have all the fields...
I can't change the data from the database (it's not mine) I hope to find a way to fixe that!
fixed with the use of "defaultContent" on my column config like this:
{
"mData": "installPath",
"defaultContent": "<i></i>"
},
Thanks