你的问题,总之就是一个页面的无闪烁刷新的问题.如果不熟悉xml,另有一解:<script> function test(){ var strConnString = "Provider=OraOLEDB.Oracle;Data Source=rz817;User ID=hr_system;Password=manager;PLSQLRset=1" var conn = new ActiveXObject("ADODB.Connection"); conn.open(strConnString); var rs = conn.execute("SELECT * FROM hr_bmxx"); var s=""; while (!rs.EOF) { for (var i=0; i < rs.fields.count; i++) { s+=rs.fields(i).name + ":" + rs.fields(i).value+"<br>"; } s+=("<p></p>"); rs.moveNext(); }
就是树状结构一开始只是显示第一层的内容,点击你想展开的选项,它将子项自动加载出来。我以前做过树状结构,但那是一开始就遍历了所有节点,当子项很多的时候,刷新的速度会很慢!我看csdn的网页,它是用链接的方法,但是怎么显示的,请高手给我一个例子!
可以先只显示一级节点,然后在触发点击事件后,通过XMLHTTP对象,传送当前节点代码至后台的jsp或者servlet,查询该节点的所有子节点,以xml dom的形式返回客户端.客户端使用DHTML技术,用一个层将查询结构显示成树状结构即可.
我是用javascript简单的实现,不知道合不合你胃口!
格式有点乱,因为从我的网站源文件粘贴过来,请见谅!源代码:
script :
<script language="JavaScript1.2">
scores = new Array(20);
var numTotal=0;
NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
ver4 = (NS4 || IE4) ? 1 : 0;
if (ver4) {
with (document) {
write("<STYLE TYPE='text/css'>");
if (NS4) {
write(".parent {position:absolute; visibility:visible}");
write(".child {position:absolute; visibility:visible}");
write(".regular {position:absolute; visibility:visible}")
}
else {
write(".child {display:none}")
}
write("</STYLE>");
}
}function getIndex(el) {
ind = null;
for (i=0; i<document.layers.length; i++)
{ whichEl = document.layers[i];
if (whichEl.id == el) {
ind = i;
break;
}
}
return ind;
}function arrange() {
nextY = document.layers[firstInd].pageY +document.layers[firstInd].document.height;
for (i=firstInd+1; i<document.layers.length; i++) {
whichEl = document.layers[i];
if (whichEl.visibility != "hide") {
whichEl.pageY = nextY;
nextY += whichEl.document.height;
}
}
}function initIt(){
if (!ver4) return;
if (NS4) {
for (i=0; i<document.layers.length; i++) {
whichEl = document.layers[i];
if (whichEl.id.indexOf("Child") != -1) whichEl.visibility = "hide";
}
arrange();
}
else {
divColl = document.all.tags("DIV");
for (i=0; i<divColl.length; i++) {
whichEl = divColl(i);
if (whichEl.className == "child") whichEl.style.display = "none";
}
}
}function expandIt(el)
{
if (!ver4) return;
if (IE4) {
whichEl1 = eval(el + "Child");
for(i=1;i<=numTotal;i++){
whichEl = eval(scores[i] + "Child");
if(whichEl!=whichEl1) {
whichEl.style.display = "none";
}
}
whichEl1 = eval(el + "Child");
if (whichEl1.style.display == "none") {
whichEl1.style.display = "block";
}
else {
whichEl1.style.display = "none";
}
}
else {
whichEl = eval("document." + el + "Child");
for(i=1;i<=numTotal;i++){
whichEl = eval("document." + scores[i] + "Child");
if(whichEl!=whichEl1) {
whichEl.visibility = "hide";
}
}
if (whichEl.visibility == "hide") {
whichEl.visibility = "show";
}
else {
whichEl.visibility = "hide";
}
arrange();
}
}onload = initIt;
</script>表格中:
<td width="145" valign="top" rowspan="2"> <br><font size="2">
<div id='KB1Parent' class='parent' > <br>
<p><a href="#" onClick="expandIt('KB1'); return false" onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">中心计划</a>
</div>
<div id='KB1Child' class='child'> <a href='zx/zx2002jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0 width="22" height="22">2002年中心计划</a><br>
<a href='zx/zx2001jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0 width="22" height="22">2001年中心计划</a>
</div>
<div id='KB2Parent' class='parent'><a href="#" onClick="expandIt('KB2'); return false" onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">报案中心
</a></div>
<div id='KB2Child' class='child'> <a href='bazx/bazx2002jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000"> <IMG SRC='../image/substract.gif' BORDER=0>2002年报案中心计划</a><br>
<a href='bazx/bazx2001jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000"> <IMG SRC='../image/substract.gif' BORDER=0>2001年报案中心计划</a></div>
<div id='KB3Parent' class='parent'><a href="#" onClick="expandIt('KB3'); return false" onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">定损部</a></div>
<div id='KB3Child' class='child'><a href='./dsb/dsb2002jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000"> <IMG SRC='../image/substract.gif' BORDER=0>2002年定损部计划</a><br>
<a href='./ds1b/ds1b2001jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000"> <IMG SRC='../image/substract.gif' BORDER=0>2001年定损一部计划</a><br>
<a href='./ds2b/ds2b2001jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000"> <IMG SRC='../image/substract.gif' BORDER=0>2001年定损二部计划</a></div>
<div id='KB4Parent' class='parent'><a href="#" onClick="expandIt('KB4'); return false" onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">报价中心</a></div>
<div id='KB4Child' class='child'> <a href='bjzx/bjzx2002jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>2002年报价中心计划</a><br>
<a href='bjzx/bjzx2001jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>2001年报价中心计划</a></div>
<div id='KB5Parent' class='parent'><a href="#" onClick="expandIt('KB5'); return false" onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">95518专线</a></div>
<div id='KB5Child' class='child'> <a href='95518/95518zx2002jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>2002年专线计划</a></div>
<div id='KB6Parent' class='parent'><a href="#" onClick="expandIt('KB6'); return false" onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">综合部</a></div>
<div id='KB6Child' class='child'> <a href='zhb/zhb2002jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>2002年综合部计划</a><br>
<a href='zhb/zhb2001jh.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>2001年综合部计划</a></div>
<div id='KB7Parent' class='parent'><a href="#" onClick="expandIt('KB7'); return false" onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">值班表</a></div>
<div id='KB7Child' class='child'> <a href='zbb/nqryzbb.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>02年行政内勤值班表</a><br>
<a href='zbb/ddnqryzbb.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>02年调度内勤值班表</a><br>
<a href='zbb/wqcc/20020301.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>02年查勘外勤值班表</a><br>
<a href='zbb/wqdd/20020301.htm' target='Content' onmouseover=this.style.color="#ff0000" onmouseout=this.style.color="#000000">
<IMG SRC='../image/substract.gif' BORDER=0>02年定点待命值班表</a></div>
<SCRIPT>numTotal=7;scores[1]='KB1';scores[2]='KB2';scores[3]='KB3';scores[4]='KB4';scores[5]='KB5';scores[6]='KB6';scores[7]='KB7'</SCRIPT>
</font> </td>
*创建一个XMLHTTP对象,并将鼠标点击节点的id传送给后台servlet
*/
function createXMLHTTP("鼠标点击节点的id"){
var httpob=new ActiveXObject("Microsoft.XMLHTTP");
httpob.Open("POST","/rlzyservlet/rlzy.servlet.XmlSqlServlet?nodeId="+鼠标点击节点的id,false);
httpob.send("");
var V_xmlDom=httpob.responseXML;
}每一节点下面设一不可见的层div_nodeId,innerText="正在加载……".鼠标点击当前节点时,将div_nodeId置为可见,并触发createXMLHTTP()方法.页面将转入后台运行.
后台的jsp或者servlet将contentType="text/xml; charset=GB2312",接收参数nodeId,查询数据库得子节点集合vec;然后将vec以符合xml规范的字符串形式的方式写回客户端,也就是V_xmlDom对象了.
现在操作V_xmlDom对象,将其代替层div_nodeId的innerHTML,即大功告成
其实,你用JS实现很方便!
网上有现成的!一搜一大把!!
:)
function test(){
var strConnString = "Provider=OraOLEDB.Oracle;Data Source=rz817;User ID=hr_system;Password=manager;PLSQLRset=1"
var conn = new ActiveXObject("ADODB.Connection");
conn.open(strConnString);
var rs = conn.execute("SELECT * FROM hr_bmxx");
var s="";
while (!rs.EOF)
{
for (var i=0; i < rs.fields.count; i++)
{
s+=rs.fields(i).name + ":" + rs.fields(i).value+"<br>";
}
s+=("<p></p>");
rs.moveNext();
}
rs.close();
conn.close();
conn = null;
a.innerHTML=s;
//alert(a.innertText);
}
</script>
<input type=button value=test onclick=test()>
<div id=a style="display:block;background-color:#cccccc;height:400;width:500"></div>
注:
此处用js连接oracle数据库的代码,我是于csdn上saucer(思归)的代码稍加改动而得到的.
当然,此法可能因无法使用连接池(是否可以?请高手指教),而速度较之稍慢一些
所以, patriot_cool() ,看来,还非得用xml不可.以下是我的项目组用来生成xml的一个servlet,接收的参数为查询用的sqlstr以及xml的一个基本节点的引子root.
package lr_servlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import lr_bean.*;public class MakeXML extends HttpServlet {
private static final String CONTENT_TYPE = "text/xml";
private static final String CONTENT_ENCODING = "<?xml version=\"1.0\" encoding=\"gb2312\" ?>";
private OutputStreamWriter ow;
/**Initialize global variables*/
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException {
String sqlstr=req.getParameter("sqlstr");
String Headstr=req.getParameter("ROOT");
ServletOutputStream out = res.getOutputStream();
ow = new OutputStreamWriter(out,"GB2312");
if (sqlstr==null){
ow.write("我没有取sqlstr中的SQL语句");
ow.flush();
return;
}
DBoperationForRs dbop=new DBoperationForRs();
Vector vdata=new Vector();
Vector vhead=new Vector();
try {
vdata=dbop.selectsql(sqlstr,vhead);
if (vdata==null||vdata.size()==0){
ow.write("生成树时我没有取到数据"+sqlstr);
ExceptionErr_log.log("生成树时我没有取到数据"+sqlstr);
ow.flush();
// return;//如果没有从数据库中取道数据,则将参数“ROOT”作为唯一节点写回客户端
}
//我开始输出XML文件
res.setContentType(CONTENT_TYPE);
ow.write(CONTENT_ENCODING);
ow.write("\n<classes>\n");
ow.write(Headstr);
for (int i=0;i<vdata.size();i++) {
Vector oneRecord=(Vector)vdata.elementAt(i);
ow.write("<class>\n");
for (int j=0;j<vhead.size();j++){
Vector oneCol=(Vector)vhead.elementAt(j);
ow.write("<!--"+(String)oneCol.elementAt(1)+"-->");
ow.write("<"+(String)oneCol.elementAt(0)+">"+(String)oneRecord.elementAt(j)+"</"+(String)oneCol.elementAt(0)+">\n");
}
ow.write("</class>\n");
}
ow.write("</classes>\n");
}
catch (Exception e) {
ow.write("生成XML文件时出现了异常错误"+e.getMessage());
ExceptionErr_log.log(e,"生成树XML文件时出现了异常错误");
//ExceptionErr_log.log(vdata.toString());
ow.flush();
}
finally {
dbop.freeConnection();
}
ow.flush();
}
/**Clean up resources*/
public void destroy() {
}
}
XML是后台生成的,作为一个包传过来,用的是远程调用的方法(corba),不过这些都不要管。我现在就是如果得到了这个XML包,怎么做到动态加载,像你前面所说的,替换调隐藏的那个子节点,是怎么做到的,有简单的小例子最好了!
有关该对象的用法,你可以去查询一下微软的msdn,这里列出一些常用的方法:
id节点集合:id = V_xmlDom.getElementsByTagName("ID");//ID
id节点集合的第no个节点:id1=id.item(no) (<ID V1="456456">1423456</ID>)
id节点集合的第no个节点的V1属性:v1=id1.getAttribute("V1")
然后用这些值,生成树形结构的html字符串ss,令层div_nodeId.innerHTML=ss;
import javax.swing.tree.*;
import java.awt.*;public class Mytree extends JFrame
{
public Mytree()
{
Container con=getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("c:\\");
DefaultMutableTreeNode t1=new DefaultMutableTreeNode("dos");
DefaultMutableTreeNode t2=new DefaultMutableTreeNode("java");
DefaultMutableTreeNode t1_1=new DefaultMutableTreeNode("applet");
DefaultMutableTreeNode t1_2=new DefaultMutableTreeNode("jre");
root.add(t1);
root.add(t2);
t1.add(t1_1);
t1.add(t1_2);
JTree tree=new JTree(root);
JScrollPane scrollpane=new JScrollPane(tree);
con.add(scrollpane);
}
}
<script>
var V_html="ID:20021118023808<br>BMBH:01<br>BMMC:一水厂<br>SSBM:default<br>DD:null<br>FZR:null<br>LXDH:null<br>YJDZ:null<br>BMZL:null<br>ZYBZ:null<br><p></p>ID:20021118023810<br>BMBH:09<br>BMMC:营业部<br>SSBM:default<br>DD:null<br>FZR:null<br>LXDH:null<br>YJDZ:null<br>BMZL:null<br>ZYBZ:null<br><p></p>ID:20021118023811<br>BMBH:11<br>BMMC:华博公司<br>SSBM:default<br>DD:null<br>FZR:null<br>LXDH:null<br>YJDZ:null<br>BMZL:null<br>ZYBZ:null<br><p></p>";function test(){
a.innerHTML=V_html;
}
</script>
<input type=button value=test onclick=test()>
<div id=a style="display:block;background-color:#cccccc;height:400;width:500"></div>大概就这样子了,你自己作一下试验吧
不管多么庞大的书目录,这个字段足够设计出你需要的节点层数,csdn时使用asp+xml制作的属性菜单。
正如july_typhoon(加油的风) 所说的使用XMLHTTP对象,传送当前节点代码至后台的jsp或者servlet,(CSDN使用asp)查询该节点的所有子节点(这里可以查询对应子节点字段内容的数据),以xml dom的形式返回客户端.客户端使用DHTML技术,用一个层(或者表格等等TAG)将查询结构显示成树状结构即可.
首先,httpob.Open("POST","/rlzyservlet/rlzy.servlet.XmlSqlServlet?nodeId="+鼠标点击节点的id,false)中的参数,第一个是servlet中的方法名吗?第二个是class文件名,还是.shtml文件名,或者就是如/servlet1这种。
然后,你给我的程序中得到xml文件的方法是service(),它把xml输入到管道中去,让js中的object自己拿吗?
最后,var V_xmlDom=httpob.responseXML,怎么取到XML文件,是V_xmlDom.value还是别的。先给你100分,进来拿吧!谢谢
"rlzyservlet"是我们在IAS上配的servlet路径,你可以根据自己服务器具体去配置servlet的使用."rlzy.servlet.XmlSqlServlet"是请求的servlet,
nodeId="+鼠标点击节点的id 是传送给XmlSqlServlet的节点参数.在servlet里,接受nodeId参数,并根据此参数查询该节点的子节点,将这些子节点的信息组成xml形式的字符串,然后直接用out(ServletOutputStream)写回客户端就是了.到客户端后,xml形式的字符串就以xml dom的形式封装到V_xmlDom里面了.最后用DHTML技术操作V_xmlDom,把子节点信息用html代码树形结构就成了