This is a migrated thread and some comments may be shown as answers.

Grid bound to json data always empty

4 Answers 464 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 29 Mar 2012, 10:18 PM
Hi, All;

I'm trying to build a simple grid bound to json data. The grid shows up but it's always empty. Help?
<div id=grid>
</>
<script type="text/javascript">
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "http://localhost:3223/DataServices/DomainService.svc/JSON/GetDATA",
                dataType: "json"
            }
        },
        schema: {
            data: "RootResults"
        }
    });
    dataSource.read();
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#grid").kendoGrid({
            dataSource: dataSource,
            columns: [
                { title: "ID", field: "ID" },
                { title: "CODE1", field: "CODE1" },
                { title: "CODE2", field: "CODE2" }
            ],
            height: 360,
            groupable: false,
            sortable: false,
            pageable: false,
            scrollable: false
        });
    });
</script>

4 Answers, 1 is accepted

Sort by
0
David
Top achievements
Rank 1
answered on 29 Mar 2012, 11:05 PM
OK, a lot of people must be rolling their eyes at me! I've touched this up so it has at least some chance of working, but now I just get the spinning icon over the grid forever...
<div id=grid>
</div>
<script type="text/javascript">
        $(document).ready(function () {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "http://localhost:3223/DataServices/DomainService.svc/JSON/GetDATA",
                        dataType: "json"
                    }
                },
                schema: {
                    data: "RootResults"
                }
            });
            $("#grid").kendoGrid({
                dataSource: dataSource,
                autobind: true,
                columns: [
                    { title: "ID", field: "ID" },
                    { title: "CODE1", field: "CODE1" },
                    { title: "CODE2", field: "CODE2" }
                ],
                height: 360,
                groupable: false,
                sortable: false,
                pageable: false,
                scrollable: false
            });
        });
    </script>
0
David
Top achievements
Rank 1
answered on 29 Mar 2012, 11:29 PM
Could it have something to do with the json file delivered by a .net domain service? Here's the start of the file...

{"GetDATAResult":{"TotalCount":99,"RootResults":[{"ID":1,"CODE1":"aaa","CODE2":"        "},{"ID":2,"CODE1":"bbb","CODE2":null}, ...

I'm specifying the data as "RootResults". Do I need to tell it to drill down from GetDATAResult to find the array?
0
Alexander Valchev
Telerik team
answered on 30 Mar 2012, 08:20 AM
Hi David,

In the data function of the schema you have to specify the array that contains your data. Please try with the following code:
schema: {
    data: "GetDATAResult.RootResults",
    //in order to retrieve total number of records - that would be required if you decide to turn on paging
    total: "GetDATAResult.TotalCount",
    //it is recommended to define the fields
    model: {
        fields: {
            ID: { type: "number" },
            CODE1: { type: "string" },
            CODE2: { type: "string" }
        }
    }
}



Greetings,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
David
Top achievements
Rank 1
answered on 30 Mar 2012, 03:54 PM
Thanks!

I had actually written it exactly that way without knowing the syntax to follow the json tree, but I thought "there's no way I guessed it right" so I never bothered to run it. :-(
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Alexander Valchev
Telerik team
Share this question
or