easyui-tree 清空树 加载树 勾选树 保存树

easyui-tree树

   
    清空树:

        //清空树
        function LoadEmptyTree() {
            $("#myTree").tree('loadData', []);
        }
    加载树:

    //加载选中权限的控件树
        function LoadSavedTree() {
            $("#div_FW_Edit").show();
            $("#div_FW_Save").hide();
            var rows = $("#DataGrid").datagrid("getSelections");
            if (rows.length == 1) {
                var id = rows[0].PermissionID;
                $.ajax({
                    type: "GET",
                    data: {
                        PermissionID: id,
                    },
                    url: "/Rights/Execute/LoadSavedTree",
                    success: function (result) {
                        if (result != '') {
                            var myJson = eval('(' + result + ')');
                            $("#myTree").tree({
                                data: myJson,
                                onlyLeafCheck: false,//仅叶子节点可以被选中
                                checkbox: false,//开启多选框
                            });
                        }
                        else {           
                            LoadEmptyTree();//当前权限不包含任何控件,清空树
                        }
                    }
                });
            }
            else {
                LoadTree();
            }
        }
    自动勾选选中项目:

    function EditTree() {
            $("#div_FW_Edit").hide();
            $("#div_FW_Save").show();
            $.ajax({
                type: "GET",
                url: "/Rights/Execute/GetTreeJson",
                success: function (result) {
                    //加载全部tree
                    var myJson = eval('(' + result + ')');
                    $("#myTree").tree({
                        data: myJson,
                        onlyLeafCheck: true,//仅叶子节点可以被选中
                        checkbox: true,//开启多选框
                    });
                    //自动勾选选中项目
                    var root = $("#myTree").tree("getRoot");
                    $("#myTree").tree("uncheck", root.target);
                    var rows = $("#DataGrid").datagrid("getSelections");
                    var id = rows[0].PermissionID;
                    $.ajax({
                        url: "/Rights/Execute/BindTree",
                        data: {
                            PermissionID: id,
                        },
                        success: function (data) {
                            for (x in data) {
                                //alert(data[x].ControlID);
                                var node = $("#myTree").tree("find", data[x].ControlID);
                                $("#myTree").tree("check", node.target);
                            }
                        }
                    });
                }
            });
        }
    保存勾选的tree

    $(function () {
            $("#btn_FW_Save").click(
                function () {
                    var rows = $("#DataGrid").datagrid("getSelections");
                    if (rows.length != 1) {
                        alert("请左边列表中选择一个[权限]!");
                        return;
                    } else {
                        var id = rows[0].PermissionID;
                        var nodes = $("#myTree").tree("getChecked");
                        var res_id = '';
                        for (var i = 0; i < nodes.length; i++) {
                            if (res_id != '') res_id += ',';
                            res_id += nodes[i].id;
                        }
                        if (res_id == '') {
                            alert("请选择[控件]!");
                            return;
                        }
                        $.ajax({
                            url: "/Rights/Execute/SaveTree",
                            data: {
                                PermissionID: id,
                                ControlID: res_id,
                            },
                            success: function (data) {
                                if (data == "OK") {
                                    alert("修改成功!");                             
                                    LoadSavedTree();                                
                                }
                                else {
                                    alert(data);
                                }
                            }
                        });
                    }
                });
        });





    你可能感兴趣的:(html,jquery,JS代码)