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>