javascript to populate select

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
laurelwilliams
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 20, 2007 8:40 pm

javascript to populate select

Post by laurelwilliams » Wed Feb 13, 2008 9:16 pm

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?

laurelwilliams
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 20, 2007 8:40 pm

Re: javascript to populate select

Post by laurelwilliams » Tue Feb 19, 2008 8:54 pm

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>


permalink
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Tue Feb 19, 2008 5:36 pm

Re: javascript to populate select

Post by permalink » Tue Feb 26, 2008 6:56 am

without knowing exactly what you want to do, here are some relevant js entries, hopefully this will help you:

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>
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 :

Code: Select all

<form name="myform">
<select name="provinces"
 OnChange="myJSMethod();">
     <option selected>Please Select...
hope this helps.

laurelwilliams
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 20, 2007 8:40 pm

Re: javascript to populate select

Post by laurelwilliams » Wed Feb 27, 2008 3:11 pm

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:

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>
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:

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>

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.

User avatar
missstar
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Mar 03, 2008 7:52 am

Re: javascript to populate select

Post by missstar » Mon Mar 03, 2008 10:15 am

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

laurelwilliams
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 20, 2007 8:40 pm

Re: javascript to populate select

Post by laurelwilliams » Mon Mar 03, 2008 11:48 am

Sorry haven't got that far. Will try to keep you posted, but I'm no longer on that project.

User avatar
missstar
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Mar 03, 2008 7:52 am

Re: javascript to populate select

Post by missstar » Mon Mar 03, 2008 8:56 pm

ok,anyway thank you so much.
Does anybody have the answer?
Thanks


Post Reply