Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Dropdown in dynamic table.

Status
Not open for further replies.

manishblr1

Programmer
Dec 20, 2010
20
0
0
US
I have a screen where users select the number of periods from a dropdown list.

Based on the number selected in the dropdown, a dynamic table is created with rows equal to the number selected from the dropdown.

On selecting a number from the dropdown a function is called which creates a dynamic table (columns fixed to 2)

In this table I am populating the first column and disabling the second one. Based on change in the first dropdown, the second dropdown should be enabled and populated.

But I am facing the following issues:
1. Every time only one row is shown no matter what number you select from the number of periods dropdown
2. On change in first dropdown in dynamic table, a function is called.
In this function I am trying to access the second dropdown id and enable it, but it’s not happening.

Here is the code
Code:
<HTML>
<Title> TimePeriod </Title>
<HEAD>
   	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<meta http-equiv="Expires" content="0"/>
	<meta http-equiv="Cache-Control" content="no-cache"/>
	<meta http-equiv="Pragma" content="no-cache"/>
</HEAD>
<script language="Javascript">

var measureClassArray = new Array();
measureClassArray[0] = "Select Measure Class";
measureClassArray[1] = "MC1";
measureClassArray[2] = "MC2";
measureClassArray[3] = "MC3";
measureClassArray[4] = "MC4";
measureClassArray[5] = "MC5";

var measureArray = new Array();
measureArray[0] = "Select Measure";
measureArray[1] = "m1";
measureArray[2] = "m2";
measureArray[3] = "m3";
measureArray[4] = "m4";
measureArray[5] = "m5";

function populateMeasure(_measureClassID){
	var rowId= _measureClassID.substring(12);
	alert(document.getElementById(_measureClassID).value);
	alert(document.getElementById('measure'+rowId));
	if(document.getElementById(_measureClassID).value != "Select Measure Class")
	{
		document.getElementById('measure'+rowId).disabled=false;
	}
	else
	{
		document.getElementById('measure'+rowId).disabled=true;
	}
	
	measureID= document.getElementById('measure'+rowId);
	if(!measureID.length)
	{
		for (i=0;i<measureArray.length-1 ;i++)
		{
			var opt = document.createElement("option");
			measureID.options.add(opt);
			opt.text = measureArray[i];
			opt.value = i;
		}
	}
}
function createTable(){
	var testArray= new Array();
	testArray[0]=0;
	var tbl = document.getElementById('shiftTable');
	deleteTableLength= tbl.rows.length;
	while(deleteTableLength>0){
		document.getElementById('shiftTable').deleteRow(deleteTableLength-1);
		deleteTableLength --;
	}
	rowCount=document.getElementById('genericFunction').value;
		var mytablebody = document.createElement("tbody");
		columnHeading = document.createElement("tr");
		metricHeading = document.createElement("TD");
		metricHeading.innerHTML="Metric";
		metricHeading.setAttribute("bgColor","#BAD2E8");
		metricHeading.setAttribute("align","center");
		columnHeading.appendChild(metricHeading);
		measureClassHeading = document.createElement("TD");
		measureClassHeading.innerHTML="Measure Class";
		measureClassHeading.setAttribute("bgColor","#BAD2E8");
		measureClassHeading.setAttribute("align","center");
		columnHeading.appendChild(measureClassHeading);
		mytablebody.appendChild(columnHeading);
	
		for(i = 0; i < rowCount; i++){
		// creates a <tr> element
			eachRow = document.createElement("tr");			
			cell0 = document.createElement("TD");
			e0 = document.createElement("select");
			e0.id='measureClass'+i;
			for (i=0;i<measureClassArray.length;i++){
				var opt = document.createElement("option");
				e0.options.add(opt);
				opt.text = measureClassArray[i];
				opt.value = measureClassArray[i];
			}
			e0.onchange= function(){populateMeasure(e0.id);};
//			e4.setAttribute('maxLength', 12);
			cell0.appendChild(e0);
			eachRow.appendChild(cell0);
		
			cell1 = document.createElement("TD");
			e1 = document.createElement("select");
			e1.id='measure'+i;
			e1.disabled=true;
			cell1.appendChild(e1);
			eachRow.appendChild(cell1);
			
			mytablebody.appendChild(eachRow);
		}
    // appends <tbody> into <table>
	tbl.appendChild(mytablebody);
	// mybody.appendChild(mytable);
	tbl.setAttribute("border", "2");

}
</script>

