I'm trying to display the following JSON object using YUI DataTable. I was able to show the lastName, firstName, startDate, employeeCode, employeeStatus successfully in YUI DataTable. But I couldn't show the values in the inner object. In the columnset I tried user.userId and it is displayed as {value} in the DataTable.
[
{
"lastName": "MyLastName",
"firstName": "MyFirstName",
"startDate": "11-11-11",
"employeeCode": "124",
"employeeStatus": "Permanent",
"user": {
"key": {
"name": null,
"parent": {
"name": null,
"parent": null,
"id": 855,
"namespace": "",
"complete": true,
"kind": "Employee"
},
"id": 856,
"namespace": "",
"complete": true,
"kind": "Users"
},
"salt": null,
开发者_如何转开发 "userId": "myemail@example.com",
"status": true,
},
{
...
}
]
Here is the Javascript code:
<script type="text/javascript">
YUI().use("jsonp", 'sortable', 'json-parse', 'datatable', "datatable-sort", "io", "node", function(Y) {
var nestedCols = [
{key : "employeeCode",label : "Employee Code", sortable:true},
{key : "firstName", label : "First Name",sortable: true},
{key : "lastName", label : "Last Name", sortable:true},
{key : "user.userId", label : "Email Id"},
];
Y.io('/Employee/AjaxList', {
on : {
success : function(tx, r) {
var data = Y.JSON.parse(r.responseText);
var table = new Y.DataTable.Base({
columnset : nestedCols,
recordset : data,
}).plug(Y.Plugin.DataTableSort);
table.render("#empTable");
}
}
});
});
</script>
is there anything wrong with this code snippet? How can I show the value of user.userId in DataTable?
Note: The JSON is generated using Jackson and application is developed in GAE/J
UPDATE:
I used DataSource following the suggestion by @Luke. This time I got an empty DataTable with only the headers. Here is the code snippet.
YUI().use("datasource-get", "datatable-base", "datatable-datasource","datasource-arrayschema", function (Y) {
var url = "/Employee/AjaxList?";
var dataSource, table;
dataSource = new Y.DataSource.Get({ source: url });
dataSource.plug(Y.Plugin.DataSourceArraySchema, {
schema: {
resultFields: ["firstName", "lastName"]
}
});
var cols = ["firstName", "lastName"];
table = new Y.DataTable.Base({
columnset: cols,
});
table.plug(Y.Plugin.DataTableDataSource, { datasource: dataSource });
table.render("#empTable");
table.datasource.load();
});
You'll need to use datasource-jsonschema to parse out the nested values. See this example: http://yuilibrary.com/yui/docs/datatable/datatable-dsget.html
You should be able to follow those steps, replacing Y.DataSource.Get with Y.DataSource.IO
I got the solution from YUI Forums following my post in the forum: http://yuilibrary.com/forum/viewtopic.php?f=92&t=8685
Here is the code which worked for me:
YUI().use( "datasource-io", "datasource-jsonschema", "datatable-base", "datatable-datasource", "datatable-scroll",
function (Y) {
var cols = [
{ key: "name", label: 'Name' },
{ key: "email", label: "Email" },
{ key: "user.username", label: 'Username' },
{ key: "user.password", label: 'Password' },
];
car url = "/Employee/AjaxList";
var ds = new Y.DataSource.IO( {
source:url
});
ds.plug(Y.Plugin.DataSourceJSONSchema, {
schema: {
resultFields: [ 'name', 'email', 'user.username', 'user.password' ],
}
});
var dt = new Y.DataTable.Base({
columnset:cols } )
.plug(Y.Plugin.DataTableDataSource, {datasource:ds});
dt.render("#dtable");
dt.datasource.load();
});
Hope this helps somebody else who is struggling with DataTable & DataSource.
精彩评论