0

我有一个在JSFiddle工作的例子。

它有一段由 YUI DataTable 使用的 JSON,我需要使用 KnockoutJS 绑定来创建它。我对 KnockoutJS 相当陌生,想知道如何做到这一点。

该表应在标记下创建 -

<div class="yui3-skin-sam" id="datatable"></div>​

代码如下 -

YUI().use('datatable', 'datasource-local', 'datasource-jsonschema',function(Y){
var data  = {"generations": [{
    "type": "Modern",
    "showName": "The Modern Generation",
    "imgURI": "file:/D:/projectGen.png",
    "relations": {
        "father": {
            "age": "49",
            "firstName": "George",
            "lastName": "Mathews",
            "priority": "1",
            "profession": "Service"
        },
        "mother": {
            "age": "47",
            "firstName": "Susan",
            "lastName": "Aldrin",
            "priority": "2",
            "profession": "Home-Maker"
        },
        "brother": {
            "age": "28",
            "firstName": "Martin",
            "lastName": "Mathews J",
            "priority": "3",
            "profession": "Engineer"
        },
        "sister": {
            "age": "23",
            "firstName": "Laura",
            "lastName": "Mathews J",
            "priority": "4",
            "profession": "Fashion Model"
        },
        "aunt": {
            "age": "50",
            "firstName": "Sally",
            "lastName": "Bekkers",
            "priority": "5",
            "profession": "Singer"                
        }
    }
 }]
};
var localDataSource = new Y.DataSource.Local({source:data});
var dynamicColumns = Y.Object.keys(data.generations[0].relations);
var config = {
    schema: {
        resultListLocator: 'generations',
        resultFields: [{key:'showName'}]
    }
};

Y.Array.each(dynamicColumns,function(key){
    config.schema.resultFields.push({
        key: key,
        locator: "relations."+key + ".firstName" 
    });
});

var jsonSchema = localDataSource.plug(Y.Plugin.DataSourceJSONSchema, config);
console.log(config.schema.resultFields); 
var table = new Y.DataTable({
    columns: config.schema.resultFields
});

table.plug(Y.Plugin.DataTableDataSource, {
    datasource: localDataSource
});

table.render('#datatable');
table.datasource.load();

}); ​</p>

这可以使用 KnockoutJS 投标来完成吗?我知道使用 KnockoutJS 绑定可以很好地创建 HTML 表,但我发现 YUI 部分很棘手。请帮忙。

4

2 回答 2

0

我假设您需要向表格单元格添加一些数据绑定属性。YUI 表允许您通过使用模板自定义 TD 元素的创建。查看YUI 文档以获取更多信息

于 2012-09-12T02:07:18.690 回答
0

自定义绑定有助于解决此问题。我可以将数据表创建代码放在自定义绑定中,以便它将在调用绑定的标记上创建表。凉爽的。

于 2012-11-20T12:03:09.317 回答