2011-09-21 13 views
5

Próbuję wyświetlić następujący obiekt JSON przy użyciu YUI DataTable. Udało mi się pokazać LastName, firstName, startDate, employeeCode, employeeStatus w YUI DataTable. Ale nie mogłem pokazać wartości w wewnętrznym przedmiocie. W zestawie kolumn próbowałem user.userId i jest wyświetlany jako {wartość} w DataTable.Wyświetl obiekt JSON w YUI 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, 
}, 
{ 
... 
} 
] 

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

jest coś nie w porządku z tym fragmencie kodu? Jak wyświetlić wartość user.userId w DataTable?

Uwaga: JSON jest generowana z Jackson i stosowania został opracowany w gae/J


UPDATE:

że stosowane źródło danych zgodnie z sugestią @Luke. Tym razem dostałem pusty DataTable z tylko nagłówkami. Oto fragment kodu.

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

Odpowiedz

1

Mam roztwór z Yui forum po moim poście na forum: http://yuilibrary.com/forum/viewtopic.php?f=92&t=8685

Oto kod, który pracował dla mnie:

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

Nadzieja pomaga to ktoś inny, kto zmaga się z DataTable & DataSource.

1

Będziesz musiał użyć źródła danych-jsonschema, aby przeanalizować zagnieżdżone wartości. Zobacz poniższy przykład: http://yuilibrary.com/yui/docs/datatable/datatable-dsget.html

Powinieneś być w stanie śledzić te czynności, zastępując Y.DataSource.Get z Y.DataSource.IO

+0

Próbowałem użyć źródła danych, ale teraz wydaje się, że YUI wysyła złe żądanie do serwera. Oto adres URL, który otrzymałem z konsoli firebug: http: // localhost: 8080/ExampleApp/Employee/AjaxListundefined & callback = YUI.Env.DataSource.callbacks.yui_3_4_0_1_1316763699238_127 Prawidłowy adres URL to http: // localhost: 8080/ExampleApp/Employee/AjaxList – libregeek

+0

Zignoruj ​​powyższy komentarz. Rozwiązałem ten problem, dodając "?" na końcu źródłowego adresu URL. Ale nadal dane nie są objaśniane w datatable. Widzę dane JSON w konsoli firebug. – libregeek

+0

Mam zaktualizowane pytanie ze skryptem przy użyciu datasource-arrayschema. Wierzę, że odpowiedź, którą otrzymuję, jest tablicą i nie mogłem zidentyfikować resultListLocator dla źródła danych-jsonschema – libregeek