Select in HTML (multiple persons and departments are selected)

Source: Internet
Author: User

<% @ Page import = "Java. SQL. *" %>
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "entity. *" %>

<% /*!
Public String Tochi (string input ){
Try {
Byte [] bytes = input. getbytes ("ISO8859-1 ");
Return new string (bytes );
} Catch (exception ex ){
}
Return NULL;
}
*/
%>

<%
String pid = "";
String XM = "";
String deptid = "";
String telno = "";
String zjhm = "";
String dept_id = "";
String dept_name = "";

List listemployee = new arraylist ();
List listdept = new arraylist ();

Try {
Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
String url = "JDBC: oracle: thin: @ 10.1.2.103: 1521: webtest ";
// Orcl is the SID of your database
String user = "wsbs ";
String Password = "wsbs ";
Connection conn = drivermanager. getconnection (URL, user, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );

String SQL = "select PID, zjhm, XM, dep_id, telno from star_listview ";
String SQL _dept = "select dept_id, dept_name from star_dept ";

Resultset rs1_stmt.exe cutequery (SQL );


While (Rs. Next ())
{

Listview Vo = new listview ();

VO. setpid (string) Rs. getstring ("PID "));

VO. setxm (string) Rs. getstring ("XM "));

VO. setdept_id (string) Rs. getstring ("dep_id "));

VO. settelno (string) Rs. getstring ("telno "));

VO. setzjhm (string) Rs. getstring ("zjhm "));

Listemployee. Add (VO );

Out. println ("PID:" + Rs. getstring ("PID "));
Out. println ("XM:" + Rs. getstring ("XM "));
Out. println ("dept_id:" + Rs. getstring ("dep_id "));
Out. println ("telno:" + Rs. getstring ("telno "));
Out. println ("zjhm:" + Rs. getstring ("zjhm") + "<br> ");

Vo = NULL;

}

Rs1_stmt.exe cutequery (SQL _dept );

Out. println ("------------- Department -------------- <br> ");
While (Rs. Next ())
{
Dept dept1 = new dept ();
Dept1.setdept _ ID (Rs. getstring ("dept_id "));
Dept1.setdept _ name (Rs. getstring ("dept_name "));

Out. println ("department ID:" + Rs. getstring ("dept_id "));
Out. println ("department name:" + Rs. getstring ("dept_name") + "<br> ");
Listdept. Add (dept1 );
Dept1 = NULL;
}
Rs. Close ();
Stmt. Close ();
Conn. Close ();
} Catch (exception E)
{

}
%>

<HTML>
<Head>
<Title> untitled document </title>

 

<Script language = "JavaScript">
Function Test (){
VaR STR = '2014 @ 3 ';
VaR indexnumber = Str. lastindexof ('@');
VaR departid = Str. substring (indexnumber + 1, str. Length );
Alert ('departid = '+ departid );
}

Function remove (){
VaR selectindex = Document. form1.select2. selectedindex;
 
// Alert (selectindex );
VaR I _temp = parseint (selectindex );
// Alert (I _temp );
// Alert (I _temp );
// Alert (isnan (selectindex ));
If (I _temp! =-1 & I _temp! = 0)
{
Document. form1.select2. Options. Remove (selectindex );
}
}

Function display (){
VaR selectindex = Document. form1.select1. selectedindex; // obtain the selected sequence
VaR datas1 = form1.select1. options;
VaR opt = Document. createelement ("option ");
Opt. Text = datas1 [selectindex]. text;
Opt. value = datas1 [selectindex]. value;
Alert ('text = '+ datas1 [selectindex]. Text );
Alert ('value = '+ datas1 [selectindex]. value );
 
 

}

