0

I have setup a datatables based on the following html

  <script>
       $('#tableListing').dataTable({
        "bAutoWidth" : true,
        "bProcessing" : true,
        "bServerSide" : true,
        "sAjaxSource" : '${actionUrl}',
        "aoColumns":[ 
                       { "sTitle": "ID", "mData": "id" },
                       { "sTitle": "Username", "mData": "userName" },
                       { "sTitle": "Password", "mData": "password" }
                     ]

            }
    });
    </script>

<table id="tableListing" class="display datatable">
            <thead>
                <tr>
                    <th><spring:message code="columnLabel.id" /></th>
                    <th><spring:message code="columnLabel.username" /></th>
                    <th><spring:message code="columnLabel.enabled" /></th>
                </tr>
            </thead>
</table>

And the JSON response from server is [1,"abiieez",true] and it works nicely. However I have changed the response from server into something like:

*Edit (updated JSON response) *

{
    "iTotalDisplayRecords": 11,
    "iTotalRecords": 11,
    "aaData": "[{\"creationTime\":0,\"enabled\":true,\"id\":1,\"loginDuration\":0,\"online\":false,\"password\":\"asdasd\",\"userName\":\"abiieez\"},{\"creationTime\":0,\"enabled\":false,\"id\":105,\"loginDuration\":0,\"online\":false,\"password\":\"asdasd\",\"userName\":\"asd1212123\"},{\"creationTime\":0,\"enabled\":false,\"id\":106,\"loginDuration\":0,\"online\":false,\"password\":\"asdsdasd\",\"userName\":\"asdasd23123\"},{\"creationTime\":0,\"enabled\":false,\"id\":107,\"loginDuration\":0,\"online\":false,\"password\":\"asdsdasd\",\"userName\":\"asdasd23123\"},{\"creationTime\":0,\"enabled\":false,\"id\":108,\"loginDuration\":0,\"online\":false,\"password\":\"anak jalanan\",\"userName\":\"alibaba90\"},{\"creationTime\":1351338218227,\"enabled\":false,\"id\":109,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338263527,\"enabled\":false,\"id\":110,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338265078,\"enabled\":false,\"id\":111,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338266329,\"enabled\":false,\"id\":112,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338267247,\"enabled\":false,\"id\":113,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"}]",
    "sEcho": "1"
}

The above json is not accepted by the datatables, I believe because the number of columns do not match.

Is there a way for me to retrieve this json object and pick which columns I would like to show on the tables on the client side ?

1 Answer 1

3

You can use mData property in aoColumns like this:

 $(document).ready(function() {
            $('#tableListing').dataTable({
               "aoColumns":[ 
                             { "sTitle": "user", "mData": "userName" },
                             { "sTitle": "pass", "mData": "password" },
                             ...
                           ]
                "bAutoWidth" : true,
                "bProcessing" : true,
                "bServerSide" : true,
                "sAjaxSource" : '${actionUrl}'
            });
    });
Sign up to request clarification or add additional context in comments.

7 Comments

Do I have put sColumns on the JSON response from server ? Do i still need to specify the 3 column in html as well ? I still have the error "Requested uknown parameter 'id' from the data source for row 0".
You should specify "aoColumns" in html during initialization of table.
I still facing the same error. I have updated my datatable initialization.
if you use server side, your return should be like this: { "aaData": [{'id': value, 'userName': value, 'password': value}] }
Added my JSON response from server. I used JSONLint to validate my JSON response.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.