JS树形菜单组件Bootstrap TreeView使用方法详解

简要介绍: 

 

之前手头的一个项目需要去做一个左侧的树形菜单,右侧则是一个整体的iframe,从而构成一个整体的网站。一开始是打算用bootstrap的tree-view插件,直接把菜单的数据传过去就好了,结果后来项目又改了需求,菜单内容和图表都是后台动态生成的,所以只能放弃使用bootstrap插件,自己着手写了一个树形菜单。本文主要分两部分讲,一个是对于bootstrap的treeview的实践,另一部分是介绍自己写的树形菜单

bootstrap-treeview

组件介绍:

其实关于该组件在其他网站上已经讲得很详细了,我就不再赘述了,但是网上的应用还是有点问题,这里主要讲一下自己使用这个插件过程吧。

1. html引用及结构

  引用css:文件本身的css文件、bootstrp.css文件

  引用js:jquery、bootstrap-treeview.js、引用该组件的treeview.js文件

  整体HTML结构主要分为了三个部分:头部、树状栏部分、iframe部分,使用组件的为树状栏部分:#tree

2.引用组件js设置:

  具体设置代码如下:主要思路是用data传入菜单的数据和依靠关系,同时可以设置一些变量来控制改树状栏的样式和基本功能,如代码40-43行,具体变量对应的数值的意义可以参见之前链接中的表格

rush:js;"> (function(win) { var data = [ { text: "Parent 1",nodes: [ { text: "Child 1",nodes: [ { text: "Grandchild 1" },{ text: "Grandchild 2" } ] },{ text: "Child 2" } ] },{ text: "Parent 2" },{ text: "Parent 3" },{ text: "Parent 4" },{ text: "Parent 5" } ];

var tree = function() {
$('#tree').treeview({
 data: data,backColor: '#293541',color: 'white',onhoverColor:'#202a33;',showBorder: false
});
}

var init = function() {
tree();
}

init();
})(window)

  设置完成之后树状栏的样式如下图所示,另外细节方面可以通过阅读相应参数来设置,值得一提的是树状栏的icon图标是通过bootstrap的glyphicon设置的,有兴趣的童鞋可以去看一下这个东西,来为菜单设置不同的icon,不过实际效果感觉不是特别好。这也是我决定自己去搞一个树状栏的原因。

  

自定义树状菜单

  treeview的插件只能点击菜单前面的加号icon展开关闭,样式的变化有限,而且我们需要根据后台传入的数据来动态设置菜单的结构和内容,所以为了满足这几个需求,重新写了一个tree.js

  js主要分成三个部分,第一个部分是为每个菜单和子菜单注册点击事件以及通过后台传来的数据为其绑定跳转链接;第二个部分是通过ajax获取后台传来的菜单数据,并将数据传入前台;第三个部分是通过underscore的template函数前台页面进行渲染,达到动态实现树状栏的功能。、

  相关js代码

rush:js;"> var tree = function() { //一级导航点击事件 $('.nodeBox').on('click',function(event) { var _this = $(this); var child = _this.parent().find('.nodechild_Box'); if (_this.attr('opened') == 'opened') { _this.parent().find('.childnode').hide(); child.hide(); _this.attr('opened',''); }else{ _this.parent().find('.childnode').show(); child.show(); _this.attr('opened','opened'); }; }); //二级导航点击事件 $('.nodechild_Box').on('click',function(event) { var _this = $(this); var child = _this.parent().find('.gchild_Box'); if (_this.attr('opened') == 'opened') { child.hide(); _this.parent().find('.gchildnode').hide(); _this.find('.add').attr('src','images/icon_add.png'); _this.attr('opened',''); }else{ child.show(); _this.parent().find('.gchildnode').show(); _this.find('.add').attr('src','images/icon_minus.png'); _this.attr('opened','opened'); }; }); //三级导航点击事件 $('.gchild_Box').on('click',function(event) { var _this = $(this); var child = _this.parent().find('.ggchild_Box'); if (_this.attr('opened') == 'opened') { child.hide(); _this.find('.add').attr('src',''); }else{ child.show(); _this.find('.add').attr('src','opened'); }; });

