jsp+javascript打造级连菜单

2004-01-15 10:35:42  作者

%26lt;%@ page import="java.util.Date,yava.fileapp.*,java.sql.*;"
contentType="text/html;charset=GB2312" pageEncoding="GB2312"
%%26gt;
%26lt;style%26gt;
.f9{ font-size:9pt; }
.bgc{ background-color:#AECAF9; color: #0033FF }
.buttons{font-family:Arial; font-size:13px; font-weight:bold; background-color:#6796E4; color:white; border-top: solid 2px #AACAFB;
border-bottom: solid 1px #4E7DC1;
border-left: solid 1px #AECAF9;
border-right: solid 1px #5679BD;
padding:1px;
margin:0px;}
%26lt;/style%26gt;
%26lt;SCRIPT LANGUAGE="JavaScript"%26gt;
%26lt;!--
function rv()
{
var val="";
for(i=0;i%26lt;combo_box.list2.length;i++){
val+=","+combo_box.list2[i].value;
}
if(val.charAt(0)==","){
val=val.substr(1,val.length);
}
opener.form1.frecname.value=val;
self.close();
}

//--%26gt;
%26lt;/SCRIPT%26gt;
%26lt;body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"%26gt;
%26lt;jsp:useBean id="user" scope="session" class="yava.fileapp.UserBean" /%26gt;
%26lt;%
CDataSource ds=new CDataSource(); //数据联结Bean实例
java.sql.Connection conn=ds.getConnection();
java.sql.Statement stmt=null;
java.sql.ResultSet rs=null;
CDataCheck dc=new CDataCheck();
%%26gt;

%26lt;%
String sqlu="SELECT t1.fno, t1.fname, t2.fname AS fdept FROM tuser t1 LEFT OUTER JOIN TDept t2 ON t1.fdept = t2.fno order by t2.fname";
stmt=conn.createStatement();
rs=stmt.executeQuery(sqlu);
%%26gt;
%26lt;script language='Javascript'%26gt;
arr = new Array();
%26lt;% int temp=0;
while(rs.next())
{
%%26gt;
arr[%26lt;%=temp%%26gt;]=new Array("%26lt;%=rs.getString("fname")%%26gt;","%26lt;%=rs.getString("fdept")%%26gt;");
%26lt;%
temp = temp + 1;
}
%%26gt;
temp=%26lt;%=temp%%26gt;;
function ChangeLocation(id){
document.combo_box.city.length=0; //初始化第2级菜单的长度,下标从0开始
var i = 0;
document.combo_box.city.options[0]=new Option('-------','');
for(i=0;i%26lt;temp;i++){
if(arr[i][1]==id){//假如相等,证实在第2级里面有输入第1级组织的子集,arr[总数目][部门]
document.combo_box.city.options[document.combo_box.city.length] = new Option(arr[i][0], arr[i][0]);
}
}
}
%26lt;/script%26gt;


%26lt;form name="combo_box"%26gt;
%26lt;table border="0" cellspacing="0" cellpadding="0" height="210" width="59"%26gt;
%26lt;tr height="24"%26gt;
%26lt;td bgcolor="#336699" class="buttons" align=center%26gt;%26lt;font color=#ffffff%26gt;部门选择%26lt;/font%26gt;%26lt;/td%26gt;
%26lt;td bgcolor="#336699" class="buttons" align=center%26gt;%26lt;font color=#ffffff%26gt;人名选择%26lt;/font%26gt;%26lt;/td%26gt;
%26lt;td bgcolor="#336699" class="buttons" align=center nowrap%26gt;%26lt;font color=#ffffff%26gt;添加/移除%26lt;/font%26gt;%26lt;/td%26gt;
%26lt;td bgcolor="#336699" class="buttons" align=center%26gt;%26lt;font color=#ffffff%26gt;最终人%26lt;/font%26gt;%26lt;/td%26gt;
%26lt;/tr%26gt;
%26lt;tr%26gt;
%26lt;td%26gt;
%26lt;select multiple id=prov name="prov" onchange="ChangeLocation(combo_box.prov.options[combo_box.prov.selectedIndex].text);" style="width:150;height:200" class="bgc"%26gt;
%26lt;option value="0"%26gt;请选择部门 ---%26gt;%26lt;/option%26gt;
%26lt;%
String sqld="SELECT * FROM tdept";
stmt=conn.createStatement();
rs=stmt.executeQuery(sqld);
while(rs.next())
{
%%26gt;
%26lt;option name="%26lt;%=rs.getString("fno")%%26gt;"%26gt;%26lt;%=rs.getString("fname")%%26gt;%26lt;/option%26gt;
%26lt;%
}
%%26gt;
%26lt;/select%26gt;
%26lt;/td%26gt;
%26lt;td%26gt;
%26lt;select multiple id=city style="width:150;height:200" class="bgc"%26gt;
%26lt;/select%26gt;
%26lt;/td%26gt;
%26lt;td nowrap align="center" class="bgc"%26gt;
%26lt;input type="button" onClick="move(this.form.list2,this.form.city)" value="%26lt;%26lt;" class="buttons"%26gt;
%26lt;input type="button" onClick="move(this.form.city,this.form.list2)" value="%26gt;%26gt;" class="buttons"%26gt;
%26lt;/td%26gt;
%26lt;td%26gt;
%26lt;select multiple size="10" name="list2" style="width:150;height:200" class="bgc"%26gt;
%26lt;/select%26gt;
%26lt;/td%26gt;
%26lt;/tr%26gt;
%26lt;tr class="bgc"%26gt;
%26lt;td colspan="4" align="center"%26gt;%26lt;input type="button" name="button1" class="buttons" value="选好了!" onclick="rv();"%26gt;%26lt;/td%26gt;
%26lt;/tr%26gt;
%26lt;/table%26gt;
%26lt;/form%26gt;

%26lt;script language="JavaScript"%26gt;
//人名移动
function move(fbox, tbox) {
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i %26lt; tbox.options.length; i++) {
arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i %26lt; fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected %26amp;%26amp; fbox.options[i].value != "") {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].text;
fLength++;
}
}
arrFbox.sort();
arrTbox.sort();
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c %26lt; arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
}
for(c = 0; c %26lt; arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
}
}

%26lt;/script%26gt;
%26lt;/body%26gt;