对于Kendo TreeView,模型结构如何绑定远程数据?

时间:2016-11-29 05:07:31

标签: model-view-controller kendo-ui treeview

如何使用Kendo TreeView模型结构来绑定远程数据

1 个答案:

答案 0 :(得分:0)

我们有一个树,显示具有层次结构的系统位置。我们使用以下代码: C#代码:

[HttpPost]
public async Task<ActionResult> GetChildrenAsync(long? parentId)
    {
        //retrieving location's children by parentId
        //DTO has ChildrenCount field that shows how many children has any particular location

        return new JsonResult
        {
            Data = locations.Select(x => new
            {
                LocationId = x.Id,
                Name = x.Name,
                HasChildren = x.ChildrenCount > 0
            }),
            MaxJsonLength = Int32.MaxValue
        };
    }

JS代码:

var locationDataSource = new kendo.data.HierarchicalDataSource({
        transport: {
            type: "json",
            read: {
                url: "@Url.Action("GetChildren", "Location")",
                type: "POST"
            },
            parameterMap: function (data) {
                return { parentId: data.LocationId };
            }
        },
        schema: {
            model: {
                id: "LocationId",
                hasChildren: "HasChildren"
            }
        }
    });

    var kendoTree = $("#location-tree").kendoTreeView({
        dataSource: locationDataSource,
        dataTextField: "Name"
    });