can't generate tadatables on client side

can't generate tadatables on client side

guillaume.lheureux.son@gmail.comguillaume.lheureux.son@gmail.com Posts: 15Questions: 6Answers: 1
edited August 2017 in Free community support

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

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    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

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    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

  • guillaume.lheureux.son@gmail.comguillaume.lheureux.son@gmail.com Posts: 15Questions: 6Answers: 1

    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

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited August 2017

    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.

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918
    edited August 2017 Answer ✓

    Looks like you have this for your column config:

    "aoColumns": [{
            "mData": "serialNumber"
        }, {
            "mData": "software"
        }, {
            "mData": "customer"
        }, {
            "mData": "name"
        }, {
            "mData": "validityDate"
        }, {
            "mData": "installPath"
        }, {
            "mData": "toUpdate"
        }]
    

    In general the JSON for each row has this data structure:

        "pk": {
            "serialNumber": "/2VKWF72/CN129635CK05A8/",
            "software": "SIMTANK"
        },
        "customer": "PCIS",
        "name": "Caroline Bidon",
        "validityDate": "déc. 31, 2099",
        "installPath": "C:\\Application\\SimTank\\",
        "toUpdate": ""
    },
    

    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.

     {
        "pk": {
            "serialNumber": "/8P9HLC2/CN1296367M07AF/",
            "software": "SIMTANK"
        },
        "customer": "Guerlain",
        "name": "Paulo Dinis",
        "validityDate": "déc. 31, 2099",
        "toUpdate": ""
    },
    

    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:

        ajax: {
            url: 'http://localhost:8080//RedlogServer3/datatables',
            dataSrc: ''
        },
    

    Look up ajax.dataSrc.

    Since serialNumber and software are in the pk object you will need to change your column config to this:

    "aoColumns": [{
            "mData": "pk.serialNumber"
        }, {
            "mData": "pk.software"
        }, {
            "mData": "customer"
        }, {
            "mData": "name"
        }, {
            "mData": "validityDate"
        }, {
            "mData": "installPath"
        }, {
            "mData": "toUpdate"
        }]
    

    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

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin

    I noticed the debug output shows DT 1.10.8 is up to date.

    Thanks! I'll look into what is causing that.

    Allan

  • guillaume.lheureux.son@gmail.comguillaume.lheureux.son@gmail.com Posts: 15Questions: 6Answers: 1
    edited August 2017

    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!

  • guillaume.lheureux.son@gmail.comguillaume.lheureux.son@gmail.com Posts: 15Questions: 6Answers: 1
    edited August 2017

    fixed with the use of "defaultContent" on my column config like this:

    {
    "mData": "installPath",
    "defaultContent": "<i></i>"
    },

  • guillaume.lheureux.son@gmail.comguillaume.lheureux.son@gmail.com Posts: 15Questions: 6Answers: 1
    edited August 2017

    Thanks

This discussion has been closed.