##這兩天工作比較忙,不過(guò)還是要總結(jié)相關(guān)的坑,希望兄弟們要謹(jǐn)慎應(yīng)用AmazeUI 里邊自帶的樹(shù)形結(jié)構(gòu)插件
##如果你公司對(duì)于樹(shù)形結(jié)構(gòu)這邊要求不要求有點(diǎn)擊事件,只是純顯示那么你可以繼續(xù)向下看,如果要求樹(shù)形結(jié)構(gòu)支持勾選,支持拖拽等等...我建議你直接點(diǎn)擊退出,去用Ztree吧
<link rel="stylesheet" href="assets/css/amazeui.tree.min.css">
<ul class="am-tree" id="tree">
<!--以下第一個(gè)li標(biāo)簽如果設(shè)計(jì)沒(méi)有子級(jí)結(jié)構(gòu),可以屏蔽-->
<li class="am-tree-branch am-hide" data-template="treebranch">
<div class="am-tree-branch-header">
<button class="am-tree-branch-name">
<span class="am-tree-icon am-tree-icon-folder"></span>
<span class="am-tree-label"></span>
</button>
</div>
<ul class="am-tree-branch-children"></ul>
<div class="am-tree-loader"><span class="am-icon-spin am-icon-spinner"></span></div>
</li>
<li class="am-tree-item am-hide" data-template="treeitem">
<button class="am-tree-item-name">
<span class="am-tree-icon am-tree-icon-item"></span>
<span class="am-tree-label"></span>
</button>
</li>
</ul>
<script src="assets/js/amazeui.tree.min.js"></script>
/*****粗加工后臺(tái)數(shù)據(jù)(給單條數(shù)據(jù)增加了id,和pid,type,title),如果后臺(tái)數(shù)據(jù)返回的直接帶有層級(jí)結(jié)構(gòu)的數(shù)據(jù)直接跳過(guò)這個(gè)步驟)
* for(i=0;i<odata.length;i++){
if(odata[i].level>=2){
//data[i].frameMenuStr
//截取倒數(shù)后兩個(gè)"."后邊的字符串/
let arr =["a","b","c","d","e","f","g","h","i"];
let str = odata[i].frameMenuStr;//當(dāng)前數(shù)據(jù)ID
odata[i].id= arr[odata[i].level-1]+str.substring(str.lastIndexOf(".")+1);
let j =str.lastIndexOf(".");//當(dāng)前數(shù)據(jù)父節(jié)點(diǎn)ID
odata[i].pid= arr[odata[i].level-2]+str.substring(str.lastIndexOf(".",j-1),str.lastIndexOf("."));
odata[i].title = odata[i].menuName;
odata[i].type = 'item';
}else{
odata[i].id = "a"+odata[i].frameMenuStr;
odata[i].title = odata[i].menuName;
odata[i].type = 'folder';
//odata[i].pid = "00000000";
}
}
* ********/
/*******
*
* data:灌入的數(shù)據(jù)(后臺(tái)返回的值要為有id和pid)
* dom 所要綁定的區(qū)域id
* callbackfun:回調(diào)函數(shù)
* 范例:
function bindTree(data,dom,callbackfun){
/************核心應(yīng)用:數(shù)組操作******************/
let tree = data;
var treeMaps = {};
tree.forEach(function (value, index) {
treeMaps[value.id] = value;
})
var data = [];
tree.forEach(function (value, index) {
var parent = treeMaps[value.pid]
if (parent !== undefined) {
if (parent.products === undefined) {
parent.products = []
}
parent.products.push(value)
} else {
data.push(value);
}
})
/***************以上這段代碼是二次加工數(shù)據(jù)為的讓之前沒(méi)有層級(jí)結(jié)構(gòu)的數(shù)據(jù),加工成有層級(jí)結(jié)構(gòu)的數(shù)據(jù)結(jié)構(gòu)********************/
dom.tree({
dataSource:function(options, callback) {
// 模擬異步加載
let num = 0;//通過(guò)num值操作區(qū)分(這是個(gè)坑一定要用這種方法,不能用data||options.products)
if(num==0){
setTimeout(function() {
callback({data: data});//初始顯示最高級(jí)別數(shù)據(jù)
num++;
}, 400);
}else{
setTimeout(function() {
callback({data: options.products});//點(diǎn)擊節(jié)點(diǎn)顯示的數(shù)據(jù)
}, 400);
}
},
multiSelect: false,
cacheItems: true,
folderSelect: false,
});
dom.on('selected.tree.amui', function (event, data) {
// do something with data: { selected: [array], target: [object] }
// console.log(data);
// console.log(event);
uuid = data.target.menuId;
resData = data.target;
if(callbackfun || typeof callbackfun != 'undefined' || callbackfun != undefined){
return callbackfun(uuid);
}
});
dom.tree("discloseAll");//這個(gè)函數(shù)暫時(shí)不起作用。
}
/**直接調(diào)用函數(shù)*/
bindTree(odata,$("#tree"),function(){console.log("-------")});
備注:
//dom.tree("destroy");//數(shù)據(jù)更新我調(diào)用這個(gè)函數(shù)。但是一旦調(diào)用,直接所有dom結(jié)構(gòu)都沒(méi)有了,所以你要向之前綁定數(shù)據(jù)的地方重新灌入dom結(jié)構(gòu)。
/***********插件結(jié)構(gòu)重新繪制***************/
// let str = "";
// str+='<li class="am-tree-branch am-hide" data-template="treebranch">';
// str+='<div class="am-tree-branch-header">';
// str+='<button class="am-tree-branch-name">';
// str+='<span class="am-tree-icon am-tree-icon-folder"></span>';
// str+='<span class="am-tree-label"></span>';
// str+='</button>';
// str+='</div>';
// str+='<ul class="am-tree-branch-children"></ul>';
// str+='<div class="am-tree-loader"><span class="am-icon-spin am-icon-spinner"></span></div>';
// str+='</li>';
// str+='<li class="am-tree-item am-hide" data-template="treeitem">';
// str+='<button class="am-tree-item-name">';
// str+='<span class="am-tree-icon am-tree-icon-item"></span>';
// str+='<span class="am-tree-label"></span>';
// str+='</button>';
// str+='</li>';
// dom.append(str);
到此這篇關(guān)于AmazeUi Tree(樹(shù)形結(jié)構(gòu)) 應(yīng)用總結(jié)的文章就介紹到這了,更多相關(guān)AmazeUi Tree樹(shù)形結(jié)構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!