如何使用Kendo Grid绑定JSON数据

时间:2014-02-20 12:24:30

标签: c# json wcf kendo-ui kendo-grid

使用MY WCF服务我公开了JSON数据:

 [OperationContract]
 [WebGet(ResponseFormat=WebMessageFormat.Json)]
 List<ProductDetails> GetProductDetails();

以下是返回的JSON示例:

  

{ “d”:[{ “__类型”: “产品详细:#NWProducts”, “折扣”:0 “的OrderId”:10248 “产品编号”:11, “单价”:14.0000 “控管数量”:12 },{ “__类型”: “产品详细:#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>

出于某种原因,我无法在网格上看到任何数据。我试图绑定数据的方式可能有问题。

3 个答案:

答案 0 :(得分:2)

结果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'应该在您的阅读对象中,并且应该是dataType: 'json'

答案 1 :(得分:0)

试试这个

dataSource: {
    transport: {
            read: {
                   url: "http://localhost/KendoServices/Web/GetProductDetails",
                   contentType: 'application/json; charset=utf-8',
                   dataType: "json"
                  }
    },
    schema: {
                 data: "d"
            }
    }
}

答案 2 :(得分:0)

我就是这样做的:

    $("#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"
            }
        ]
    });

Code Result Example