• ztree-demo 2


    <!DOCTYPE html>
    <HTML>
    <HEAD>
        <TITLE> ZTREE DEMO - Async</TITLE>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/demo.css" type="text/css">
        <link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css" type="text/css">
        <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
        <script type="text/javascript" src="js/jquery.ztree.core.js"></script>
        <script type="text/javascript" src="js/jquery.ztree.excheck.js"></script>
        <script type="text/javascript" src="js/jquery.ztree.exedit.js"></script>
    </HEAD>
    
    <BODY>
        <ul id="treeDemo" class="ztree"></ul>
        <button type="button" class="save">保存</button>
        <table>
            <tr data_id="1">
                <td class="title"><a href="javascript:void(0)" class="update">修改</a><a href="javascript:void(0)" class="delete">删除</a></td>
                <td class="filePath">n1/n1.n1/n1.n1.n1/n1.n1.n1.n3<br>n3/n3.n2</td>
            </tr>
            <tr data_id="2">
                <td><a href="javascript:void(0)" class="update">修改</a><a href="javascript:void(0)" class="delete">删除</a></td>
                <td class="filePath">n1/n1.n1/n1.n1.n1/n1.n1.n1.n4<br>n3/n3.n3/n3.n3.n1/n3.n3.n1.n2</td>
            </tr>
        </table>
    
        <SCRIPT type="text/javascript">
            <!--
            var setting = {
                check: {
                    enable: true
                },
                async: {
                    enable: true,
                    url:"demo/cn/asyncData/getNodes.php",
                    autoParam:["id", "name=n", "level=lv"],
                    otherParam:{"otherParam":"zTreeAsyncTest"},
                    dataFilter: filter
                },
                callback: {
                    onAsyncSuccess: onAsyncSuccess,
                    onExpand: onExpand,
                    onCheck: onCheck,
                    onNodeCreated: onNodeCreated
                }
            };
    
            var zTreeObj;
             $(document).ready(function(){
                zTreeObj = $.fn.zTree.init($("#treeDemo"), setting);
            });
    
            function filter(treeId, parentNode, childNodes) {
                if (!childNodes) 
                    return null;
                childNodes.forEach(function(childNode){
                    // 计算路径
                    var path = '';
                    if(!parentNode){
                        path = childNode.name;
                    }else{
                        path = parentNode.filePath + '/' + childNode.name;
                    }
                    childNode.filePath = path;
    
                    // 判断是否已选中
                    var checked = false;
                    var halfCheck = false;
                    var chkDisabled = false;
                    $(".filePath").each(function(i, item){
                        var filePathArray = $(item).html().trim().split('<br>');
                        filePathArray.forEach(function(filePath){
                            filePath = filePath.trim();
                            if(filePath == childNode.filePath){// 全匹配(叶节点)
                                checked = true;
                                if(jQuery.inArray(filePath,filterFilePathArray) < 0){
                                    chkDisabled = true;
                                }
                            }else if(filePath.indexOf(childNode.filePath) == 0){// 部分匹配(父节点)
                                checked = true;
                                // halfCheck = true;
                            }
                        });
                    });
                    childNode.checked = checked;
                    childNode.halfCheck = halfCheck;
                    childNode.chkDisabled = chkDisabled;
                });
                return childNodes;
            }
    
            function onAsyncSuccess(event, treeId, treeNode, msg) {
                console.log(treeNode);
            }
    
            function onExpand(event, treeId, treeNode) {
                // console.log(treeNode);
            };
    
            function onCheck(event, treeId, treeNode) {
                // console.log(treeNode);
            };
    
            function onNodeCreated(event, treeId, treeNode) {
                if(treeNode.checked){
                    zTreeObj.expandNode(treeNode, true, false, false, false);
                }
            };
            //-->
        </SCRIPT>
    
    
        <SCRIPT type="text/javascript">
            <!--
            var filterFilePathArray = [];
            var nowUpdateIndex = -1;
    
            $(document).on("click", ".delete", function(){
                $(this).parents("tr").remove();
    
                zTreeObj.destroy();
                zTreeObj = $.fn.zTree.init($("#treeDemo"), setting);
            });
    
            $(document).on("click", ".update", function(){
                nowUpdateIndex = $(this).parents("tr").attr("data_id");
    
                filterFilePathArray = [];
                var filePathArray = $(this).parents("td").siblings(".filePath").text().html().split('<br>');
                filePathArray.forEach(function(filePath){
                    filePath = filePath.trim();
                    filterFilePathArray.push(filePath);
                });
    
                zTreeObj.destroy();
                zTreeObj = $.fn.zTree.init($("#treeDemo"), setting);
            });
    
            $(document).on("click", ".save", function(){
                var str = '';
                var nodes = zTreeObj.getCheckedNodes(); 
                if(!!nodes){
                    var i = 0;
                    nodes.forEach(function(node){
                        if(!node.isParent){
                            if(i == 0){
                                str = node.filePath;
                            }else{
                                str = str + '<br>' + node.filePath;
                            }
                            i++;
                        }
                    });
                }
    
                if(str==''){
                    return;
                }
    
                if(nowUpdateIndex<=0){
                    var id = $("tr").length + 1;
                    id = id + '.' + Math.floor(Math.random()*100+1);
                    $("table").append("<tr data_id='"+id+"'><td><a href='javascript:void(0)' class='update'>修改</a><a href='javascript:void(0)' class='delete'>删除</a></td><td class='filePath'>"+str+"</td></tr>");
                }else{
                    $("<tr data_id='"+nowUpdateIndex+"'><td><a href='javascript:void(0)' class='update'>修改</a><a href='javascript:void(0)'' class='delete'>删除</a></td><td class='filePath'>"+str+"</td></tr>").replaceAll("tr[data_id='"+nowUpdateIndex+"']");
                }
    
                zTreeObj.destroy();
                zTreeObj = $.fn.zTree.init($("#treeDemo"), setting);
            });
            //-->
        </SCRIPT>
    </BODY>
    </HTML>
  • 相关阅读:
    js变量如何赋值给css使用?
    前端预览与下载PDF小结
    子组件如何改父组件传过来的值
    TensorRT转换时的静态模式与动态模式
    Linux:搭建GlusterFS文件系统
    OpenFeign传输文件MultipartFile
    Docker:commit、export、import、save、load命令的使用
    Git:代码版本回退
    docker安装Drools Workbench
    ArchLinux:Typora设置gitee图床
  • 原文地址:https://www.cnblogs.com/zyjzz/p/5759326.html
Copyright © 2020-2023  润新知