AJAX source - array of objects with sub-object

AJAX source - array of objects with sub-object

evil_evilevil_evil Posts: 6Questions: 1Answers: 0
edited April 2014 in General
Hi,

Can datatable handle an array of object with 1 column that is a sub-onject?

I've read the example with sub-array, and i was wondering if is possible to do something like this:
(see the "details" filed, it has braces not square brackets)

[code]
$(document).ready(function() {
var oTable = $('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "sources/objects_subarrays.txt",
"aoColumns": [
{ "mData": "engine" },
{ "mData": "browser" },
{ "mData": "platform" },
{ "mData": "details.0" },
{ "mData": "details.1" }
]
} );
} );


{
"aaData": [
{
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"details": {
id_details: "4",
name_details: "X"
}
}
....
[/code]

Why i ask you this? Because i have an entity on my db, for example "engine", that has a field that is a foreign key to an other table, for example "details"..

the db structure is:

[code]
Table Engine:
engine char
browser char
platform char
id_details integer

Table Details:
id integer
name char
[/code]

Assuming the db structure above, i've created 2 java class, one for "engine" and one for "details". The "engine" class have the filed id_details defined as "details class".

when i send the json to the datatable, the "details" filed it's formatted with braces because it's an object and not a string.

Cna datatable handle this object with a sub-object?
Any suggestion on how handle this?

Kind Regards,

Stefano.

Replies

  • evil_evilevil_evil Posts: 6Questions: 1Answers: 0
    Any suggestion?

    has anyone used an object with sub-object?



    for further clarity this is my class:

    [code]
    public class UserBean {

    private int id;
    private String name;
    private String surname;
    private String email;
    private String login;
    private String plaintextPwd;
    private boolean valid;
    private int id_profile;
    }

    public class ProfileBean {

    private Integer id;
    private String name;
    }
    [/code]


    and from the servlet i decote the UserBean into a json:

    [code]
    ......
    List userList = new ArrayList();
    userList = UserDaoBean.getAll();

    String json = gson.toJson(userList);
    .......
    [/code]

    and in the jsp page:
    [code]
    oTable = $("#mytable").dataTable( {
    "bProcessing": false,
    "bServerSide": false,
    "sAjaxSource": "../PopulateTableUser",
    "bJQueryUI": true,
    "sServerMethod": "POST",
    "sScrollX": "100%",
    "sScrollXInner": "110%",
    "bScrollCollapse": true,
    "aoColumns": aryJSONColTable
    });
    [/code]

    in the aryJSONColTable i've defined the column as:

    [code]
    [
    {
    "mData": "id",
    "sTitle": "ID",
    "bVisible": false,
    "bSearchable": false,
    "aTargets": [
    0
    ]
    },
    {
    "mData": "name",
    "sTitle": "Nome",
    "bVisible": true,
    "bSearchable": true,
    "aTargets": [
    0
    ]
    },
    {
    "mData": "surname",
    "sTitle": "Cognome",
    "bVisible": true,
    "bSearchable": true,
    "aTargets": [
    0
    ]
    },
    {
    "mData": "email",
    "sTitle": "EMail",
    "bVisible": true,
    "bSearchable": true,
    "aTargets": [
    0
    ]
    },
    {
    "mData": "login",
    "sTitle": "User Login",
    "bVisible": true,
    "bSearchable": true,
    "aTargets": [
    0
    ]
    },
    {
    "mData": "id_profile.0",
    "sTitle": "ID",
    "bVisible": false,
    "bSearchable": false,
    "aTargets": [
    0
    ]
    },
    {
    "mData": "id_profile.1",
    "sTitle": "Name",
    "bVisible": true,
    "bSearchable": true,
    "aTargets": [
    0
    ]
    }
    ]
    [/code]

    Kind Regards,

    Stefano.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Yes it is perfectly possible. Just use Javascript dot notation as you have, but access the sub parameter you want:

    [code]
    details.id_details
    [/code]

    There is an example here: http://datatables.net/release-datatables/examples/ajax/deep.html

    Allan
  • ashiersashiers Posts: 101Questions: 8Answers: 7
    Since you are on the Java platform, you might be interested in going to the JED website. It has many examples on how to use DataTables on the Java platform. http://jed-datatables.ca/jed/
This discussion has been closed.