码迷,mamicode.com
首页 > Web开发 > 详细

通过ajax 后台传递的 区域id 选中ztree的节点 并展开节点

时间:2015-09-09 17:09:02      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

代码如下:

< script type = "text/javascript" >
    var flag = "<%=request.getParameter("
flag ")%>";
if (flag != "true") {
    flag = false;
} else {
    flag = true;
}
var reginTree = {
    setting: {
        view: {
            fontCss: {
                color: "#000"
            }
        },
        check: {
            enable: true,
            chkboxType: {
                "Y": "p",
                "N": "p"
            }, //加上ps  当点击父节点时子节点全部选中
            children: "children"
        },
        data: {
            key: {
                checked: "checked"
            },
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: 0
            }
        }
    },
    //获取ztree对象
    getZtreeObj: function(id) {
        var zTree;
        if ($.fn.zTree.getZTreeObj(id) != null && $.fn.zTree.getZTreeObj(id) != undefined) {
            zTree = $.fn.zTree.getZTreeObj(id);
        }
        return zTree;
    },
    //初始化ztree
    loadZtree: function(json, id, treeNode, callback) {
        var setter = $.extend({}, reginTree.setting, json);
        $.fn.zTree.init($(‘#‘ + id), setter, treeNode);
        var zTree = reginTree.getZtreeObj(id);
        if (callback) callback();
    },
    //加载ztree
    loadArea: function(bal, regionIdList) {
        var regionJson = {
            "level": "2"
        };
        var treeNode = [];
        _public_.getJson(regionJson, "frontServer?busiCode=02060203").done(function(data) {
            if (data.returnCode == "Y") {
                for (var i = 0; i < data.regionList.length; i++) {
                    var id = data.regionList[i].rid;
                    var pId = data.regionList[i].pid;
                    var name = data.regionList[i].regionName;
                    var json = {
                        "id": id,
                        "name": name,
                        "pId": pId
                    };
                    if (bal) {
                        if (reginTree.setbal(regionIdList, json.id)) {
                            json.checked = true;
                        }
                    }
                    treeNode.push(json);
                }
                console.dir(treeNode);
            } else {
                console.log("加载数据失败");
            }
            reginTree.loadZtree({
                callback: {
                    onCheck: reginTree.zTreeOnCheck
                }
            }, ‘tree‘, treeNode, function() {
                var treeObj = reginTree.getZtreeObj(‘tree‘);
                var nodes = treeObj.getCheckedNodes(true);
                $.each(nodes, function(i, value) {
                    treeObj.expandNode(value, true, false, true);
                });
            });
        });

        $(‘#tree‘).css({
            ‘width‘: ‘400px‘,
            ‘height‘: ‘450px‘,
            ‘overflow‘: ‘auto‘
        });
        $(‘#tree‘).parent().css({
            ‘width‘: $(‘#tree‘).get(0).offsetWidth + ‘px‘,
            ‘height‘: $(‘#tree‘).get(0).offsetHeight + ‘px‘
        });

    },
    //ztree节点选中   当选中节点时树形 结构展开
    zTreeOnCheck: function(event, treeId, treeNode) {
        var treeObj = reginTree.getZtreeObj(‘tree‘);

        if (treeNode.checked == true) {
            //单选控制
            treeObj.checkAllNodes(false);
            treeObj.checkNode(treeNode, true, true, false);

            var oner = treeObj.getCheckedNodes(true);
            treeObj.expandNode(treeNode, true, false, true);
        }
        /* var nodes = treeObj.getCheckedNodes(true);
        console.dir(nodes); */

        /* var treeObj = reginTree.getZtreeObj(‘tree‘),
            nodes = treeObj.getCheckedNodes(true),
            v = "";
        for (var i = 0; i < nodes.length; i++) {
            v += nodes[i].name + ",";
            alert(nodes[i].id + "," + v); //获取选中节点的值
        } */
    },
    getCheckNode: function(id, bal, param) {
        var zTree = reginTree.getZtreeObj(id);
        var nodes = zTree.getCheckedNodes(bal);
        var arr = [];
        var i = 0;
        var len = nodes.length;
        console.dir(nodes);
        for (i = 0; i < len; i++) {
            arr.push(nodes[i][param]);
        }
        return arr.join();
    },
    setbal: function(regionIdList, id) {
        var arr = regionIdList.split(‘,‘);
        var i = 0;
        var len = arr.length;
        for (i = 0; i < len; i++) {
            if (arr[i] == id) {
                return true;
                break;
            }
        }
    }
};


$(function() {
    $(".submit").on("click", function() {
        var regionIds = reginTree.getCheckNode("tree", true, "id");
        if (flag == true) {
            var jsonr = {
                "regionIds": regionIds
            };
            _public_.getJson(jsonr, "frontServer?busiCode=SYSRegionGetAllName").done(function(data) {
                window.opener.setRegionIds(data.regionAllName, regionIds);
                window.close();
            });
        } else {
            window.opener.setRegionIds("", regionIds);
            window.close();
        }
    });
});
var regionJson = {};
var regionIdList = window.opener.getRegionIds();
var bal = false;
if (regionIdList != null || regionIdList != "") {
    /* regionJson = {"regionIdList":regionIdList}; */
    bal = true;
}

//console.dir(regionJson);

reginTree.loadArea(bal, regionIdList); < /script>

通过ajax 后台传递的 区域id 选中ztree的节点 并展开节点

标签:

原文地址:http://www.cnblogs.com/nianzhilian/p/4795090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!