//hover显示箭头及背景变化
$('.nodeBox').mouSEOver(function(event) {
$(this).addClass('tree_hover');
$(this).find('.arrow').show();
});
$('.nodeBox').mouSEOut(function(event) {
$(this).removeClass('treehover');
$(this).find('.arrow').hide();
});
$('.nodechild
Box').mouSEOver(function(event) {
$(this).addClass('Boxhover');
$(this).find('.arrow').show();
});
$('.nodechild
Box').mouSEOut(function(event) {
$(this).removeClass('Boxhover');
$(this).find('.arrow').hide();
});
$('.gchild
Box').mouSEOver(function(event) {
$(this).addClass('Boxhover');
$(this).find('.arrow').show();
});
$('.gchild
Box').mouSEOut(function(event) {
$(this).removeClass('Boxhover');
$(this).find('.arrow').hide();
});
$('.ggchild
Box').mouSEOver(function(event) {
$(this).addClass('Boxhover');
$(this).find('.arrow').show();
});
$('.ggchild
Box').mouSEOut(function(event) {
$(this).removeClass('Box_hover');
$(this).find('.arrow').hide();
});
};

//链接函数
var tree_link = function() {

var linkBox = $('[menurl]');
linkBox.each(function(i,ele) {
var _ele = $(ele);
var key = _ele.attr('menurl');
if(key != '/'){
$(this).on('click',function(){
$('#mainweb').attr('src',key);
auto();
})
}

});
};

//获取登陆用户数据
var getData = function() {
var cond = sessionStorage.cond;

$.post("XXXX",{},function(json) {
console.log(json)
if(json.code == 200){
data = json.data;
fillUserName(data);
fillTree(data);
var length = $('.nodeBox').length ;
for (var i = 0;i < length;i++) {
var iconId = data.icons[i].iconId;
$('.nodeBox').eq(i+1).attr('menuid',i);
$('.nodeBox').eq(i+1).find('img').attr('src','images/'+ data.icons[iconId-1].name +'');

}
//为每个菜单添加链接
tree_link()
}
},function(xhr) {
console.log(xhr)
});

}

var fillTree = function(data){
var tmplDom = $('#tree');
tmplDom.parent().html(eking.template.getHtml(tmplDom.html(),data));
tree();
}

HTML渲染:

rush:xhtml;">
Box">
Box index" menurl="notice.html"> 首页
<%var menus = data.menus;%> <%for(var i = 0;i < menus.length;i++){%>
Box">
Box" menurl=<%=menus[i].url%> >
<%var childmenus = menus[i].childs;%> <%for(var j = 0;j < childmenus.length;j++){%>
Box" menurl=<%=childmenus[j].url%> > <%if(childmenus[j].childs.length != 0){%> <%}else{%> <%}%>
<%var cchildmenus = childmenus[j].childs;%> <%for(var k = 0;k < cchildmenus.length;k++){%>
Box" menurl=<%=cchildmenus[k].url%> > <%if(cchildmenus[k].childs.length != 0){%> <%}else{%> <%}%>
<%var ccchildmenus = cchildmenus[k].childs;%> <%for(var l = 0;l < ccchildmenus.length;l++){%>
Box" menurl=<%=ccchildmenus[l].url%> >
<%}%>
<%}%>
<%}%>
<%}%>

后台传入的数据格式为

菜单效果如图:

存在的不足和问题:

为了跟上项目进度,tree.js尚未组件化,等有时间了打算把这一块封装为一个js组件,通过设置参数完成树状栏的设置。

P.S.由于个人技术水平有限,难免出现错误,请多多指正 :)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

Bootstrip HTML 查询搜索常用格式模版 &lt;form class=&...
如何在按钮上加红色数字 您可以使用Bootstrap的badge组件来在...
要让两个按钮左右排列,你可以使用 Bootstrap 的网格系统将它...
是的,可以将status设置为布尔类型,这样可以在前端使用复选...
前端工程师一般用的是Bootstrap的框架而不是样式,样式一般自...
起步导入:<linkrel="stylesheet"href="b...