5

YUI DataTable を使用して、次の JSON オブジェクトを表示しようとしています。YUI DataTable で、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 を使用しました。今回は、ヘッダーのみの空の 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();
});
4

2 に答える 2

1

ネストされた値を解析するには、datasource-jsonschema を使用する必要があります。この例を参照してください: http://yuilibrary.com/yui/docs/datatable/datatable-dsget.html

Y.DataSource.Get を Y.DataSource.IO に置き換えて、これらの手順に従うことができるはずです。

于 2011-09-22T16:21:53.680 に答える
1

フォーラムでの私の投稿に続いて、 YUIフォーラムから解決策を得ました。

これが私のために働いたコードです:

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

これが、DataTable と DataSource に苦労している他の誰かに役立つことを願っています。

于 2011-09-28T06:23:15.253 に答える