Kendo UI Grid JSON DataSource无法加载数据

时间:2013-08-14 02:48:53

标签: json kendo-grid

出于某种原因,我似乎无法在Kendo UI Grid中获得以下内容:

grid-image

HTML:

<div id="grid"></div>
<script>
    var remoteDataSource = new kendo.data.DataSource(
    {
        transport:
        {
            read: {
                type: "POST",
                dataType: "json",
                url: "/home/getopportunities/"
            }
        },
        pageSize: 4
    })
    $("#grid").kendoGrid(
        {
            dataSource: remoteDataSource,
            columns: [
                {
                    title: "Title",
                    headerAttributes: {
                        style: "text-align:center"
                    },
                    attributes: {
                        "class": "table-cell"
                    },
                    width: 600,
                    filterable: true
                },
                {
                    title: "Activity Type",
                    headerAttributes: {
                    },
                    attributes: {
                        "class": "table-cell",
                        style: "text-align:center"
                    },
                    width: 100,
                    filterable: true
                },
                {
                    title: "Specialty",
                    filterable: true,
                    headerAttributes: {
                        style: "text-align:center"
                    },
                    attributes: {
                        "class": "table-cell",
                        style: "text-align:center"
                    }
                },
            {
                title: "Total Credits",
                format: "{0}",
                headerAttributes: {
                    style: "text-align:center"
                },
                attributes: {
                    "class": "table-cell",
                    style: "text-align:center"
                }
            }
        ],
        height: 430,
        scrollable: true,
        sortable: true,
        pageable: true,
        filterable: {
            extra: false,
            operators: {
                string: {
                    contains: "Contains",
                    startswith: "Starts with",
                    eq: "Is equal to",
                    neq: "Is not equal to"
                },
                number: {
                    eq: "Is equal to",
                    neq: "Is not equal to",
                    gte: "Greater Than",
                    lte: "Less Than"
                }
            }
        }
    });
</script>

这是返回给它的JSON:

[
{"ActivityID":367,"Title":"Non Webinar Test For Calendar","ActivityType":"Other","TotalCredits":2,"Specialty":"[AB] [AE]"},
{"ActivityID":370,"Title":"Stage - Test SI Changes Part II","ActivityType":"Other","TotalCredits":2,"Specialty":"[NE]"},
{"ActivityID":374,"Title":"Webinar Test Event For Calendar","ActivityType":"Webinar","TotalCredits":2,"Specialty":"[FE] [NE] "},
{"ActivityID":401,"Title":"Module #1 Webinar: Learn Stuff","ActivityType":"Webinar","TotalCredits":2,"Specialty":"[AB] ",},
{"ActivityID":403,"Title":"Module #3 Webinar: Learn Even More Stuff","ActivityType":"Webinar","TotalCredits":2,"Specialty":"[AB] [AE]",}
]

我觉得我很亲近但却错过了最后一块。当我在截止日期前,任何帮助都会非常感激。

6 个答案:

答案 0 :(得分:3)

常见的问题是缺少架构属性! 将它添加到grid的 - datasource中,并在创建json时检查它是否已设置。

(当plain数组被序列化/ to_json时,数据数组需要一个指示shema的属性)

这是一个明确的例子:

js:示例网格初始化/数据源:

$("#grid").kendoGrid({ dataSource: { transport: { read: "/getdata/fromthisurl" }, schema: { data: "data" } } });

当你输出/输出你的json时,看看shema信息是否在编码结果中:

php:

 $somedata= get_my_data();

 header("Content-type: application/json");
 echo "{\"data\":" .json_encode($somedata). "}";

或:

     $viewdata['data'] = get_my_data();

     header("Content-type: application/json");
     echo (json_encode($viewdata));

所以发送到网格的json看起来像:

{data:
      [
       {item}
       {item}
      ]
}

而不只是:

[
  {item}
  {item}
]

答案 1 :(得分:1)

代码看起来不错。我想知道你是否改变数据源创建如下。将类型从POST更改为GET并查看其是否有效,

var remoteDataSource = new kendo.data.DataSource(
    {
        transport:
        {
            read: {
                type: "GET",
                dataType: "json",
                url: "/home/getopportunities/"
            }
        },
        pageSize: 4
    })

答案 2 :(得分:1)

试试这个,

  $(document).ready(function () {
var remoteDataSource = new kendo.data.DataSource(
    {
        transport:
        {
            read: {
                type: "POST",
                dataType: "json",
                url: "/home/getopportunities/"
            }
        },
        pageSize: 4
    });
});

答案 3 :(得分:1)

您可以在某些调试工具中查看代码的哪个部分引发异常(我建议使用Chrome的DevTools(只需按Chrome中的F12键)。

我很确定问题是在网格的列数组中错误field属性,因此Kendo不知道数据源中的哪些数据要显示在哪个网格列中。

columns: [
                {
                    field: "Title", // attr name in json data
                    title: "Title", // Your custom title for column (it may be anything you want)
                    headerAttributes: {
                        style: "text-align:center"
                    },
                    attributes: {
                        "class": "table-cell"
                    },
                    width: 600,
                    filterable: true
                },

不要忘记将请求类型从“POST”更改为“GET”。

答案 4 :(得分:0)

这是不洁净的,我偶然发现了它,但对我有用的是从控制器而不是Json(dataList)返回Json(Json(dataList))。

答案 5 :(得分:0)

What I found when inspecting the JSON coming back from the grid datasource json query was the field names were being JavaScripted -- what was ActivityID in C# became activityID on the wire...