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, "錯誤提示");
        }
    });
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM