关于dtree伸缩展开的问题
最近在用那个dtree做个树形菜单采用的jquery+ajax+dtree+struts2的结构,下面是我的做法:
大部分地方比较顺利,先分享一下我的做法,再提个问题。
利用struts2中json的插件,返回菜单表的json数据,再利用dtree将菜单表展现出来:
下面是ajax及构造dtree的js文件,因为dtree中的图片是路径是默认的所以我改成下面的这种
function dTree(objName,sImgPath) {
this.config = {
target: null,
folderLinks: true,
useSelection: true,
useCookies: true,
useLines: true,
useIcons: true,
useStatusText: false,
closeSameLevel: false,
inOrder: false
}
this.icon = {
root: sImgPath+'base.gif',
folder: sImgPath+'folder.gif',
folderOpen: sImgPath+'folderopen.gif',
node: sImgPath+'page.gif',
empty: sImgPath+'empty.gif',
line: sImgPath+'line.gif',
join: sImgPath+'join.gif',
joinBottom: sImgPath+'joinbottom.gif',
plus: sImgPath+'plus.gif',
plusBottom: sImgPath+'plusbottom.gif',
minus: sImgPath+'minus.gif',
minusBottom: sImgPath+'minusbottom.gif',
nlPlus: sImgPath+'nolines_plus.gif',
nlMinus: sImgPath+'nolines_minus.gif'
};
this.obj = objName;
this.aNodes = [];
this.aIndent = [];
this.root = new Node(-1);
this.selectedNode = null;
this.selectedFound = false;
this.completed = false;
};
这样在new时就可以把图片的路径加进去,这也是根据网上的个朋友的做法改写的,我的jquery的代码是单独放到一个文件中去的。如下所示
$(document).ready(function(){
var wscmtree = new dTree("tree","main/pic/");
var url = "createmenu.action";
var m_username=encodeURIComponent("");
var params = {username:m_username};
jQuery.post(url, params, callbackFun, "json");
function callbackFun(data)
{
var list = data.list;
for(var i=0;i<list.length;i++)
{
var menuid = list[i].menuid;
var parentid = list[i].parentid;
var menuname = "";
if (list[i].menuname==null)
{menuname=""}else{menuname = trim(list[i].menuname)}
var url = "";
if (list[i].url==null)
{url=""}else{url = trim(list[i].url)}
var title = "";
var target = "";
var icon = "";
var iconopen = "";
var open = false;
wscmtree.add(menuid, parentid, menuname,url,title,target,icon,iconopen,open);
}
$("body").append(wscmtree.toString());
$("#test").attr("value",wscmtree.toString());
}
$("#openall").click(function(){
wscmtree.openAll();
}
);
$("#closeall").click(function(){
wscmtree.closeAll();
}
);
function trim(str)
{
return str.replace(/^\s+/, ' ').replace(/\s+$/, ' ');
}
})
现在的问题是我的树形菜单可以完全展示出来没有问题了,点击那个openall与closeall按钮时正常。现在有一个问题,我点击那个+或-号时不会出现任何反应是什么问题?页面左下角提示那个javascript是错误的。javascript:tree.0(1),括号中的数字是结点的ID
[解决办法]
加号或者减号是图片,楼主在图片上没有添加事件,所以没有翻译,你在减号和加号上面添加和打开关闭相同的事件就可以了