0

我想模板化使用 KnockoutJS 数据绑定创建 YUI3 数据表。假设我有以下 JSON 字符串 -

{ "@lang": "en-US",

"cartItem": {
    "@lang": "en-US",
    "cartId": "14",
    "items": [
        {
            "@lang": "en-US",
            "id": "5",
            "name": "global/network/main",
            "propertyURL": "mail.yahoo.com",
            "rootPropertyName": "All Other",
            "spaceId": "2023407535"
        },
        {
            "@lang": "en-US",
            "id": "5",
            "name": "global/network/main",
            "propertyURL": "mail.yahoo.com",
            "rootPropertyName": "All Other",
            "spaceId": "2023407535"
        }
    ]
},
"status": {
    "@lang": "en-US",
    "message": "",
    "status": "success"
}

}

我正在使用以下代码创建使用上述 JSON 的 YUI 数据表 -

YUI().use("datatable", "datatable-datasource", "datasource-local", "datasource-jsonschema", function(Y) {

    var jsonString = Y.one("#jsondata").getHTML();

    var dtsource = new Y.DataSource.Local({source:jsonString});
    dtsource.plug(Y.Plugin.DataSourceJSONSchema, {
        4schema: {
            resultListLocator: "cartItem.items",
            resultFields: [
            // Re-map the "id" member to "rid" ...    
                { key:"rid", locator:"id"},
                "name", "propertyURL", "rootPropertyName"
            ]
        }
    });

    var colums = ["rid", "name", "propertyURL", "rootPropertyName"];

    var dtable = new Y.DataTable({  
        columns: colums,
        summary:"Shopping cart items",
        caption:"Table with JSON data from api"});

    dtable.plug( Y.Plugin.DataTableDataSource,{
        datasource:dtsource
    });

    dtable.render("#jsonapi-datatable");
    dtable.datasource.load({request:"&id=14"});        
});

如何为上述代码创建 KnockoutJS 模板?

小提琴 - http://jsfiddle.net/pPY7K/6/

4

1 回答 1

0

使用 KnockoutJS 自定义绑定,然后使用该绑定创建模板。

于 2012-11-20T12:50:10.833 回答