如何在Kendo UI Treeview中跟踪已检查的节点ID

时间:2016-02-14 16:35:27

标签: jquery asp.net-mvc kendo-ui telerik kendo-treeview

我正在使用Kendo UI的树视图控件和asp.net mvc中的复选框。父节点和子节点都具有id属性。当用户检查任何子节点时,我想跟踪哪些子节点已被检查。以下是我的cshtml代码以及javascript代码,用于跟踪用于填充树视图的id和我的json。

                <div style="padding-top: 2em;">
                <h4>Status</h4>
                <p id="result">No nodes checked.</p>
            </div>

        <script>                   
            $("#treeview").kendoTreeView({
                checkboxes: {
                    checkChildren: true,
                },
                dataSource: {
                    //type: "odata",
                    transport: {
                        read: {
                            url: '@Url.Content("~/Document/GetDocuments")',
                            type: "post",
                            dataType: "json"
                        }
                    },                        
                    schema: {
                        model: {
                            id: "id", text: "Name",
                            children:  "Files"
                        }
                    }
                },
                dataTextField: [ "Name", "FileName"],
                check: onCheck                                 
            });
            // function that gathers IDs of checked nodes
            function checkedNodeIds(nodes, checkedNodes) {
                for (var i = 0; i < nodes.length; i++) {                      
                    if (nodes[i].checked) {
                        checkedNodes.push(nodes[i].name);
                    }
                    if (nodes[i].hasChildren) {
                        checkedNodeIds(nodes[i].children.view(), checkedNodes);
                    }
                }
            }
            // show checked node IDs on datasource change
            function onCheck() {                  
                var checkedNodes = [],
                    treeView = $("#treeview").data("kendoTreeView"),
                    message;
                checkedNodeIds(treeView.dataSource.view(), checkedNodes);
                if (checkedNodes.length > 0) {                        
                    message = "IDs of checked nodes: " + checkedNodes.join(",");
                } else {
                    message = "No nodes checked.";
                }
                $("#result").html(message);
            }
        </script>

json object
    [{"id":1,"Name":"Checking",
    "Files":[{"Filename":"doc10","id":"1afd5a4f-086f-44d2-9287-8098384e379e"},
    {"Filename":"doc11","id":"89ea3366-14b8-4e91-8273-6e2a51fbe516"}]},
    {"id":2,"Name":"Saving",
    "Files":[{"Filename":"doc20","id":"c7a88f5d-067e-4f20-93b6-da6eff69d532"},
    {"Filename":"doc21","id":"8a0a62ed-1b4a-4e5e-8d59-d57a975a7ab0"}]}]

当我检查一些子节点时,它只显示&#34;已检查节点的ID:,,,&#34;。所以看起来ID值是空白的。

由于

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。

获取所有选定的ID: -

<div id="treeview"></div>
<script>
    var data = [{ "id": 1, "Name": "Checking", "Files": [{ "Filename": "doc10", "id": "1afd5a4f-086f-44d2-9287-8098384e379e" }, { "Filename": "doc11", "id": "89ea3366-14b8-4e91-8273-6e2a51fbe516" }] }, { "id": 2, "Name": "Saving", "Files": [{ "Filename": "doc20", "id": "c7a88f5d-067e-4f20-93b6-da6eff69d532" }, { "Filename": "doc21", "id": "8a0a62ed-1b4a-4e5e-8d59-d57a975a7ab0" }] }];

    var inline = new kendo.data.HierarchicalDataSource({
        data: data,
        schema: {
            model: {
                id: "id", text: "Name",
                children: "Files"
            }
        }
    });

    $(document).ready(function () {
        $("#treeview").kendoTreeView({
            checkboxes: {
                checkChildren: true,
            },
            dataSource: inline,
            dataTextField: ["Name", "Filename"],
            check: onCheck
        });
    });

    function checkedNodeIds(nodes, checkedNodes) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].checked) {
                checkedNodes.push(nodes[i].id);
            }
            if (nodes[i].hasChildren) {
                checkedNodeIds(nodes[i].children.view(), checkedNodes);
            }
        }
    }

    function onCheck() {
        var checkedNodes = [],
            treeView = $("#treeview").data("kendoTreeView"),
            message;
        checkedNodeIds(treeView.dataSource.view(), checkedNodes);
        if (checkedNodes.length > 0) {
            message = "IDs of checked nodes: " + checkedNodes.join(",");
        } else {
            message = "No nodes checked.";
        }
        console.log(message);
    } 
</script>

获取所有选定的姓名和文件名详细信息: -

<div id="treeview"></div>
<script>
    var data = [{ "id": 1, "Name": "Checking", "Files": [{ "Filename": "doc10", "id": "1afd5a4f-086f-44d2-9287-8098384e379e" }, { "Filename": "doc11", "id": "89ea3366-14b8-4e91-8273-6e2a51fbe516" }] }, { "id": 2, "Name": "Saving", "Files": [{ "Filename": "doc20", "id": "c7a88f5d-067e-4f20-93b6-da6eff69d532" }, { "Filename": "doc21", "id": "8a0a62ed-1b4a-4e5e-8d59-d57a975a7ab0" }] }];

    var inline = new kendo.data.HierarchicalDataSource({
        data: data,
        schema: {
            model: {
                id: "id", text: "Name",
                children: "Files"
            }
        }
    });

    $(document).ready(function () {
        $("#treeview").kendoTreeView({
            checkboxes: {
                checkChildren: true,
            },
            dataSource: inline,
            dataTextField: ["Name", "Filename"],
            check: onCheck
        });
    });

    function checkedNodeIds(nodes, checkedNodes) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].checked) {
                if (nodes[i].Name)
                    checkedNodes.push(nodes[i].Name);
                else
                    checkedNodes.push(nodes[i].Filename);
            }
            if (nodes[i].hasChildren) {
                checkedNodeIds(nodes[i].children.view(), checkedNodes);
            }
        }
    }

    function onCheck() {
        var checkedNodes = [],
            treeView = $("#treeview").data("kendoTreeView"),
            message;
        checkedNodeIds(treeView.dataSource.view(), checkedNodes);
        if (checkedNodes.length > 0) {
            message = "IDs of checked nodes: " + checkedNodes.join(",");
        } else {
            message = "No nodes checked.";
        }
        console.log(message);
    }


</script>

如果有任何疑虑,请告诉我。