layui ztree 实现下拉树

layui ztree 实现下拉树_第1张图片

layui ztree 实现下拉树_第2张图片

layui ztree 实现下拉树_第3张图片

layui ztree 实现下拉树_第4张图片

完整代码下载
JS 代码


//id div 的id,isMultiple 是否多选,chkboxType 多选框类型{"Y": "ps", "N": "s"} 详细请看ztree官网
function initSelectTree(id, isMultiple, chkboxType) {
    var setting = {
        view: {
            dblClickExpand: false,
            showLine: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            enable: false,
            chkboxType: {"Y": "ps", "N": "s"}
        },
        callback: {
            onClick: onClick,
            onCheck: onCheck
        }

    };
    if (isMultiple) {
        setting.check.enable = isMultiple;
    }
    if (chkboxType !== undefined && chkboxType != null) {
        setting.check.chkboxType = chkboxType;
    }
    var html = '
' + '' + '' + '
'; $("#" + id).append(html); $("#" + id).parent().append('
' + '' + '
    ' + '
    '); $("#" + id).bind("click", function () { if ($(this).parent().find(".tree-content").css("display") !== "none") { hideMenu() } else { $(this).addClass("layui-form-selected"); var Offset = $(this).offset(); var width = $(this).width() - 2; $(this).parent().find(".tree-content").css({ left: Offset.left + "px", top: Offset.top + $(this).height() + "px" }).slideDown("fast"); $(this).parent().find(".tree-content").css({ width: width }); $("body").bind("mousedown", onBodyDown); } }); $.fn.zTree.init($("#" + id + "Tree"), setting, zNodes); } function beforeClick(treeId, treeNode) { var check = (treeNode && !treeNode.isParent); if (!check) alert("只能选择城市..."); return check; } function onClick(event, treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj(treeId); if (zTree.setting.check.enable == true) { zTree.checkNode(treeNode, !treeNode.checked, false) assignment(treeId, zTree.getCheckedNodes()); } else { assignment(treeId, zTree.getSelectedNodes()); hideMenu(); } } function onCheck(event, treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj(treeId); assignment(treeId, zTree.getCheckedNodes()); } function hideMenu() { $(".select-tree").removeClass("layui-form-selected"); $(".tree-content").fadeOut("fast"); $("body").unbind("mousedown", onBodyDown); } function assignment(treeId, nodes) { var names = ""; var ids = ""; for (var i = 0, l = nodes.length; i < l; i++) { names += nodes[i].name + ","; ids += nodes[i].id + ","; } if (names.length > 0) { names = names.substring(0, names.length - 1); ids = ids.substring(0, ids.length - 1); } treeId = treeId.substring(0, treeId.length - 4); $("#" + treeId + "Show").attr("value", names); $("#" + treeId + "Show").attr("title", names); $("#" + treeId + "Hide").attr("value", ids); } function onBodyDown(event) { if ($(event.target).parents(".tree-content").html() == null) { hideMenu(); } }

    HTML代码

    
    
    
        
        layui
        
        
        
        
        
        
        
    
    
    
    
    
    
    

    你可能感兴趣的:(前端)