2011-09-21 78 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