Click to See Complete Forum and Search --> : Dynamically creating SELECT box


gnanesh
07-15-2003, 06:53 AM
folks

I have a JSP application, From the backend i need to populate particular columns into an SELECT boxes dynamically, If any one knows please let me know, If you could post some code that would be really great

Thanks & Regards
GG

Khalid Ali
07-15-2003, 09:38 AM
I hope this example helps

http://68.145.35.86/dev/jsp/dynamicListBox.jsp

gnanesh
07-15-2003, 09:47 AM
hi

thanks for your reply, by the way let me know how do i populate the option list with an returned Hashtable below:-

Hashtable entity = Entity.getEntities();
Enumeration enum = entity.keys();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
Entity en = (Entity) entity.get(key);

}
String entityName = en.entityName;

This will return me 20 values , but how do i populate to the option list with all these returned values

Regards
GG

Khalid Ali
07-15-2003, 09:53 AM
String htmlStr = "<select name=\"listBox\" onchange=\"Process(this.options[this.selectedIndex].value);\">";
htmlStr +=" <option value=\"-1\" >Please Select One</option>";

Hashtable entity = Entity.getEntities();
Enumeration enum = entity.keys();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
Entity en = (Entity) entity.get(key);
String entityName = en.entityName;
htmlStr +=" <option value=\""+entityName +"\" >"+entityName +"</option>";

}
//and once all that is done you can use the htmlStr variable to print it where ever you want

<%=htmlStr %>

gnanesh
07-15-2003, 10:40 AM
I got it before , but i am getting error if i do like below:-

<div align="center">
<%
String arg ="contact";
ViewableCollection stuff = ResourceBroker.getObjects(arg);
DynamicObject dyno = (DynamicObject) stuff.get(0);
String html = stuff.toHtml();
System.out.println("HTML OUTPUT:" + html);
Hashtable entity = Entity.getEntities();
Enumeration enum = entity.keys();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
Entity en = (Entity) entity.get(key);
String name = en.entityName;
System.out.println("Entity Types:" + en.entityName);
String htmlStr = "<select name=\"listBox\" onchange=\"Process(this.options[this.selectedIndex].value);\">";
htmlStr +=" <option value=\"-1\" >Please Select One</option>";
htmlStr +=" <option value=\""+name +"\" >"+name+"</option>";
System.out.println("HTML String:" + htmlStr);
}
%>
<%=htmlStr%> // HERE i am getting error if i do this
</div><p>


The error ouput below:-

Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] C:\Tomcat 4.1\work\Standalone\localhost\Meta\NetTest_jsp.java:204: cannot resolve symbol
[javac] symbol : variable htmlStr
[javac] location: class org.apache.jsp.NetTest_jsp
[javac] out.print(htmlStr);
[javac] ^
[javac] 1 error

Let me know where i am doing wrong !!\

Regards
GG

gnanesh
07-15-2003, 11:30 AM
Below code is the latest one which i tried, Its working fine , But getting SELECT boxes for each OPTION'S

<form id="form1" action="" onsubmit="" style="margin:auto;">
<p style="text-align:center">
<%
String arg ="contact";
ViewableCollection stuff = ResourceBroker.getObjects(arg);
DynamicObject dyno = (DynamicObject) stuff.get(0);
String html = stuff.toHtml();
System.out.println("HTML OUTPUT:" + html);
Hashtable entity = Entity.getEntities();
Enumeration enum = entity.keys();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
Entity en = (Entity) entity.get(key);
String name = en.entityName;
System.out.println("Entity Types:" + name);
String htmlStr = "<select name=\"entityNames\" onchange=\"Process(this.options[this.selectedIndex].value);\">";
htmlStr +=" <option value=\"-1\" >Please Select One</option>";
htmlStr +=" <option value=\""+name +"\" >"+name+"</option>";
htmlStr+="</select>";
%>
<div align="center"><%=htmlStr%></div>
<%
}
%>
</p>
</form>


Could you please let me know about this, By the way let me know your personal id , so that i can send any mails immediately instead of sending from this forum, what you say ?

Regards
Gnanesh

Khalid Ali
07-15-2003, 12:51 PM
Had you just followed the code I posted above here its again


String htmlStr = "<select name=\"listBox\" onchange=\"Process(this.options[this.selectedIndex].value);\">";
htmlStr +=" <option value=\"-1\" >Please Select One</option>";

Hashtable entity = Entity.getEntities();
Enumeration enum = entity.keys();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
Entity en = (Entity) entity.get(key);
String entityName = en.entityName;
htmlStr +=" <option value=\""+entityName +"\" >"+entityName +"</option>";

}
//and once all that is done you can use the htmlStr variable to print it where ever you want

<%=htmlStr %>



Make sure these 2 line

String htmlStr = "<select name=\"listBox\" onchange=\"Process(this.options[this.selectedIndex].value);\">";
htmlStr +=" <option value=\"-1\" >Please Select One</option>";

are above all of the code above the while loop..if you put it in the whileloop you are creating a select box for each record...

gnanesh
07-15-2003, 01:09 PM
Khalid

Thanks , it was my mistake,

By the way how do i pass the selected value to a Java String like

function displayEntityTable(obj){
<%
String str = request.getParameter("displayEntity");
System.out.println("Parameter String:" + str);
%>
document.getElementById('displayTable').innerHTML = obj.value;
document.getElementById('displayTable').style.visibility = "visible";
}

Where displayEntity is the name of the Select tag , So when i select any option i should get the value and display at "displayTable" which is an Id of an DIV tag

Regards
GG

Khalid Ali
07-15-2003, 01:22 PM
Originally posted by gnanesh
Khalid

Thanks , it was my mistake,
Regards
GG

You are welcome...and no roblem...
I don't really get it what is you are asking in the second part...
are you saying that you want to create a table using jsp based on the value selected by a user from the list box.

gnanesh
07-15-2003, 01:27 PM
Let me clear you about that:-

As you know that i have created a SELECT box where in i will be calling a javascript function which is below:-

function displayEntityTable(obj){
<%
String str = request.getParameter("displayEntity");
System.out.println("Parameter String:" + str);
%>
document.getElementById('displayTable').innerHTML = obj.value;
document.getElementById('displayTable').style.visibility = "visible";
}


and I need to get the value which i selected from the Drop Down List(SELECT) and pass that value to an Java String object and I have a couple of Java Objects using those i need to query to display a TABLE(created by my DynamicObject class) in a particular place like DIV,

so if i tell

String str = request.getParameter("displayEntity");

i am getting null value, Please let me know how do i get the value into my Java Object

Thanks
GG

Khalid Ali
07-15-2003, 02:08 PM
Thereis no way you can pass data directly from JS to any server side app.
What you would need to do is submit valye from the page to the servlet/jsp page and then you can do what you are doing....