[英]How to bind JSON data with Kendo Grid
使用MY WCF服务我公开JSON数据:
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
List<ProductDetails> GetProductDetails();
以下是返回的JSON示例:
{ “d”:[{ “__类型”: “产品详细:#NWProducts”, “折扣”:0 “的OrderId”:10248 “产品编号”:11, “单价”:14.0000 “控管数量”:12},{ “__type”: “产品详细:#NWProducts”, “折扣”:0 “的OrderId”:10248 “产品编号”:42, “单价”:9.8000 “控管数量”:10},{ “__类型”:“产品详细: #NWProducts”, “折扣”:0 “的OrderId”:10248 “产品编号”:72, “单价”:34.8000 “控管数量”:5},{ “__类型”: “产品详细:#NWProducts”, “折扣” :0 “的OrderId”:10249 “产品编号”:14, “单价”:18.6000 “控管数量”:9},{ “__类型”: “产品详细:#NWProducts”, “折扣”:0 “的OrderId”: 10249 “产品编号”:51, “单价”:42.4000 “控管数量”:40}
尝试使用以下方法将其绑定到Kendo Grid:
<script>
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: {
type: "json",
transport: {
read: "http://localhost/KendoServices/Web/GetProductDetails"
},
pageSize: 10
},
groupable: true,
sortable: true,
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
columns: [{
field: "OrderId",
title: "OrderId",
width: 140
}, {
field: "ProductId",
title: "ProductId",
width: 190
}, {
field: "UnitPrice",
title: "UnitPrice"
}, {
field: "quanity",
width: 110
}]
});
});
</script>
出于某种原因,我无法在网格上看到任何数据。 我试图绑定数据的方式可能有问题。
由此产生的JSON是罪魁祸首。 默认情况下,kendo dataSource查找返回对象以使数组中的项称为results。 很容易修复。 只需要定义响应JSON对象中数据的位置。
dataSource: {
transport: {
read: {
url: "http://localhost/KendoServices/Web/GetProductDetails",
dataType: 'json'
}
},
pageSize: 10,
schema: {
data: function(response) {
return response.d;
}
}
},
- 编辑......哎呀,错过了别的什么。 你的type: 'json'
应该在你的read对象里面,应该是dataType: 'json'
尝试这个
dataSource: {
transport: {
read: {
url: "http://localhost/KendoServices/Web/GetProductDetails",
contentType: 'application/json; charset=utf-8',
dataType: "json"
}
},
schema: {
data: "d"
}
}
}
这就是我做到的方式:
$("#grid").kendoGrid({
dataSource: {
transport: {
read: {
url : pUrl,
dataType: "json"
}
},
pageSize:40,
schema: {
data: function(response) {
return response.json;
}
}
},
height: 550,
groupable: false,
sortable: true,
pageable: {
refresh: false,
pageSizes: false,
buttonCount: 5
},
columns: [
{
field: "SEQ_NO",
title: "No",
filterable: false,
width: 120
}, {
field: "LOT_NO",
title: "Lot No (INS' No)"
}, {
field: "TYPE",
title: "INPUT (At 100% Burden)"
}, {
field: "ATTRIBUTE01",
title: "1.0 In"
}, {
field: "ATTRIBUTE02",
title: "2.0 In"
}, {
field: "ATTRIBUTE03",
title: "0.05 In"
}, {
field: "RESILT",
title: "RESILT"
}
]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.