javascript to populate select
Moderators: tjay, seadap, Rogue4ngel, matthewhayashida
-
- Joomla! Apprentice
- Posts: 6
- Joined: Tue Nov 20, 2007 8:40 pm
javascript to populate select
Does anyone know how i can use javascript in a joomla form (view) to populate a select box depending on the selected value in another select box?
-
- Joomla! Apprentice
- Posts: 6
- Joined: Tue Nov 20, 2007 8:40 pm
Re: javascript to populate select
To elaborate below is some preliminary code in my view. $selected_province_id would be the id of the element selected in the first selection box.
Code: Select all
<tr>
<td width="100" align="right" class="key">
<label for="provinces">
<?php echo JText::_( 'Province' ); ?>:
</label>
</td>
<td>
<?php echo JHTML::_( 'select.genericlist', $this->provinces, 'provinces', null, 'id', 'title' ) ?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
<label for="universities">
<?php echo JText::_( 'University' ); ?>:
</label>
</td>
<td>
<?php echo JHTML::_('select.genericlist', $this->universities[$selected_province_id], 'universities', null, 'id', 'title')
</td>
</tr>
Re: javascript to populate select
without knowing exactly what you want to do, here are some relevant js entries, hopefully this will help you:
you can get javascript into the onchange attribute of the select by sending in - 'onchange="myJSMethod();"' as the 4th arg to HTML::_ (you have it as null)
what you want is :
hope this helps.
Code: Select all
<script language="JavaScript">
function myJSMethod(){
//selects
var prov_select = document.forms['myform'].provinces;
var univ_select = document.forms['myform'].universities;
//selected indicies
var selected_prov_index = prov_select.selectedIndex;
var selected_univ_index = univ_select.selectedIndex;
//set univ index on prov select box, effectively make the univ select follow the prov select
univ_select.selectedIndex = selected_prov_index;
return false;
}
</script>
what you want is :
Code: Select all
<form name="myform">
<select name="provinces"
OnChange="myJSMethod();">
<option selected>Please Select...
-
- Joomla! Apprentice
- Posts: 6
- Joined: Tue Nov 20, 2007 8:40 pm
Re: javascript to populate select
I solved this problem in the following way:
1) in my model I populated a multi-dimensional array called "universities" with the university information that I needed
2) in my view page I added the following which puts a javascript array in a script block at the top of the form:
3) in my form (in the view also) I added the following which uses the script block created previously and a js method called changeDynaList which is available in the joomla js library:
I used the joomla code for admininistrator/com_content which has dynamic select boxes for article and category as a model for how to do this.
L.
1) in my model I populated a multi-dimensional array called "universities" with the university information that I needed
2) in my view page I added the following which puts a javascript array in a script block at the top of the form:
Code: Select all
<script language="javascript" type="text/javascript">
<!--
var provinceuniversities = new Array;
<?php
$i = 0;
foreach ($this->universities as $province_id=>$university) {
foreach($university as $key=>$value) {
echo "provinceuniversities[".$i++."] =
new Array( '$province_id','".addslashes( $value->id)."','".addslashes( $value->title )."' );\n\t\t";
}
}
?>
//-->
</script>
Code: Select all
<tr>
<td width="100" align="right" class="key">
<label for="provinces">
<?php echo JText::_( 'Province' ); ?>:
</label>
</td>
<td>
<?php
$javascript = "onchange=\"changeDynaList( 'universities',provinceuniversities, document.adminForm.provinces.options[document.adminForm.provinces.selectedIndex].value, 0, 0);\"";
array_unshift($this->provinces, array("id"=>-1, "title"=>"Select province"));
echo JHTML::_( 'select.genericlist', $this->provinces, 'provinces', $javascript, 'id', 'title', '-1' )
?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
<label for="universities">
<?php echo JText::_( 'University' ); ?>:
</label>
</td>
<td>
<select name="universities">
</select>
</td>
</tr>
L.
Re: javascript to populate select
would please kindly tell me how i can save the selected item to database?
(I use MVC style and want to see the list in frontend)
Thank's in advance
(I use MVC style and want to see the list in frontend)
Thank's in advance
-
- Joomla! Apprentice
- Posts: 6
- Joined: Tue Nov 20, 2007 8:40 pm
Re: javascript to populate select
Sorry haven't got that far. Will try to keep you posted, but I'm no longer on that project.
Re: javascript to populate select
ok,anyway thank you so much.
Does anybody have the answer?
Thanks
Does anybody have the answer?
Thanks