Kendo Treeview和阅读网址

本文关键字:Treeview Kendo | 更新日期: 2023-09-27 18:27:50

问题:使用层次数据源将Kendo Treeview绑定到远程数据源时出现问题

对象不支持属性或方法"级别"

我认为这个问题与我的json响应有关,因为它是用一大堆scape字符编码的;我不知道如何强制Transport.Read.Url使用$.parseJson(data)。我所能做的就是在控制器中向它提供我的操作的Url;但我的控制器的结果是纯json,有很多scape字符。。。请帮助

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
[HttpPost]
public JsonResult TreeItems(int? jobId, int? parentJobId)
{
    var data = new List<object>()
    {
        new
        {
            NodeId = 1,
            caption = "test1",
            hasChildren = true,
            items = new List<object>()
            {
                new
                {
                    NodeId = 2,
                    caption = "test2",
                    hasChildren = false,
                    parentId = "1"
                },
                new
                {
                    NodeId = 3,
                    caption = "test3",
                    hasChildren = false,
                    parentId = "1"
                },
            }
        }
    };
    return Json(JsonConvert.SerializeObject(data), JsonRequestBehavior.AllowGet);
}

JavaScript:

var ds1 = new kendo.data.HierarchicalDataSource(
{
    transport:
    {
        read:
        {
            url: urlToTreeItems
            , dataType: "json"
            , type: "POST"
        },
        schema:
        {
            model:
            {
                id: "id",
                text: "caption",
                hasChildren: "hasChildren"
            }
        },
    }
});
var tv = $("#test").kendoTreeView(
{
    dataSource: ds1
}).data("kendoTreeView");

响应:

"[{'"NodeId'":1,'"caption'":'"test1'",'"hasChildren'":true,'"items'":[{'"NodeId'":2,'"caption'":'"test2'",'"hasChildren'":false,'"parentId'":'"1'"},{'"NodeId'":3,'"caption'":'"test3'",'"hasChildren'":false,'"parentId'":'"1'"}]}]"

//编辑:还想提一下,我试过以下几种;它只是为节点文本显示"未定义",树节点不展开

var ds2 = new kendo.data.HierarchicalDataSource(
{
    transport:
    {
        read: function (options)
        {
            Ajax(urlToTreeItems, false, null, "POST", null, "application/json; charset=utf-8", "json",
            function (result)
            {
                var jResult = $.parseJSON(result);
                options.success(jResult);
            });
        },
        schema:
        {
            model:
            {
                id: "NodeId",
                text: "caption",
                hasChildren: "hasChildren"
            }
        },
    }
});
var tv = $("#test").kendoTreeView(
{
    dataSource: ds2
}).data("kendoTreeView");

Kendo Treeview和阅读网址

try:

 schema:
    {
        model:
        {
            id: "NodeId",
            children: "items"
            hasChildren: "hasChildren"
        }
    }