我有一个如下所示的 JSON 数据源:
var productDataSource = new kendo.data.DataSource({
transport: {
read: {
url: 'http://...',
dataType: "json"
}
},
pageSize: 10
});
并返回如下内容:
{
"ProdSet1":[
{
"Name": "Product 1-1",
"Price": 20,
"Quantity": 50,
"Change": 4
},
{
"Name": "Product 1-2",
"Price": 14,
"Quantity": 74,
"Change": 5
}
],
"ProdSet2":[
{
"Name": "Product 2-1",
"Price": 15,
"Quantity": 12,
"Change": 2
}
]
}
然后我有多个网格使用这个数据源:
$("#prodSet1").kendoGrid({
dataSource: productDataSource,
columns: [
{ field: "ProdSet1[0].Name", title: "Name" },
{ field: "ProdSet1[0].Price", title: "Price" },
{ field: "ProdSet1[0].Quantity", title: "Quantity" },
{ field: "ProdSet1[0].Change", title: "Change" }
]
});
$("#prodSet2").kendoGrid({
dataSource: productDataSource,
columns: [
{ field: "ProdSet2[0].Name", title: "Name" },
{ field: "ProdSet2[0].Price", title: "Price" },
{ field: "ProdSet2[0].Quantity", title: "Quantity" },
{ field: "ProdSet2[0].Change", title: "Change" }
]
});
但是 { field: "ProdSet1[0].Name"...}
不起作用。
如何引用正确的产品数据?
最佳答案
由于集合在返回对象中命名,您可以为每个 ProdSet 设置 schema.data 属性,并将网格绑定(bind)到它。
我会使用 datasource.read() 手动从数据源获取数据
var datafromService = productDataSource.read();
文档... http://docs.telerik.com/kendo-ui/documentation/api/framework/datasource#methods-read
然后将每个网格绑定(bind)到该 datafromService,每个网格指定要绑定(bind)到的 JSON 对象内的集合。
$("#prodSet1").kendoGrid({
dataSource: {
data: datafromService,
schema: {
data: 'ProdSet1'
}
},
columns: [
{ field: "Name", title: "Name" },
{ field: "Price", title: "Price" },
{ field: "Quantity", title: "Quantity" },
{ field: "Change", title: "Change" }
]
});
和
$("#prodSet2").kendoGrid({
dataSource: {
data: datafromService,
schema: {
data: 'ProdSet2'
}
},
columns: [
{ field: "Name", title: "Name" },
{ field: "Price", title: "Price" },
{ field: "Quantity", title: "Quantity" },
{ field: "Change", title: "Change" }
]
});
现在它们将绑定(bind)到同一组数据,只是显示来自 JSON 数据的不同集合。
查看示例... http://jsbin.com/dokub/1/edit
如果您需要完整的 CRUD 操作,那就另当别论了。
关于javascript - 剑道UI : one multi-level JSON dataSource for multiple grids,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22310440/