<!-- To avoid scrollbar at the right side:  style="overflow: hidden" -->
<BODY style="overflow: hidden">
<!-- Row Definition -->
<form name="myGenericFunctionAssocForm">
<TABLE cellpadding="0" cellspacing="0" width="800px" height="100%">
	<TR id="Section_2" style="display: inline">
		<TD class="ContentHeaderSection" valign="top" align="left">
			<TABLE cellspacing="0" cellpadding="0">
				<TR>
					<TD>
						<TABLE  class="Content" WIDTH="780px" cellspacing="0" cellpadding="5" bordercolor="#FFFFFF" border="1">
							<TR  id="no_time_period_label">
								<TD WIDTH="170px"><label>No Of Periods</label><font color="red">*</font></TD>
								<TD WIDTH="170px">
									<SELECT id="genericFunction" onchange="createTable();">
										<OPTION value="Select Generic Function">Select Generic Function</OPTION>
										<OPTION value="1">1</OPTION>
										<OPTION value="2">2</OPTION>
										<OPTION value="3">3</OPTION>
										<OPTION value="4">4</OPTION>										
									</SELECT>
								</TD>
							</TR>
							<TR id="shift_timeperiod">
								<TD colspan="4">
									<TABLE id="shiftTable">
									</TABLE>
								</TD>
							</TR>
						</TABLE>
					</TD>
				</TR>
			</TABLE>
		</TD>
	</TR>
	<TR height="100%">
		<TD valign="top" align="left">
<!--iframe src="TimePeriod.irpt" width="100%" height="100%" name="ifr_Content" scrolling="auto"  frameborder="0" >
  <p>Your browser cann't dislay iframes</p>
</iframe-->
		</TD>
	</TR>
</TABLE>
 </form>
</BODY>
</HTML>

Thanks in advance.
 
Its a little hard to follow your ode, and you explanation, but as a start you are using the variable i inside the same loop for 2 different thins. How do you think that affects i?

Code:
[red]for(i = 0; i < rowCount; i++){[/red]
        // creates a <tr> element
            eachRow = document.createElement("tr");            
            cell0 = document.createElement("TD");
            e0 = document.createElement("select");
            e0.id='measureClass'+i;
            [red]for (i=0;i<measureClassArray.length;i++)[/red]{
                var opt = document.createElement("option");
                e0.options.add(opt);
                opt.text = measureClassArray[i];
                opt.value = measureClassArray[i];
            }
            e0.onchange= function(){populateMeasure(e0.id);};
//            e4.setAttribute('maxLength', 12);
            cell0.appendChild(e0);
            eachRow.appendChild(cell0);
        
            cell1 = document.createElement("TD");
            e1 = document.createElement("select");
            e1.id='measure'+i;
            e1.disabled=true;
            cell1.appendChild(e1);
            eachRow.appendChild(cell1);
            
            mytablebody.appendChild(eachRow);
        }


So you start your loop with [blue]i[/blue] being set to 0.
On the first iteration you get to your internal loop, and roceed to receti to 0 and star a new oop.

B the time this second loop is done and you're back to your outer loop i is now a value which most probably is higher than rowCount so your loop ends in its first iteration.


Change your second loop's variable to something else, maybe j or x or whatever.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi Vacunita ,
Thanks for the reply. It solved 90% of my problem except one . When ever i select a value from second column drop down(measure class) only last cell of measure(third column drop down) is being populated. For example if i select 3 from initial drop down and select value from second column drop down (any cell of second column ) only last cell of third column drop down is populated.
Code:
<!--
/**
---------------------------------------------------------

 * @author      Manish Jain
 *
 * <p>
 *
 * <u>Purpose</u>:<br>
 * This Screen allows the user to create Time Periods for the selected Production calendar.
  * <p>
 *
 * <u>Description</u>:<br>
 *  A user can create the following Time Periods for the selected Production calendar.
 * Shift ,Day, Week, Month and Year
 *
 * <p>
 *
* <u>Copyright</u>:<br>
 * The information contained herein is confidential,<br>
 * and is the property of Anglo KIO Sishen.<br>
 *
* <u>Version 1.0</u>:<br>
*
---------------------------------------------------------
*/
-->

<HTML>
<Title> TimePeriod </Title>
<HEAD>
   	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<meta http-equiv="Expires" content="0"/>
	<meta http-equiv="Cache-Control" content="no-cache"/>
	<meta http-equiv="Pragma" content="no-cache"/>
</HEAD>
<script language="Javascript">

var measureClassArray = new Array();
measureClassArray[0] = "Select Measure Class";
measureClassArray[1] = "MC1";
measureClassArray[2] = "MC2";
measureClassArray[3] = "MC3";
measureClassArray[4] = "MC4";
measureClassArray[5] = "MC5";

var measureArray = new Array();
measureArray[0] = "Select Measure";
measureArray[1] = "m1";
measureArray[2] = "m2";
measureArray[3] = "m3";
measureArray[4] = "m4";
measureArray[5] = "m5";


