json - Kendo UI Treeview 和 JSON

标签 json treeview datasource hierarchical-data kendo-ui

所以我想用 kendo UI treeview 项创建一个树,并将它绑定(bind)到一个作为 JSON 文件的远程分层数据源。

我希望生成的树是这样的:

(车辆)

--(汽车)

----FM-1100

----FM-4200

--(自行车)

----FM-3100

(人员)

--(客户)

----GH-3000

--(贵宾)

----GH-3100

附言。 () 中的名称应该类似于包含其“子级”的文件夹

我已经在 kendo ui 网站上查看了有关上述所有内容的文档,但我对每次在树中展开项目时 TreeView 用于加载更深阶段的整个回调函数感到有点困惑。

我们以剑道文档中的例子为例:

var homogeneous = new kendo.data.HierarchicalDataSource({
transport: {
    read: {
        url: "http://demos.kendoui.com/service/Employees",
        dataType: "json"
    }
},
schema: {
    model: {
        id: "EmployeeId",
        hasChildren: "HasEmployees"
    }
}
});

$("#treeview").kendoTreeView({dataSource: homogeneous});

JSON 示例数据:

    {
"employees": [
{"EmployeeId":2,"FullName":"Andrew Fuller","HasEmployees":true,"ReportsTo":null},
{"EmployeeId":3,"FullName":"Carl Jenkins","HasEmployees":true,"ReportsTo":null},
{"EmployeeId":4,"FullName":"Aston Miller","HasEmployees":false,"ReportsTo":2},
{"EmployeeId":5,"FullName":"Damon Sherbands","HasEmployees":false,"ReportsTo":2},
{"EmployeeId":6,"FullName":"Dariel Hanks","HasEmployees":true,"ReportsTo":null},
{"EmployeeId":7,"FullName":"Jason Jackson","HasEmployees":false,"ReportsTo":3},
{"EmployeeId":8,"FullName":"Reylen Scribbs","HasEmployees":false,"ReportsTo":6}
]
}

所以,我必须在“http://demos.kendoui.c​​om/service/Employees”上设置一个休息服务器,它接受来自提供“EmployeeId”的树的 GET,然后在文件中进行搜索,返回那些“ReportTo”收到“EmployeeId”的人......? 树第一次想要显示初始节点时会发生什么?

类似于:

@Path("/Employees")
@GET
@Produces(MediaType.TEXT_HTML)
public String returnEmployees(@QueryParam("EmployeeId") int accID) {
    //search the employees.json
    return "<head></head><body><pre>" + searchResultsString + "</pre></body>";
}

如何有效地搜索 JSON 文件并以字符串形式返回所有结果? 或者,如果所有这些都是错误的,有人可以帮助我理解所有 GET 和回调的东西吗?也许它确实与我听说过的 jsonp 有关?这里有点迷失..

提前致谢

最佳答案

您能否创建具有以下结构的 JSON 文件(类似于您建议的 XML 格式)?

[
    {
        "id"   :100,
        "text" :"tree",
        "items":[
            {
                "id"   :1,
                "text" :"Vehicle",
                "items":[
                    {
                        "id"   :2,
                        "text" :"Cars",
                        "items":[
                            {
                                "id"  :3,
                                "text":"FM-1100"
                            },
                            {
                                "id"  :4,
                                "text":"FM-4200"
                            }
                        ]
                    },
                    {
                        "id"   :5,
                        "text" :"Bikes",
                        "items":[
                            {
                                "id"  :6,
                                "text":"FM-3100"
                            }
                        ]
                    }
                ]
            },
            {
                "id"   :7,
                "text" :"Personnel",
                "items":[
                    {
                        "id"   :8,
                        "text" :"Personnel",
                        "items":[
                            {
                                "id"   :9,
                                "text" :"Clients",
                                "items":[
                                    {
                                        "id"  :10,
                                        "text":"GH-3000"
                                    }
                                ]
                            },
                            {
                                "id"   :11,
                                "text" :"VIPs",
                                "items":[
                                    {
                                        "id"  :12,
                                        "text":"GH-3100"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

其中每个元素都有一个 id 一个 text 它将显示在树中,还有一个数组 items 包含每个子元素树。

如果是这样,您的代码应该是:

$.getJSON("/testTree.json", function (data) {
    $("#treeview").kendoTreeView({
        dataSource: data
    });
})

其中 /testTree.json 是您的 JSON 文件的 URL。

关于json - Kendo UI Treeview 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286874/

相关文章:

java - 在Jmeter中提取数组中的json

java - 更改 JSON 字符串中的键

winforms - Winform .NET 增强 TreeView ?

python-3.x - 如何选择所有 TreeView 并使用 ctrl-c 复制

java - 如何使用 Websphere Liberty Profile 8.5 中定义的数据源?

angular - @angular/cdk/collections DataSource<T> 的断开连接方法未被调用

PHP json 用父类和子类编码数据

javascript - 在 jquery .post 中需要将 json 数组格式化为 html 表

vb.net - 从 VB6 迁移到 .NET,TreeNode.FirstSibling 是否有等效功能?

tomcat - 数据源属性的 SpringBoot 配置文件