Function add (){
VaR selectindex = Document. form1.select1. selectedindex; // obtain the selected sequence
VaR datas1 = form1.select1. options; // datas1 is select1

VaR hasrec = false;

If (selectindex! =-1) {// when select1 is selected
If (! Isdepart (datas1 [selectindex]. Value) {// if it is a department
For (VAR I = 0; I <datas1.length; I ++ ){

If (selectindex = I ){
Continue;
}
If (getdepartid (datas1 [I]. Value) = datas1 [selectindex]. Value) {// determine whether the employee belongs to this department.

Hasrec = false;
For (var j = 0; j <document. form1.select2. length; j ++) // you can determine whether there are duplicates in select2.
{
// Alert (datas1 [I]. Value + '|' + datas1 [I]. Text + '|' + document. form1.select2 [J]. value );

If (hasrec)
Break;

If (datas1 [I]. value = Document. form1.select2 [J]. value)
{
// Alert ('Repeat '+ datas1 [I]. Text + ''+ document. form1.select2 [J]. Text );
Hasrec = true;
}
Else
// Alert ('Do not repeat '+ datas1 [I]. Text + ''+ document. form1.select2 [J]. Text );
Hasrec = false;

}
If (! Hasrec)
{
VaR opt = Document. createelement ("option ");
Opt. Text = datas1 [I]. text;
Opt. value = datas1 [I]. value;
Opt. Selected = false;
Document. form1.select2. Options. Add (OPT );
}
}
}
} Else // if it is an individual
{
If (form1.select2. Options. Length <1) // If select2 is empty:
{
VaR opt = Document. createelement ("option ");
Opt. Text = Document. form1.select1 [selectindex]. text;
Opt. value = Document. form1.select1. value;
Opt. Selected = false;
Document. form1.select2. Options. Add (OPT );
}
Else // The value is not empty. Check whether select2 already exists.
{
VaR flag = 0; // For flag
VaR exitfor = 0; // same exit flag
For (var j = 0; j <form1.select2. Options. length; j ++)
{
If (exitfor = 1)
Break;
// Alert ('select2: '+ form1.select2 [J]. Value + 'select1:' + form1.select1 [selectindex]. Value + 'exitfor: '+ exitfor );
If (form1.select2 [J]. value! = Form1.select1 [selectindex]. value)
{
// Alert ('None ');
Flag = 1;
}
Else
{
// Alert ('same, exit !! ');
Exitfor = 1;
Flag = 0;
}
// Alert ('flag = '+ flag + 'exitfor: =' + exitfor );
}
If (flag = 1)
{
VaR opt = Document. createelement ("option ");
Opt. Text = Document. form1.select1 [selectindex]. text;
Opt. value = Document. form1.select1. value;
Opt. Selected = false;
Document. form1.select2. Options. Add (OPT );
}
}
}
}
}

Function isdepart (value) {// determines whether it is an individual. If yes, Tur, department, and flase are returned.
If (value. indexof ('@')! =-1 ){
Return true;
} Else {
Return false;
}
}

/* Copy
Function add (){
VaR selectindex = Document. form1.select1. selectedindex; // obtain the selected sequence
VaR datas1 = form1.select1. options;
VaR opt = Document. createelement ("option ");
Opt. Text = datas1 [selectindex]. text;
Opt. value = datas1 [selectindex]. value;
 
Document. form1.select1. Options. Remove (selectindex );
Document. form1.select2. Options. Add (OPT );
}*/

// Get the departideg getdepartid ('1970 @ 1') of the department and return 1
Function getdepartid (STR ){
VaR indexnumber = Str. lastindexof ('@');
// Alert (indexnumber );
VaR departid = Str. substring (indexnumber + 1, str. Length );
// Alert ('------' + departid );
Return departid;
}

Function display (){
VaR selectindex = Document. form1.select2. selectedindex;
VaR d_temp = parseint (selectindex );
If (d_temp! =-1)
{
VaR datas = form1.select2. options;
// Alert ('select2 text = '+ datas [selectindex]. text + 'value = '+ datas [selectindex]. value + 'size = '+ document. form1.select2. length );
}
}

Function deleteall ()
{
VaR size = Document. form1.select2. Options. length;
// Alert ('select2 size = '+ size );
VaR v_select = Document. form1.select2. options;
 
If (size> 0)
{
// Alert ('length: '+ v_select );
For (var ii = 0; II <size; II ++)
{
Document. form1.select2. Options. Remove (0 );
}
}
VaR opt = Document. createelement ("option ");
Opt. Text = "";
Opt. value = "";
Opt. Selected = false;
Document. form1.select2. Options. Add (OPT );
}

Function submitform ()
{
VaR temp = Document. form1.select2. length;
 
// Alert (document. form1.textcontent. Trim ());
If (temp <2)
{
Alert ('select a person or department !!!! ');
}
Else
{
If (TRIM ())
{
For (var c = 0; C <temp; C ++)
{
Document. form1.select2 [C]. Selected = true;
}
Form1.submit ();
}
Else
{
Alert ('the sent content cannot be blank !!! ');
}
}
}

// Determine whether there is a value (regular expression)
Function trim (){

VaR temp = Document. form1.textcontent. value. Replace (/(^/S *) | (/S * $)/g ,"");
If (temp = "")
Return false;
Else
Return true;
}

</SCRIPT>
</Head>
<Body>

<% /*
String s_value = "";
String [] temp = (request. getparametervalues ("select2") = NULL )? Null :( string []) request. getparametervalues ("select2 ");
// System. Out. println ("----");
Out. println ("<br> after submission: <br> ");
If (temp! = NULL)
{
For (int K = 1; k <temp. length; k ++)
{
S_value = temp [k];
// Out. println ("@:" + s_value.indexof ('@') + "================ ");;
Out. println ("PID:" + s_value.substring (0, s_value.indexof ("@")));
Out. println ("dept_id:" + s_value.substring (s_value.indexof ("@") + 1, s_value.length () + "<br> ");
// Out. println (temp [k]);
}
}
Out. println ("<br> content:" + Tochi (string) request. getparameter ("textcontent ")));
*/
%>

<Form name = "form1" method = "Post" Action = "insertlist. jsp">
<P>
<Table width = "731" border = "0" align = "center">
<Tr>
<TD width = "231" rowspan = "8"> <select name = "select1" size = "15">
<%

For (int K = 0; k <listemployee. Size (); k ++)
{
Listview V1 = (listview) listemployee. Get (k );

PID = v1.getpid ();
XM = v1.getxm ();
Dept_id = v1.getdept _ ID ();
Telno = v1.gettelno ();
Zjhm = v1.getzjhm ();

Out. println ("<option value = '" + zjhm + "@" + dept_id + "'>" + XM + "& nbsp; "+ telno +" </option> ");

}
For (Int J = 0; j <listdept. Size (); j ++)
{
Dept temp_dept = (Dept) listdept. Get (j );
Deptid = temp_dept.getdept_id ();
Dept_name = temp_dept.getdept_name ();

Out. println ("<option value = '" + deptid + "'>" + dept_name + "</option> ");
}
%>
<! --
<Option value = "200008037071810 @ 2"> ye Ping 1239485768999 </option>
<Option value = "200008000022470 @ 1"> Mao yunjia 1239485768999 </option>
<Option value = "200008000046200 @ 1"> Zhu Minmin 1239485768999 </option>
<Option value = "200008000055910 @ 1"> Qu yuehong 1239485768999 </option>
<Option value = "200008000239270 @ 2"> Zhu Huiying 1239485768999 </option>
<Option value = "200008000590020 @ 1"> wowenyun 1239485768999 </option>
<Option value = "200211153082200 @ 1"> porn 1239485768999 </option>
<Option value = "200008000046110 @ 1"> Zhang Yin 1239485768999 </option>
<Option value = "200008000046300 @ 1"> Ma jingzhi 1239485768999 </option>

<Option value = "1"> system_dept </option>
<Option value = "2"> general_dept </option>

-->
</SELECT> </TD>
<TD width = "170"> <input type = "button" value = ">" onclick = "javascript: add ();"> </TD>
<TD width = "316" rowspan = "8"> <select name = "select2" size = "15" multiple>
<Option value = ""> & nbsp; & nbsp; </option>
</SELECT> </TD>
</Tr>
<Tr>
<TD> <input type = "button" value = "delete a record" onclick = "javascript: Remove ();"> </TD>
</Tr>
<Tr>
<TD> <! -- <Input type = "button" value = "display" onclick = "javascript: Display ();"> -->
</TD>
</Tr>
<Tr>
<TD> <input type = "button" value = "delete all" onclick = "javascript: deleteall ();"> </TD>
</Tr>
<Tr>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD colspan = "3">
<Div align = "Left">
<P>
<Textarea name = "textcontent" Cols = "80" rows = "10"> </textarea>
<Input type = "button" value = "OK" name = "OK" onclick = "javascript: submitform ();">
</P>
</Div> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

--------------------------------------------------- Listview. Java file ---------------------

GET/set (PID, zjhm, XM, dept_id, telno)

Database Structure:

PID varchar2 (15) y
Zjhm varchar2 (18) y
XM varchar2 (10) y
Dep_id varchar2 (5) y
Telno number (13) y

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.