How to connect JSON data to a Kendo network

Using MY WCF Service I expose JSON data:

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

Here is an example of the returned JSON:

{"d": [{"__ type": "Product Details: #NWProducts", "Discount": 0, "OrderId": 10248, "ProductID": 11, "UnitPrice": 14.0000, "Quanity": 12}, {"__ type": "Product Details: #NWProducts", "Discount": 0, "OrderId": 10248, "ProductID": 42, "UnitPrice": 9.8000, "Quanity": 10}, {"__ type": "#NWProducts: Product Details", "discount": 0, "OrderId": 10248, "ProductID": 72, "UnitPrice": 34.8000, "Quanity": 5}, {"__ type ":": #NWProducts Product details "," Discount ": 0," OrderId ": 10249," ProductID ": 14," UnitPrice ": 18.6000," Quanity ": 9}, {" __ type ": "Product Details: #NWProducts "," discount ": 0," OrderId ": 10249," ProductID ": 51," UnitPrice ": 42.4000," Quanity ": 40}

Trying to connect this to a Kendo grid using:

   <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>

For some reason, I cannot see any data in the grid. Maybe something is wrong with the way I am trying to relate my data.

+3
source share
3 answers

The JSON result is the culprit. The kendo data source, by default, searches for a return object to have elements in the array called results. Easy fix. You just need to determine where the data is in the response JSON object.

dataSource: {
    transport: {
        read: {
             url: "http://localhost/KendoServices/Web/GetProductDetails",
             dataType: 'json'
        }
    },
    pageSize: 10,
    schema: {
        data: function(response) {
            return response.d;
        }
    }
},

- Change ... Oops, missed something else. Your type: 'json'must be inside your reading object and must bedataType: 'json'

+2
source

try it

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

Here is how I did it:

    $("#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

0
source

All Articles