Rendering JSON data

Rendering JSON data

HarishHarish Posts: 2Questions: 0Answers: 0
edited January 2012 in DataTables 1.8
Hi Everyone,

Below is the code that I'm having to use the datatable being initialized using JSON data.

HTML Page - DataTable section
[code]


Users In The System




Username
First Name
Last Name
Acid
Last Login




[/code]

The JavaScript code is given below. For some reason if I give userData for aaData then the table doesn't get initialized, however, if the JSON is directly given like in the below code it works fantastic. Please suggest how to make it work with userData.

[code]
$(document).ready(function() {
$.ajax({
url : "http://localhost/wlife/v1/user/active",
type : "GET",
data : "uid=" + readCookie("uid") + "&key=" + readCookie("key"),
success : function(data) {
alert(JSON.stringify(data));

var packet = jQuery.parseJSON(JSON.stringify(data));

var userData = JSON.stringify(packet["tUsers"]);

alert(userData);

$("#usertable").dataTable({
"aaData": [{"acid":"ccd6687e-32ab-11e1-af7e-992b0cb8782f","firstname":"Rajendra","lastlogin":"2012-01-18 10:51:27.0","lastname":"Kulkarni","username":"rk@viaone.in"},{"acid":"ccd6687e-32ab-11e1-af7e-992b0cb8782f","firstname":"Jonathan","lastlogin":"2012-01-18 10:51:27.0","lastname":"Keshrew","username":"harish@viaone.in"}],

"aoColumns": [
{ "sTitle": "Account ID", "mDataProp": "acid" },
{ "sTitle": "First Name", "mDataProp": "firstname" },
{ "sTitle": "Last Login", "mDataProp": "lastlogin" },
{ "sTitle": "Last Name", "mDataProp": "lastname" },
{ "sTitle": "User Name", "mDataProp": "username" }
]
});
},
error : function(data){
location.replace("500.html");
}
});
});

[/code]

JSON emitted for the REST GET call is given below
[code]
[{"acid":"ccd6687e-32ab-11e1-af7e-992b0cb8782f","firstname":"Rajendra","lastlogin":"2012-01-18 10:51:27.0","lastname":"Kulkarni","username":"rk@viaone.in"},{"acid":"ccd6687e-32ab-11e1-af7e-992b0cb8782f","firstname":"Jonathan","lastlogin":"2012-01-18 10:51:27.0","lastname":"Keshrew","username":"harish@viaone.in"}]
[/code]

Replies

  • HarishHarish Posts: 2Questions: 0Answers: 0
    Got this to work all that I had to do was to change the aaData value to packet["tUsers"] everything works like magic
This discussion has been closed.