| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
||||
|
||||
|
Dynamic Triple Linked Dropdowns
I posted this sample script some time ago but it seems to have disappeared. Since I have been getting requests to extend the Dynamic Linked Dropdowns example I am reposting this script.
The code below sets up a triple linked list box, so that a selection in box 1 will populate box 2, and a selection in box 2 will populate box 3. Haven't had time to comment too well so if you have any questions just post back. This is the JavaScript that controls the listbox population and clearing: Code:
<SCRIPT LANGUAGE="JavaScript">
<!--
function clearcombo(elem){
var i;
for (i = elem.options.length; i >= 0; i--) elem.options[i] = null;
elem.selectedIndex = -1;
}
function populatecombo2(elem, index){
if (array1.length >= index){
if (array1[index]){
for (var i = 0; i < array1[index].length; i= i + 2){
elem.options[elem.options.length] = new Option(array1[index][i + 1], array1[index][i]);
}
}
else{
elem.options[elem.options.length] = new Option("[none available]", 0);
}
}
else{
elem.options[elem.options.length] = new Option("[none available]", 0);
}
}
function populatecombo3(elem, index){
if (array2.length >= index){
if (array2[index]){
for (var i = 0; i < array2[index].length; i= i + 2){
elem.options[elem.options.length] = new Option(array2[index][i + 1], array2[index][i]);
}
}
else{
elem.options[elem.options.length] = new Option("[none available]", 0);
}
}
else{
elem.options[elem.options.length] = new Option("[none available]", 0);
}
}
function clickcombo(nWhich,elem1,elem2,elem3){
if (nWhich == 1){
clearcombo(elem2);
clearcombo(elem3);
populatecombo2(elem2, elem1[elem1.selectedIndex].value);
}
if (nWhich == 2){
clearcombo(elem3);
populatecombo3(elem3, elem2[elem2.selectedIndex].value);
}
return true;
}
// -->
</script>
Then the VBScript function that controls the display of the listboxes: Code:
<%
' this section of code is self-contained and suitable to be moved to an include file
Function TripleLinkedList(oCon, sQuery, sFormFieldName, nSize, sDBField1, sDBField2, sDBField3, sDBFieldResult)
' this is a general-purpose routine that implements triple-linked
' listboxes. here is the drill
Dim sTemp ' general-purpose temp variable
Dim sScript ' bucket for holding the script structure
Dim sSelect ' bucket for the <SELECT> statement
Dim sArray1 ' bucket to store the DBField2 array
Dim nField1 ' counter for the primary array
Dim sArray2 ' bucket to store the DBField3 array
Dim nField2 ' counter for the secondary array
Dim rs ' recordset
Dim sLastVal1 ' comparison string to test for record changes
Dim sLastVal2 ' comparison string to test for record changes
On Error Resume Next
If Not IsObject(oCon) Then
sScript = "error processing triplelist -- need a connection object."
ElseIf oCon.State <> 1 Then
If Err.number <> 0 Then
sScript = "error processing triplelist -- invalid connection object."
Else
sScript = "error processing triplelist -- connection is not open."
End If
Else
Set rs = oCon.Execute(sQuery)
If Err.number <> 0 Then
sScript = "error processing query. Error " & Hex(Err.number) & ": " & Err.Description
ElseIf rs.EOF Then
sScript = "no records found -- seems wrong"
Else
On Error Goto 0
sScript = "<SCR" & "IPT LANGUAGE=""JavaScript"">" & vbCrlf
sScript = sScript & "var array1 = new Array();" & vbCrlf
sScript = sScript & "var array2 = new Array();" & vbCrlf
sSelect = "<SELECT NAME=""" & sFormFieldName & "1"" SIZE=""" & nSize & _
""" ONCHANGE=""return(clickcombo(1,document.forms[0]." & _
sFormFieldName & "1,document.forms[0]." & sFormFieldName & _
"2,document.forms[0]." & sFormFieldName & "3));""><OPTION>Select a value</OPTION>" & vbCrlf
sLastVal1 = "empty" ' set up a default test value...
Do Until rs.EOF
If rs(sDBField1) <> sLastVal1 Then
If Right(sArray1, 3) = "," & vbCrlf Then
' pull off any trailing commas
sArray1 = Left(sArray1, Len(sArray1) - 3)
End If
If Len(sArray1) > 0 Then
sArray1 = sArray1 & ");" & vbCrlf
End If
' pick up new information for this row...
nField1 = nField1 + 1
sLastVal1 = rs(sDBField1)
' write the new contents of field 1 to the select statement
sSelect = sSelect & "<OPTION VALUE=" & nField1 & ">" & sLastVal1 & "</OPTION>"
' write a new entry in array1 for the field 2 values...
sArray1 = sArray1 & "array1[" & nField1 & "] = new Array(" & vbCrlf
' and reset the test values for field 2
nField2 = 0
sLastVal2 = "empty"
End If
If sLastVal2 <> rs(sDBField2) Then
If Right(sArray2, 3) = "," & vbCrlf Then
' pull off any trailing commas
sArray2 = Left(sArray2, Len(sArray2) - 3)
End If
If Len(sArray2) > 0 Then
sArray2 = sArray2 & ");" & vbCrlf
End If
' pick up new information for this row...
sLastVal2 = rs(sDBField2)
nField2 = nField2 + 1
' write a new entry in array1 containing this set of field 2 values...
sArray1 = sArray1 & " " & 1000 * nField1 + nField2 & ",""" & sLastVal2 & """," & vbCrlf
' write a new entry in array2 for this set of field 3 values...
sArray2 = sArray2 & "// values for " & sLastVal2 & vbCrlf
sArray2 = sArray2 & "array2[" & 1000 * nField1 + nField2 & "] = new Array(" & vbCrlf
End If
' write the field3 values to the field2 array...
sArray2 = sArray2 & " " & rs(sDBFieldResult) & ",""" & rs(sDBField3) & """," & vbCrlf
rs.MoveNext ' move on to the next record...
Loop
' if these arrays weren't previously closed out, then close them now
If Right(sArray2, 3) = "," & vbCrlf Then
' pull off any trailing commas
sArray2 = Left(sArray2, Len(sArray2) - 3)
End If
If Len(sArray2) > 0 Then
sArray2 = sArray2 & ");" & vbCrlf
End If
' if these arrays weren't previously closed out, then close them now
If Right(sArray1, 3) = "," & vbCrlf Then
' pull off any trailing commas
sArray1 = Left(sArray1, Len(sArray1) - 3)
End If
If Len(sArray1) > 0 Then
sArray1 = sArray1 & ");" & vbCrlf
End If
' close out the listbox/combobox and add the second and third listbox/combobox entries...
sSelect = sSelect & "</SELECT>" & vbCrlf
sSelect = sSelect & "<SELECT NAME=""" & sFormFieldName & "2"" SIZE=""" & nSize & _
""" ONCHANGE=""return(clickcombo(2,document.forms[0]." & sFormFieldName & _
"1,document.forms[0]." & sFormFieldName & "2,document.forms[0]." & _
sFormFieldName & "3));""><OPTION>Select a value</OPTION></SELECT>" & vbCrlf
sSelect = sSelect & "<SELECT NAME=""" & sFormFieldName & "3"" SIZE=""" & nSize & _
"""><OPTION>Select a value</OPTION></SELECT>" & vbCrlf
' finally clean up the script and write the whole thing out as a block
sScript = sSelect & vbCrlf & _
sScript & vbCrlf & _
sArray1 & vbCrlf & _
sArray2 & vbCrlf & _
"</SCR" & "IPT>" & vbCrlf
' -----------------------------------------------------------------
End If
' close and free the recordset
rs.Close
Set rs = Nothing
End If
' and get the fleep outta here
TripleLinkedList = sScript
End Function
%>
Then the normal db connections, form and call to the functions: Code:
<%
Dim Conn
Dim sQuery
Set Conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.mappath("yourdbname.mdb")
conn.Open ' connect to the database
' -----------------------------------------------
' then set up a query
' -----------------------------------------------
sQuery = "SELECT DISTINCT * from tablename Order By columnA, columnB " ' << insert your own sql here
%>
<FORM id=form1 name=form1>
<CENTER>
<%
Response.Write TripleLinkedList(Conn, sQuery, "listboxname", 1, "dbfieldname-for-1st-listbox", "dbfieldname-for-2nd-listbox", "dbfieldname-for-3rd-listbox", "name-of-the-result-from-sql")
Conn.Close
Set Conn = Nothing
%>
<P>
</FORM>
Hope you can follow through and fit to your requirements. Have fun
__________________
-
thought-after | my thoughts on web development Get Firefox, the developers browser Budget hosting - recommended [/left] |
|
#2
|
||||
|
||||
|
Comments, notes, FAQ's
Some clarification and use notes:
Q - What is the last variable (name of the result from sql) needed for in the function ? A - The last variable is the field name of the value from the recordset that you are trying to pass after all selections have been made. For example: Assume that you have a list of contacts, each contact is in a city, in a country. You want to provide 3 dropdowns so that users can select first the country, which will show a filtered list of cities in that country in the second dropdown. Selecting a city will then show a filtered list of contacts in that city in the third dropdown. It is the id of the contact that needs to be submitted through the form, so the last variable in the function would be the name of the contactid field in the database. |
|
#3
|
|||
|
|||
|
Hi,
What is this part trying to do? What is the function Left and Right doing? What is the function of sArray? What does it contain? Code:
If rs(sDBField1) <> sLastVal1 Then If Right(sArray1, 3) = "," & vbCrlf Then ' pull off any trailing commas sArray1 = Left(sArray1, Len(sArray1) - 3) End If If Len(sArray1) > 0 Then sArray1 = sArray1 & ");" & vbCrlf For this part, why is 1000 used? Code:
nField2 = nField2 + 1
' write a new entry in array1 containing this set of field 2 values...
sArray1 = sArray1 & " " & 1000 * nField1 + nField2 & ",""" & sLastVal2 & """," & vbCrlf
' write a new entry in array2 for this set of field 3 values...
sArray2 = sArray2 & "// values for " & sLastVal2 & vbCrlf
sArray2 = sArray2 & "array2[" & 1000 * nField1 + nField2 & "] = new Array(" & vbCrlf
End If
|
|
#4
|
|||
|
|||
|
Thank you for this code example.
I have c & p it exactly as it is on this page, in the order that it is. nofriends kindly helped me with the sql in this post When I run it, I get an error message as follows: ADODB.Recordset error '800a0cc1' Item cannot be found in the collection corresponding to the requested name or ordinal. /multi_drops/tripple_lists1.asp, line 94 which points to the line Code:
If rs(sDBField1) <> sLastVal1 Then The full code for this section is Code:
sLastVal1 = "empty" ' set up a default test value... Do Until rs.EOF If rs(sDBField1) <> sLastVal1 Then If Right(sArray1, 3) = "," & vbCrlf Then ' pull off any trailing commas sArray1 = Left(sArray1, Len(sArray1) - 3) End If The only code that is different is my sql which replaces this line Code:
sQuery = "SELECT DISTINCT * from tablename Order By columnA, columnB " ' << insert your own sql here with Code:
sQuery = "select * from tbl_continents order by continent"
'--> select values for country, drop down 2
'--> check if continent has a value, if it has, use the value in your sql
if Len(request.form("continent")) > 0 then
sQuery = "select * from tbl_countries order by country where continent_ID = " & request.form("continent")
else
sQuery = "select * from tbl_countries order by country"
end if
'--> select values for place, drop down 3
'--> check if country has a value, if it has, use the value in your sql
if Len(request.form("country")) > 0 then
sQuery = "select * from tbl_place order by place_name where country_ID = " & request.form("country")
else
sQuery = "select * from tbl_place order by PlaceName"
end if
Could someone kindly give me some ideas of how to fix this? thank you for your time ![]() Last edited by hauruapai : April 3rd, 2006 at 05:51 AM. Reason: needed to add code |
|
#5
|
|||
|
|||
|
How to get that
name-of-the-result-from-sql?using the select name 3? coz i had try and having a problem to get the id of row. ![]() |
|
#6
|
|||
|
|||
|
Dear everyone,
this is what i need, but when i add my id( the last variable to the function) my first option display the duplicate data, which all is what i need....only the duplication... |
|
#7
|
||||
|
||||
|
you better post new thread with more details about your specific problem.
|
|
#8
|
|||
|
|||
|
ask about tripple linked list
Dear sir
Your code is very amazing ,I want to ask I want to replace the part sArray1 = sArray1 & " " & 1000 * nField1 + nField2 & ",""" & sLastVal2 & """," & vbCrlf ' write a new entry in array2 for this set of field 3 values... sArray2 = sArray2 & "// values for " & sLastVal2 & vbCrlf sArray2 = sArray2 & "array2[" & 1000 * nField1 + nField2 & "] = new Array(" & vbCrlf End If I want to replace this part to retrive the actual code selected from the list and not the index of the selected item thank you best regards |
|
#9
|
||||
|
||||
|
The code works great.
One problem though.... If there is only one possible selection, let's say in the second dropdown list, then the third ddl will not show the options. Even if you click on the one option in the second ddl. What needs to be done is keep the -select a value- as the first option. Here is a quick fix... Code:
function populatecombo2(elem, index){
if (array1.length >= index){
if (array1[index]){
elem.options[elem.options.length] = new Option('select', 'select');
for (var i = 0; i < array1[index].length; i= i + 2){
elem.options[elem.options.length] = new Option(array1[index][i + 1], array1[index][i]);
}
}
//snip
Zath |
|
#10
|
|||
|
|||
|
display values of selected items in dynamic dropdown.
Hi guys i am totally a novice in this & have no programming background. However i managed to use your code along with the quick fix & it works fine.
There was another thing that i was trying to do i.e. display the values selected on submit ( Code pasted below ). However i get the id of the selected items displayed. I would like the values to be displayed instead of the id. Could u just help me out. [Code] <% Call Main() Sub Main() ' If the form is submitted, just display the selected Country, State, City If Request.Form("Submit") <> "" Then Response.write "<center><p> Country = " & Request.Form("listboxname1") & " </p></center>" Response.write "<center><p> State = " & Request.Form("listboxname2") & " </p></center>" Response.write "<center><p> City = " & Request.Form("listboxname3") & " </p></center>" %> <% Exit Sub End IF %> |
|
#11
|
||||
|
||||
|
this makes no sense - Request.Form("listboxname1") will give you the
value of the selected item in the drop down list with name "listboxname1". do you mean you want its text instead of the value? |
|
#12
|
|||
|