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!

For RnK dependent droplists

Status
Not open for further replies.

unreal1969

Programmer
Dec 7, 2003
9
0
0
CA
Hi there i saw your post on dependent droplists nice
how would i do if i wanted to populate both droplists with data from DB. I have my data in an array but in asp how would i transfer the asp array to a javascript array or would i have to do this.

thanx
aj
 
sorry forgot to post code
basically how do i populate droplists with asp array

<BODY onLoad = &quot;init_page();&quot;>
<FORM NAME=&quot;F1&quot;>

<SELECT id=&quot;DD1&quot;
name=&quot;DD1&quot;
onChange = &quot;fill_list(document.DD2_Array, document.F1.DD1[document.F1.DD1.selectedIndex].value, document.F1.DD2);&quot;>
</SELECT>
<SELECT id=&quot;DD2&quot;
name=&quot;DD2&quot;>
</Select>
</form>
</Body>

<SCRIPT language = &quot;JavaScript&quot;>

function init_page()
{
fill_list(document.DD1_Array, 0, document.F1.DD1, true);
fill_list(document.DD2_Array, document.F1.DD1[document.F1.DD1.selectedIndex].value, document.F1.DD2, false);
}

function fill_list(item_array, parent_item_id, output_select_list, init_flag)
{
output_select_list.length = 0;
for (x = 0; x < item_array.length; x++ )
{
if ( item_array[x] == parent_item_id || init_flag == true )
{
var option1 = new Option(item_array[x+1], item_array[x+2]);
output_select_list.options[output_select_list.length] = option1;
}
x+=2;
}
output_select_list[0].selected = true;
}

//The Arrays that hold the values to display
document.DD1_Array = new Array ('1', 'Value 1', '1',
'2', 'Value 2', '2');

document.DD2_Array = new Array('1', 'Sample 1', '1',
'1', 'Sample 2', '2',
'1', 'Sample 3', '3',
'1', 'Sample 4', '4',
'2', 'Sample 5', '5',
'2', 'Sample 6', '6',
'2', 'Sample 7', '7',
'2', 'Sample 8', '8',
'2', 'Sample 9', '9');

</SCRIPT>
 
No need to do any client DOM work @ all. Just write in the options.

<%@LANGUAGE=JSCRIPT%>
...
<%
...
var recordCount = someArray.length;
with(Response){
//===========
// SELECT TAG
//===========
write(&quot;<SELECT>&quot;);
//===================
// Write the options
//===================
for(var j=0;j<recordCount;j++){
var thisRecord = recordCount[j];
write(&quot;<OPTION name='&quot; + thisRecord + &quot;' VALUE='&quot;+thisRecord+&quot;'>)
write(thisRecord);
write(&quot;</OPTION>&quot;)
}
write(&quot;</SELECT>&quot;);
}
...
%>
 
Actually I was the one who posted the particular code you are referencing and I do at times populate the the arrays directly from a database.

Perhaps you can use GIGN's example of how each element of the array can be populated in JavaScript straight from the Database and recordset.

The array that I am using is geared for numerous Dropdowns and that is why you see it hold three elements. LeftKey, DropDownValue and RightKey. Once loaded the dropdowns are dynamically changed depending on previous selections and with out having to refresh or reload the page.

LeftKey represent the index of (Parent) item selected
DropDownValue represents the value for the target DropDown
RightKey represents the index for item of the target.

The way I do (May there are easier ways) is to create a UserDefined Type and then create an array based on that type.

Here is some sample code to play with, might give you an idea

//Create a User Defined Type called objArray
function objArray()
{
this.Name = &quot;f&quot;;
this.LKey = &quot;f&quot;;
this.Value = &quot;f&quot;;
this.RKey = &quot;f&quot;;
}

//Create an Array based on the objArray and fill it
function Fill_objArray()
{
//var thisRecord = recordCount[j];

this.DDArray = new Array();

//for(var i = 0; i < recordCount; i++)
for(var i = 0; i < 10; i++)
{
//thisRecord = recordCount;
DDArray = new objArray();
DDArray.Name = &quot;DD3_Array&quot;;
DDArray.LKey = 1; //write thisrecord(&quot;fieldName&quot;)
DDArray.Value = &quot;Sample&quot;; // etc..
DDArray.RKey = i; // etc..
}
}

//Test how it works
function Test_objArray()
{
for(var i = 0; i < 10; i++) {
alert(DDArray.Name + &quot; &quot; + DDArray.LKey + &quot; &quot; + DDArray.Value + &quot; &quot; + DDArray.RKey);
}
}




 
unreal1969,
Did kevinclark and GIGN answer your question? Or are you looking to populate your dropdown list based on selection in a previous dropdown?

RnK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top