1、得到树信息
public Treeinfo[] getAllTreeinfo() throws Exception{
Connection conn = getConn();
PreparedStatement smt = conn.prepareStatement("select * from treeTable where parent_treeId = -1");
CachedRowSet cs = new CachedRowSetImpl();
cs.populate(smt.executeQuery());
List<Treeinfo> treeinfoList = new ArrayList<Treeinfo>();
while(cs.next()){
Treeinfo treeinfo = new Treeinfo();
treeinfo.setTreeId(cs.getInt(1));
treeinfo.setNodeName(cs.getString(2));
treeinfo.setParentId(cs.getInt(3));
treeinfo.setChildren(getChildren(treeinfo.getTreeId()));
treeinfoList.add(treeinfo);
}
cs.close();
smt.close();
conn.close();
Treeinfo[] treeinfo = new Treeinfo[treeinfoList.size()];
treeinfo = treeinfoList.toArray(treeinfo);
return treeinfo;
}
2、得到当前id的子节点信息,这里用了递归调用
public Treeinfo[] getChildren(int treeNodeId) throws Exception{
Connection conn = getConn();
PreparedStatement smt = conn.prepareStatement("select * from treeTable where parent_treeId = ?");
smt.setInt(1, treeNodeId);
ResultSet rs = smt.executeQuery();
List<Treeinfo> childList = new ArrayList<Treeinfo>();
while(rs.next()){
Treeinfo treeinfo = new Treeinfo();
treeinfo.setTreeId(rs.getInt(1));
treeinfo.setNodeName(rs.getString(2));
treeinfo.setParentId(rs.getInt(3));
treeinfo.setChildren(getChildren(treeinfo.getTreeId()));
childList.add(treeinfo);
}
rs.close();
smt.close();
conn.close();
Treeinfo[] childResult = new Treeinfo[childList.size()];
childResult = childList.toArray(childResult);
return childResult;
}
}
3、最后新建显示树形菜单的jsp页面
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'testtree.jsp' starting page</title>
<s:head theme="ajax" debug="true" />
<script language="JavaScript">
function treeNodeSelected(arg) {
alert("id["+arg.source.widgetId+"], name["+ arg.source.title+ "] selected");
}
dojo.addOnLoad(function() {
var s = dojo.widget.byId('treeTestId').selector;
dojo.event.connect(s, 'select', 'treeNodeSelected');
});
</script>
</head>
<body>
<s:tree id="treeTestId"
theme="ajax"
rootNode="root"
childCollectionProperty="children"
nodeIdProperty="treeId"
nodeTitleProperty="nodeName"
treeSelectedTopic="treeSelected">
</s:tree>
</body>
</html>
㈡ java程序员javascript要掌握到什么水平
java程序员JavaScript要掌握到基础能用的水平。
因为JavaScript对于普通的java程序员来说,一般很难会用到回,当开答发前端的功能,会用到一些功能,当大部分时候只需要能看到别人写的代码。
㈢ javascript实现动态加载树形菜单(tree),
页面既然得到抄从服务袭器传过来的json数据之后,还要转换为Object
比如,服务器组装好的json数据:
{
id:'001',
nodeMsg:'节点1'
}
这个是ajax成功之后的回调
function(xhr){
var o = eval('(' + xhr.responseText + ')');
alert(o.id + ':' + o.nodeMsg);
}
㈣ 用javascript 树形菜单(可拖动效果)
用jquery的第三方插件simpleTree
这插件可以自由拖动,你可以为拖运添加自己的事件
添加子类功能可以自己增加,很容易实现
㈤ js怎么实现复选框树形菜单勾选父节点,子节点全部勾选。子节点勾选,父节点不勾选。
提供一种方来案:
首先你要构建这个自菜单的树结构,
每个菜单的节点对象至少有以一的属性和方法。
a) value 0 (未选取),1(选取)
b) parentNode ------- 父菜单节点对象
c) childNodes ------- 子菜单对象数组.
d) checkAllChilds() {
检查所以孩子,如果全部的子节点为false,则 this.value = false;
并调用父节点的checkAllChilds()方法.
e) onChange事件,
如果是前者的话比较容易,只要判断当前节点的childNodes是不是为空就可以了.
为空的时候,通过调用父节点的checkAllChilds(),将消息向上传递.直到父节点为空或者
或父节点为选中为止.
f) setChildValue 和 checkAllChilds同理,不过是向下进行消息传递.
㈥ 如何用javascript 实现树形结构菜单,可伸缩,有节点。请高手指教!
这个应该适合你:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" href="http://123.jialulu.com/css/tree.css" type="text/css" />
<style type="text/css">
a {text-decoration:none;}
a,a:visited {color:#000;background:inherit;}
body {margin:0;padding:20px;font:12px tahoma,宋体,sans-serif;}
dt {font-size:22px;font-weight:bold;margin:0 0 0 15px;}
dd {margin:0 0 0 15px;}
h4 {margin:0;padding:0;font-size:18px;text-align:center;}
p {margin:0;padding:0 0 0 18px;}
p a,p a:visited {color:#00f;background:inherit;}
/*Temp CSS for View Demo*/
#CNLTreeMenu {float:left;width:249px;border:1px solid #99BEEF;background:#D2E4FC;color:inherit;margin:3px;padding:3px;}
#CNLTreeMenu {padding-bottom:15px;}
</style>
<title>无限级树形菜单</title>
</head>
<body>
<div class="CNLTreeMenu" id="CNLTreeMenu">
<h4>CNL Tree Menu1</h4>
<p><a id="AllOpen" href="#" onClick="MyCNLTreeMenu.SetNodes(0);Hd(this);Sw('AllClose');">全部展开</a><a id="AllClose" href="#" onClick="MyCNLTreeMenu.SetNodes(1);Hd(this);Sw('AllOpen');" style="display:none;">全部折叠</a></p>
<ul>
<li class="Opened"><a href="http://www.jialulu.com">Zzsky.Cn</a>
<ul>
<li><a href="#1">技术区</a>
<ul>
<li><a href="#">网页技术</a>
<ul>
<li class="Child"><a href="#">JavaScript</a></li>
<!--Child Node-->
<li class="Child"><a href="#">HTML/XHTML/CSS</a></li>
<li class="Child"><a href="#">Ajax</a></li>
<li class="Child"><a href="#">网页制作工具</a></li>
<li class="Child"><a href="#">设计/图形</a></li>
<li class="Child"><a href="#">Flash/多媒体</a></li>
<li class="Child"><a href="#">VML/Web3D</a></li>
</ul>
</li>
<!--Sub Node 3-->
<li><a href="#">Web编程</a>
<ul>
<li class="Child"><a href="#">Java</a></li>
<!--Child Node-->
<li class="Child"><a href="#">.Net</a></li>
<li class="Child"><a href="#">ASP/VBScript</a></li>
<li class="Child"><a href="#">PHP</a></li>
<li class="Child"><a href="#">Perl/Python</a></li>
<li class="Child"><a href="#">Web综合/开源</a></li>
</ul>
</li>
<!--Sub Node 3-->
<li><a href="#">数据库</a>
<ul>
<li class="Child"><a href="#">Access/SQLServer</a></li>
<!--Child Node-->
<li class="Child"><a href="#">MySQL/PostgreSQL</a></li>
<li class="Child"><a href="#">Oracle/DB2/Sybase</a></li>
</ul>
</li>
<!--Sub Node 3-->
<li><a href="#">服务器</a>
<ul>
<li class="Child"><a href="#">Windows/IIS</a></li>
<!--Child Node-->
<li class="Child"><a href="#">Unix/Linux/Apache</a></li>
<li class="Child"><a href="#">应用服务器</a></li>
</ul>
</li>
<!--Sub Node 3-->
</ul>
</li>
<!--Sub Node 2-->
<li><a href="#1">二级目录</a>
<ul>
<li><a href="#">三级目录</a>
<ul>
<li><a href="#">四级目录</a>
<ul>
<li><a href="#">五级目录</a>
<ul>
<li><a href="#">......</a>
<ul>
<li><a href="#">第n级目录</a>
<ul>
<li class="Child"><a href="#">叶结点1</a></li>
<!--Child Node-->
<li class="Child"><a href="#">叶结点2</a></li>
<li class="Child"><a href="#">叶结点3</a></li>
</ul>
</li>
<!--Sub Node n -->
</ul>
</li>
<!--Sub Node 6 -->
</ul>
</li>
<!--Sub Node 5 -->
</ul>
</li>
<!--Sub Node 4-->
</ul>
</li>
<!--Sub Node 3-->
</ul>
</li>
<!--Sub Node 2-->
<li><a href="#1">二级目录</a>
<ul>
<li><a href="#">三级目录</a>
<ul>
<li><a href="#">四级目录</a>
<ul>
<li><a href="#">五级目录</a>
<ul>
<li><a href="#">......</a>
<ul>
<li><a href="#">第n级目录</a>
<ul>
<li class="Child"><a href="#">叶结点1</a></li>
<!--Child Node-->
<li class="Child"><a href="#">叶结点2</a></li>
<li class="Child"><a href="#">叶结点3</a></li>
</ul>
</li>
<!--Sub Node n -->
</ul>
</li>
<!--Sub Node 6 -->
</ul>
</li>
<!--Sub Node 5 -->
</ul>
</li>
<!--Sub Node 4-->
</ul>
</li>
<!--Sub Node 3-->
</ul>
</li>
<!--Sub Node 2-->
</ul>
</li>
<!--Sub Node 1 -->
</ul>
</div>
<!-- CNLTreeMenu -->
<!--CNLTreeMenu1 End!-->
<script type="text/javascript">
<!--
function Ob(o){
var o=document.getElementById(o)?document.getElementById(o):o;
return o;
}
function Hd(o){
Ob(o).style.display="none";
}
function Sw(o){
Ob(o).style.display="";
}
function ExCls(o,a,b,n){
var o=Ob(o);
for(i=0;i<n;i++){o=o.parentNode;}
o.className=o.className==a?b:a;
}
function CNLTreeMenu(id,TagName0){
this.id=id;
this.TagName0=TagName0==""?"li":TagName0;
this.AllNodes = Ob(this.id).getElementsByTagName(TagName0);
this.InitCss = function (ClassName0,ClassName1,ClassName2,ImgUrl){
this.ClassName0=ClassName0;
this.ClassName1=ClassName1;
this.ClassName2=ClassName2;
this.ImgUrl=ImgUrl || "http://123.jialulu.com/images/s.gif";
this.ImgBlankA ="<img src=\""+this.ImgUrl+"\" class=\"s\" onclick=\"ExCls(this,'"+ClassName0+"','"+ClassName1+"',1);\" alt=\"展开/折叠\" />";
this.ImgBlankB ="<img src=\""+this.ImgUrl+"\" class=\"s\" />";
for (i=0;i<this.AllNodes.length;i++ ){
this.AllNodes[i].className==""?this.AllNodes[i].className=ClassName1:"";
this.AllNodes[i].innerHTML=(this.AllNodes[i].className==ClassName2?this.ImgBlankB:this.ImgBlankA)+this.AllNodes[i].innerHTML;
}
}
this.SetNodes = function (n){
var sClsName=n==0?this.ClassName0:this.ClassName1;
for (i=0;i<this.AllNodes.length;i++ ){
this.AllNodes[i].className==this.ClassName2?"":this.AllNodes[i].className=sClsName;
}
}
}
var MyCNLTreeMenu=new CNLTreeMenu("CNLTreeMenu","li");
MyCNLTreeMenu.InitCss("Opened","Closed","Child","http://123.jialulu.com/images/s.gif");
-->
</script>
</body>
</html>
㈦ 谁有详细php 下拉菜单代码
这是我自己用的一段代码,你自己改一下,点一下一级文本框,它所属的文本框会收起或伸展。
<SCRIPT language=javascript>
<!--
function menu_tree(meval)
{
var left_n=eval(meval);
if (left_n.style.display=="none")
{ eval(meval+".style.display='';"); }
else
{ eval(meval+".style.display='none';"); }
}
-->
</SCRIPT>
<TABLE class=navi cellSpacing=1 align=center border=0>
<TBODY>
<TR>
<TH>后台 >> 新闻分类</TH></TR></TBODY></TABLE><BR>
<table border=0 cellspacing=1 align=center class=form>
<tr>
<th colspan="2">添加分类</th>
</tr>
<form action="" method="post">
<tr>
<td colspan="2" align="center" height='30'>
<select name="fid">
<option value="0">添加大类</option>
<?php
$query=$db->findall("shipin where fid=0");
while ($row=$db->fetch_array($query)){
$new_class_arr[$row[id]]=$row[name];
echo "<option value=\"$row[id]\">$row[name]</option>";
}
?>
</select>
<input type="text" name="name" value="">
<input type="submit" name="into_class" value="添加分类"/>
</td>
</form>
</tr>
</table>
<br>
<table border=0 cellspacing=1 align=center class=form>
<tr>
<th colspan="2">系统分类</th>
</tr>
<?php
foreach ($new_class_arr as $id=>$val){
?>
<tr>
<td onClick ="javascript:menu_tree('left<?=$id ?>');" >
<form action="" method="post">
<IMG src="images/menu.gif" align=absMiddle border=0>
<input type="hidden" name="id" value="<?php echo $id?>">
<input type="text" name="name" value="<?php echo $val?>">
<input type="submit" name="update_class" value="更新"/>
<input type="button" value="删除" onclick="location.href='?del=<?php echo $id?>'"/>
</form>
</td>
</tr>
<tr id=left<?=$id ?>>
<td>
<table >
<tbody>
<?php
$query_fid=$db->findall("shipin where fid=$id");
while ($row_fid=$db->fetch_array($query_fid)){
?>
<tr>
<td>
<form action="" method="post">
<IMG src="images/menu.gif" align=absMiddle border=0>
<input type="hidden" name="id" value="<?php echo $row_fid[id]?>">
<input type="text" name="name" value="<?php echo $row_fid[name]?>">
<input type="submit" name="update_class" value="更新"/>
<input type="button" value="删除" onclick="location.href='?del=<?php echo $row_fid[id]?>'"/>
</form>
</td></tr>
<?php }?>
</tbody>
</table>
</td>
</tr>
<?php
}
?>
</table>
</BODY></HTML>