javascript - 在 YUI 数据表中显示 JSON 对象

标签 javascript json yui yui-datatable

我正在尝试使用 YUI DataTable 显示以下 JSON 对象。我能够在 YUI 数据表中成功显示 lastName、firstName、startDate、employeeCode、employeeStatus。但是我无法显示内部对象中的值。在列集中,我尝试了 user.userId,它在 DataTable 中显示为 {value}

[    
{
            "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,
},
{
...
}
]

这是 Javascript 代码:

<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>

这段代码有什么问题吗?如何在 DataTable 中显示 user.userId 的值?

注意:JSON 是使用 Jackson 生成的,应用程序是在 GAE/J 中开发的


更新:

我按照@Luke 的建议使用了DataSource。这次我得到了一个只有标题的空数据表。这是代码片段。

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();
});

最佳答案

您需要使用 datasource-jsonschema 来解析嵌套值。请参阅此示例:http://yuilibrary.com/yui/docs/datatable/datatable-dsget.html

您应该能够按照这些步骤操作,将 Y.DataSource.Get 替换为 Y.DataSource.IO

关于javascript - 在 YUI 数据表中显示 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7496481/

相关文章:

javascript - 为什么浏览器不遵循使用 XMLHTTPRequest 和 CORS 的重定向?

javascript - AngularJS 如何在使用 controller-as 语法时注入(inject)依赖项

php - 使用json连接android与php和mysql : Not working

javascript - 将 <img class ="image"> 替换为 <span class = "image"> 使用 YUI3 或 Javascript,无 jQuery

javascript - 如何让YUI更轻更快?

javascript - jQuery.ajax() 不触发处理程序

javascript - 这个脚本有可能被通用化吗?

javascript - Ajax 调用 Express 应用程序不传递数据

json - JSON.parse Map希望String获取整数

javascript - YUI 3 : Getting the selected option's text