ztree中点击按钮添加新节点(可自定义名称)

<a href="#" id="move-to-space-button">个人空间</a>

<!-- // 移动到个人空间 -->
<div class="modal fade" id="move-to-space-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-create">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title">移动到个人空间</h4>
            </div>
            <form id="move-to-space-modal-form" method="post" class="form-horizontal" action="">
                <div class="modal-body">
                    <!-- // modal-body begin -->

                    <ul id="move-to-space-modal-tree" class="ztree" style="height: 260px; overflow-y: scroll;"></ul>

                    <!-- // modal-body end -->
                </div>
                <div class="modal-footer">
                    <a class="btn btn-default" href="#" role="button" id="move-to-space-modal-new-folder-button" style="float: left;">
                        <i class="glyphicon glyphicon-folder-open"></i>
                        <span>新建文件夹</span>
                    </a>
                    
                    <button class="btn btn-success" id="move-to-space-modal-submit" type="submit">
                        确 定
                    </button>
                    <button class="btn btn-default" type="button" data-dismiss="modal">
                        取 消
                    </button>
                </div>
            </form>
        </div><!-- /Modal -->
    </div>
</div>
$("#move-to-space-button").click(function(){
    var checkedFile = $fileTable.bootstrapTable('getSelections');
    var len = checkedFile.length;
    
    if(len == 0){
        toastr.error("请选择需要移动的文件或文件夹!", "错误提示")
    }else if(len >= 1){        
        $("#move-to-space-modal").modal({
            show: true,
            backdrop:'static'
        });
        
        $.ajax({
            type: "POST",
            url: '/kscd/api/general-storage/dirs/',
            data: JSON.stringify({"file_id": 0}),
            dataType: "json",
            timeout : 390*1000, //todo
            success: function(data){
                if(data.ret){
                    $.fn.zTree.init($("#move-to-space-modal-tree"), fileTreeSettingForMoveToSpace, data.dirs);
                }else{
                    toastr.error(data.msg, "错误提示");
                }
            },
            error: function(data){
                toastr.error(data.msg, "错误提示");
            }
        });
    }
});

// 添加文件夹
function addNewFolder(e) {
    var zTree = $.fn.zTree.getZTreeObj("move-to-space-modal-tree"),
    isParent = e.data.isParent,
    nodes = zTree.getCheckedNodes(),
    treeNode = nodes[0];
    
    var newNodeArr = [];
    var time = getNow();
    
    if (treeNode) {
        filePidForMoveToSpaceNewFolder = treeNode.id;
        newNodeArr = zTree.addNodes(treeNode, {id: time, pId:treeNode.id, is_parent:false, icon_skin: "dir", name:"新文件夹" + time});
    } else {
        toastr.error("请先选择一个父文件夹!", "错误提示");
        return false;
    }
    
    if (newNodeArr.length > 0) {
        zTree.editName(newNodeArr[0]);
    } else {
        toastr.error("请先选择一个父文件夹!", "错误提示");
    }
};

$("#move-to-space-modal-new-folder-button").bind("click", {isParent:false}, addNewFolder);

$("#move-to-space-modal-submit").click(function(e){
    e.preventDefault();
    
    if(file_id == filePidForMoveToSpace){
        toastr.error("目标文件夹与当前文件夹一致,请重新选择!", "错误提示");
        return;
    }else if(filePidForMoveToSpace == ""){
        toastr.error("请选择目标文件夹!", "错误提示");
        return;
    }
    
    var checkedFiles = $fileTable.bootstrapTable('getSelections');
    var len = checkedFiles.length;
    var file_ids = [];
    for(var i = 0; i < len; i++){
        file_ids.push(checkedFiles[i].id);
    }

    var params = JSON.stringify({"file_ids": file_ids, "file_new_pid": filePidForMoveToSpace});
    
    _shade("移动中,请稍候...");
    
    $.ajax({
        type: "POST",
        url: '/kscd/api/general-storage/move-many/',
        data: params,
        timeout : 390*1000, //todo 超时(6分半钟)的话,只能认为该分片未上传过
        dataType: "json",
        success: function(data){
            $(".shade").hide();
            
            if(data.ret){
                toastr.success("移动成功!", "成功提示");
                $("#move-to-space-modal").modal('hide');
                $fileTable .bootstrapTable("refresh");
                hideToolbar();
                filePidForMoveToSpace = "";
            }else{
                toastr.error(data.msg, "错误提示");
            }
        },
        error: function(data){
            $(".shade").hide();
            toastr.error(data.msg, "错误提示");
        }
    });
});
原文地址:https://www.cnblogs.com/samve/p/13956292.html