问题
I have an example working at JSFiddle.
It has a piece of JSON consumed by a YUI DataTable which I need to create using KnockoutJS bindings. I'm fairly new to KnockoutJS and would like to know how this can be done.
The table should be created under the markup -
<div class="yui3-skin-sam" id="datatable"></div>
The code goes as below -
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();
});
Can this be done using KnockoutJS bidings? I know that an HTML table can be created well using KnockoutJS bindings but I find the YUI part tricky. Please help.
回答1:
I assume you need to add some data-bind attributes to your table cells. YUI table allows you to customize the TD element creation through the use of templates. Take a look at the YUI documentation for more information on this
回答2:
A custom binding helps resolve this problem. I can put the datatable creation code in the custom binding so that it will create he table on the markup from which the binding was called. Cool.
来源:https://stackoverflow.com/questions/12353330/yui-datatable-using-knockoutjs-bindings