2011-09-21 65 views
5

我想使用YUI DataTable显示下面的JSON对象。我能够在YUI DataTable中成功显示lastName,firstName,startDate,employeeCode,employeeStatus。但是我无法在内部对象中显示值。在列集中,我尝试了user.userId,它在DataTable中显示为{value}在YUI中显示JSON对象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": "[email protected]", 
       "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和应用程序在GAE/J开发


UPDATE:

我使用的DataSource通过@Luke建议以下。这一次我得到一个只有头文件的空DataTable。这是代码片段。

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

回答

1

更换Y.DataSource.Get我从YUI论坛解决以下我在论坛上发帖:http://yuilibrary.com/forum/viewtopic.php?f=92&t=8685

这里是为我工作的代码:

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

希望这有助于别人谁与数据表&数据源挣扎。

1

你需要使用的数据源,jsonschema解析出嵌套值。看到这个例子:http://yuilibrary.com/yui/docs/datatable/datatable-dsget.html

你应该能够遵循这些步骤,Y.DataSource.IO

+0

我尝试使用数据源,但现在看来YUI发送错误的请求到服务器。这里是我从萤火虫控制台得到的URL:http:// localhost:8080/ExampleApp/Employee/AjaxListundefined&callback = YUI.Env.DataSource.callbacks.yui_3_4_0_1_1316763699238_127正确的网址是http:// localhost:8080/ExampleApp/Employee/AjaxList – libregeek

+0

请忽略上述评论,我通过追加'?'来解决此问题。在源URL的末尾。但数据仍然没有在数据表中出现。我可以在Firebug控制台中看到JSON数据。 – libregeek

+0

我用脚本使用datasource-arrayschema更新了问题。我相信我得到的响应是一个数组,我无法识别datasource-jsonschema的resultListLocator – libregeek