菜单联动实现三(AJAX+TV20.js)

1、拿到父目录信息

    

<%
List<Category> categories = CategoryService.getInstance().getTopCategories();
%>

 

2、主脚本

      

<script language="javascript">
<!--
	addListener("dblclick", "myLabelDblClick");//当事件类型为dblclick时,就调用myLabelDblClick方法
	addNode(-1,0,"类别","images/top.gif");//增加根节点
	<%
	for(int i=0; i<categories.size(); i++) {//把父目录信息增加到根节点下面
		Category c = categories.get(i);
		%>
		addNode(<%=c.getPid()%>,<%=c.getId()%>,"<%=c.getName()%>","images/top.gif");
		<%
	}
	%>
	showTV();
-->
</script>

 3、实现myLabelDblClick方法

     

<script language="javascript" src="script/TV20.js"></script>//引用TV20.js
<script language="javascript">
	var req;
	var gKey;
	function init() {
		if(windodw.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	function myLabelDblClick(key, parentKey) {
		if(findNode(key).subitems.length > 0) return;
		init();
		gKey = key;
		var url = "GetCategoryChilds2.jsp?id=" + escape(key);
		req.open("GET", url, true);
		req.onreadystatechange = callback;
		req.send(null);
	}
	
	function callback() {
		if(4 == req.readyState) {
			if(200 == req.status) {
				eval(req.responseText);
				var node = findNode(gKey);//将插入孩子的父节点刷新后再打开
				if(node.subitems.length > 0) {
					node.refresh();
					node.open();
				}
			}
		}
		
	}
	
	
</script>

 

    4、实现GetCategoryChilds2.jsp

  

<%
int id = Integer.parseInt(request.getParameter("id"));
//取出该孩子下面的所有孩子信息,加入到相应的父节点下面
List<Category> childs = CategoryService.getInstance().getChilds(id) ;
System.out.println(childs.size());
StringBuffer buf = new StringBuffer();

for(int i=0; i<childs.size(); i++) {
	Category c = childs.get(i);
	buf.append("addNode(" + id + "," + c.getId() + ",'" + c.getName() + "','images/top.gif');" + "\n");
}

response.setContentType("text/html;charset=gbk");
response.setHeader("Cache-Control", "no-store"); //HTTP1.1
response.setHeader("Pragma", "no-cache"); //HTTP1.0
response.setDateHeader("Expires", 0); //prevents catching at proxy server

response.getWriter().write(buf.toString());
%>

 

你可能感兴趣的:(JavaScript,C++,c,Ajax,C#)