function createTable(){
	var testArray= new Array();
	testArray[0]=0;
	var tbl = document.getElementById('shiftTable');
	deleteTableLength= tbl.rows.length;
	while(deleteTableLength>0){
		document.getElementById('shiftTable').deleteRow(deleteTableLength-1);
		deleteTableLength --;
	}
	rowCount=document.getElementById('genericFunction').value;
		var mytablebody = document.createElement("tbody");
		columnHeading = document.createElement("tr");
		metricHeading = document.createElement("TD");
		metricHeading.innerHTML="Metric";
		metricHeading.setAttribute("bgColor","#BAD2E8");
		metricHeading.setAttribute("align","center");
		columnHeading.appendChild(metricHeading);
		measureClassHeading = document.createElement("TD");
		measureClassHeading.innerHTML="Measure Class";
		measureClassHeading.setAttribute("bgColor","#BAD2E8");
		measureClassHeading.setAttribute("align","center");
		columnHeading.appendChild(measureClassHeading);
		measureHeading = document.createElement("TD");
		measureHeading.innerHTML ="Measure";
		measureHeading.setAttribute("bgColor","#BAD2E8");
		measureHeading.setAttribute("align","center");
		columnHeading.appendChild(measureHeading);
		mytablebody.appendChild(columnHeading);
	
		for(i = 0; i < rowCount; i++){
		// creates a <tr> element
			eachRow = document.createElement("tr");			
			cell1 = document.createElement("TD");
			e1 = document.createElement("input");
			e1.setAttribute('type',"text");
			e1.name='offSetShiftST[]';
			e1.id='offSetShiftST'+i;
			e1.size = '12';
			e1.setAttribute('maxLength', 12);
			e1.value=i;
			e1.disabled=true;
			cell1.appendChild(e1);
			eachRow.appendChild(cell1);
			
			
			cell0 = document.createElement("TD");
			e0 = document.createElement("select");
			e0.name='measureClass[]';
			e0.id='measureClass'+i;
			for (j=0;j<measureClassArray.length;j++){
				var opt = document.createElement("option");
				e0.options.add(opt);
				opt.text = measureClassArray[j];
				opt.value = measureClassArray[j];
			}
			cell0.appendChild(e0);
			e0.onchange= function(){populateMeasure(e0.id);};
			eachRow.appendChild(cell0);

			cell2 = document.createElement("TD");
			e2 = document.createElement("select");
			e2.name='measure[]';
			e2.id='measure'+i;
			e2.disabled=true;
			cell2.appendChild(e2);
			eachRow.appendChild(cell2);

			mytablebody.appendChild(eachRow);
		}
    // appends <tbody> into <table>
	tbl.appendChild(mytablebody);
	// mybody.appendChild(mytable);
	tbl.setAttribute("border", "2");

}

function populateMeasure(_measureClassID){
	var rowId= _measureClassID.substring(12);
	if(document.getElementById(_measureClassID).value != "Select Measure Class")
	{
		document.getElementById('measure'+rowId).disabled=false;
	}
	else
	{
		document.getElementById('measure'+rowId).disabled=true;
	}
	measureID= document.getElementById('measure'+rowId);
	if(!measureID.length)
	{
		for (k=0;k<measureArray.length-1 ;k++)
		{
			var opt = document.createElement("option");
			measureID.options.add(opt);
			opt.text = measureArray[k];
			opt.value = measureArray[k];
		}
	}
}
</script>

<!-- To avoid scrollbar at the right side:  style="overflow: hidden" -->
<BODY style="overflow: hidden">
<!-- Row Definition -->
<form name="myGenericFunctionAssocForm">
<TABLE cellpadding="0" cellspacing="0" width="800px" height="100%">
	<TR id="Section_2" style="display: inline">
		<TD class="ContentHeaderSection" valign="top" align="left">
			<TABLE cellspacing="0" cellpadding="0">
				<TR>
					<TD>
						<TABLE  class="Content" WIDTH="780px" cellspacing="0" cellpadding="5" bordercolor="#FFFFFF" border="1">
							<TR  id="no_time_period_label">
								<TD WIDTH="170px"><label>No Of Periods</label><font color="red">*</font></TD>
								<TD WIDTH="170px">
									<SELECT id="genericFunction" onchange="createTable();">
										<OPTION value="Select Generic Function">Select Generic Function</OPTION>
										<OPTION value="1">1</OPTION>
										<OPTION value="2">2</OPTION>
										<OPTION value="3">3</OPTION>
										<OPTION value="4">4</OPTION>										
									</SELECT>
								</TD>
							</TR>
							<TR id="shift_timeperiod">
								<TD colspan="4">
									<TABLE id="shiftTable">
									</TABLE>
								</TD>
							</TR>
						</TABLE>
					</TD>
				</TR>
			</TABLE>
		</TD>
	</TR>
	<TR height="100%">
		<TD valign="top" align="left">
<!--iframe src="TimePeriod.irpt" width="100%" height="100%" name="ifr_Content" scrolling="auto"  frameborder="0" >
  <p>Your browser cann't dislay iframes</p>
</iframe-->
		</TD>
	</TR>
</TABLE>
 </form>
</BODY>
</HTML>
 
Seems like the code is passing the last measureID ID to all the populateMeasure function calls.

Try this instead:

Code:
 e0.onchange= function(){populateMeasure([red]this[/red].id);};





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi Vacunita,

Thanks for that.It solved my problem.You simply rock.
God bless you.
Take care

Regards,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top