Dynamic related combo boxes in table cells | Newbie | | Join Date: Feb 2007
Posts: 4
| | |
I am developing a web page for order processing using Spring MVC.
In my JSP I have a table populated from the list of orders that are passed in the model (form backing object). Some fields there are filled and some are not.
User is suppose to complete mandatory fields and submit the form further.
I have it almost done except one thing where I can use some help.
One <td> contains 2 related combo boxes for shipping service selection:
First combo box contains options for carrier selection:USPS, FedEx, UPS, etc.
Second one should be populated with the type of service offered by the respective carrier: USPS - Express Mail, Priority Mail, Parcel Post; FedEx - Priority Overnight, Overnight, ..., Home Delivery, etc.
I found on the web a solution with the Javascript function that is called onChange event on the first combo box and generates options for the second.
My problem is that I have these two combo boxes in a table cell for several dozens of rows and I am not sure how can I address them properly.
I am specializing on the back end Java development but right now I need to deliver a complete web application. My front end skills are quite limited.
Help me please.
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma
Posts: 1,076
| | | re: Dynamic related combo boxes in table cells
To address the combo boxes you need to give them an ID
Example
[HTML]
<select id="shipping" onchange="setOptions(this.value);"><option value="blank">Select Shipping Type</option><option value="0">UPS<option value="1">FedEx</select>
[/HTML]
This will give this combo box an ID -- The second select box could be something like
[HTML]
<select id="service"></select>
[/HTML]
your javascript would then call upon these like this -
<script>
-
function setOptions(val) {
-
var opt = document.getElementById("service")
-
if (val == 0) {
-
//this is if they selected ups
-
clearlist();
-
opt.options[0] = new Option('Express Mail','Express');
-
opt.options[1] = new Option('Priority Mail','Priority');
-
opt.options[2] = new Option('Parcel Post','Parcel');
-
opt.options[3] = new Option('Priority Overnight','Overnight');
-
//So on
-
alert('The brown guys');
-
}
-
if (val == 1) {
-
clearlist();
-
//this is if they selected fedex
-
opt.options[0] = new Option('Fedex1','Fedex1');
-
opt.options[1] = new Option('Fedex2','Fedex2');
-
alert('Fedex');
-
}
-
}
-
-
function clearlist() {
-
var opt = document.getElementById("service").options;
-
opt.length = 0;
-
}
-
-
</script>
-
please post with anything you don't understand about this so i can give you a further explanation.
don't forget in a form you need to name your select boxes so your serverside code can request it.
And welcome to thescripts!
| | Newbie | | Join Date: Feb 2007
Posts: 4
| | | re: Dynamic related combo boxes in table cells
Thank you,
I understand everything but unfortunately my case is little bit more complicated.
I wrote JSP that generates the javascript based on what you suggested but it involves a hash array of lists of objects, but cannot make it work. Here is the generated page source: - <script language="JavaScript" type="text/javascript">
-
var optmap = new Array();
-
-
optmap["1"] = [
-
-
new serviceOption("1", "FedEx First Overnight"),
-
-
new serviceOption("2", "FedEx Priority Overnight"),
-
-
new serviceOption("3", "FedEx Standard Overnight"),
-
-
new serviceOption("4", "FedEx 2 Days"),
-
-
new serviceOption("5", "FedEx Express Saver"),
-
-
new serviceOption("6", "FedEx Home Delivery")
-
];
-
-
optmap["2"] = [
-
-
new serviceOption("1", "UPS Next Day Air Early AM"),
-
-
new serviceOption("2", "UPS Next Day Air"),
-
-
new serviceOption("3", "UPS Next Day Air Saver"),
-
-
new serviceOption("4", "UPS Second Day Air AM"),
-
-
new serviceOption("5", "UPS Second Day Air"),
-
-
new serviceOption("6", "UPS 3 Day Select")
-
];
-
-
optmap["3"] = [
-
-
new serviceOption("2", "USPS Express Mail"),
-
-
new serviceOption("4", "USPS Priority Mail"),
-
-
new serviceOption("6", "USPS Parcel Post")
-
];
-
-
optmap["4"] = [
-
-
new serviceOption("1", "DHL Next Day AM"),
-
-
new serviceOption("2", "DHL Next Day Noon"),
-
-
new serviceOption("3", "DHL Next Day 3 PM"),
-
-
new serviceOption("4", "DHL Second Day"),
-
-
new serviceOption("5", "DHL Ground"),
-
-
new serviceOption("6", "DHL @ Home")
-
];
-
-
function serviceOption(priority, name)
-
{
-
this.priority = priority;
-
this.name = name;
-
}
-
-
function refill(p, x)
-
{
-
document.write("came in with p = " + p + " and x = " + x);
-
var temp = document.getElementById(p);
-
clearopts(p);
-
var opts = optmap[x];
-
document.write(opts);
-
for(var i = 0; i < opts.length; i++)
-
{
-
var opt = opts[i];
-
temp.option[i] = new Option(opt.priority, opt.name);
-
}
-
}
-
-
function clearopts(p)
-
{
-
var o = document.getElementById(p).options;
-
o.length = 0;
-
}
-
-
</script>
and here is the call: - <td>
-
-
<select name=""
-
onchange="refill('service_10088',this.value)">
-
-
<option value="1">
-
FedEx
-
</option>
-
-
<option value="2">
-
UPS
-
</option>
-
-
<option value="3">
-
USPS
-
</option>
-
-
-
<option value="4">
-
DHL
-
</option>
-
-
</select><select id="service_10088"></select>
-
<!-- this should be populated dynamicaly -->
-
</td>
-
Function refill get called with correct parameter values but variable opts inside the function is undefined and has no properties. The call to clearopts function occurs but errors out at this line: - var o = document.getElementById(p).options;
Error console says that - Error: document.getElementById(p) has no properties
-
Source File: http://localhost:8084/operations/operations.do
-
Line: 3173
Can you please point out what is wrong with the code and how can I fix it?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Dynamic related combo boxes in table cells
Your serviceOption function doesn't actually create the options. What's priority? Also, what is the this object that you are setting the name and priority for?
See this page for creating/manipulating options.
| | Newbie | | Join Date: Feb 2007
Posts: 4
| | | re: Dynamic related combo boxes in table cells Quote:
Originally Posted by acoder Your serviceOption function doesn't actually create the options. What's priority? Also, what is the this object that you are setting the name and priority for?
See this page for creating/manipulating options. Why it does not create the object? I followed the book for using constructors.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Dynamic related combo boxes in table cells Quote:
Originally Posted by GaryGreenberg Why it does not create the object? I followed the book for using constructors. OK, I had a closer look at your code and it does create options but in the refill function. serviceOptions is, of course, an object.
Ok, I've pinpointed the error. Use the following code for your refill function: - function refill(p, x)
-
{
-
alert("came in with p = " + p + " and x = " + x);
-
var temp = document.getElementById(p);
-
clearopts(p);
-
var opts = optmap[x];
-
alert(opts);
-
for(var i = 0; i < opts.length; i++)
-
{
-
var opt = opts[i];
-
temp.options[i] = new Option(opt.priority, opt.name);
-
}
-
}
-
I've changed the document.write()s to alerts. The main problem with your code was temp.option should have been temp. options | | Newbie | | Join Date: Feb 2007
Posts: 4
| | | re: Dynamic related combo boxes in table cells
I got it working, thank you for your help. Main problem was not in the script but in the configuration file for the JSP. JSP has been rendered twice. first time normally and second time without params, therefore, obliterating the array created in the first run.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|