How to do condition drop-down's

rob

Administrator
Conditional drop downs are when you select one option in a first drop down list, and this selection alters the options available in a second list.

This how to is for Fabrik 1.0.4 +


lets say we have 2 drop downs who's ids are "cascadingexample___country" and "cascadingexample___town" -
"cascadingexample___country" has two options "france" and "england"
When these options are changed you need to populate "cascadingexample___town" with either "paris, toulouse, bordeaux" when france is selected or "london, liverpool, birmingham" when "england" is selected.

So we edit our country drop down list element, select the javascript tab and press "Add Javascript action to element " to a javascript event to it.

We want the event to run each time the country lists changes - so select "onchange" for the event and enter this in the code section:
(you can remove all the comments from the code if you like - there are here just to explain what's going on )
Code:
//lets build our data set, for each value in the first drop down (e.g. 'france' and 'england' we create an array of objects
//each object (the things encased in {} brackets, has a value and a label - these correspond to the option's value and label that we will be
//inserting into the second drop down

var data = {
'france':[{'value':'1','label':'paris'},{'value':'2','label':'toulouse'},{'value':'3','label':'bordueaux'}],
'england':[{'value':'1','label':'london'},{'value':'2','label':'birmingham'},{'value':'3','label':'liverpool'}]
};

// lets assign the town drop down to a variable 'towndd'
var towndd = $('cascadingexample___town');

//now lets empty it of its current options
towndd.empty();

//'this' refers to the drop down element you are adding the javascript to 
//- in this case its the country drop down list.
// we then get the current selected value from the drop down list
var country = this.getValue();


// now we can get the array of towns from our data set
var towns = data[country];


//for each town we create a new 'option' element, assigning it a value and label, and injecting it inside the town drop down list
towns.each(function(town){
 new Element('option', {value:town.value}).appendText(town.label).injectInside(towndd);
});

Now loading your form and selecting "france" should load in the french towns, and selecting "england" will load in the english towns.
hth

Rob
 
I have been trying to implement this for the better part of the last month. The code here works fine in firefox but does not appear to function at all in internet explorer. I tried several tweaks to this code to no avail, does this code work okay for others using ie 6? If not, what would be needed to make this work in ie6?

Thanks,

tim.
 
Could this be used to create a different type of element? For example a drop down list, depending on the answer selected could show an upload file element? I have a number of different answers, and each answer could then sub out to another drop down list, file upload or simply a text field.

Pete.
 
Yes, absolutely.

You'd simply create all the elements you need on your form, and set them all to hide, except the one you want displayed by default (if any). Then use the onchange event code to show/hide as appropriate. You'll need a form level onload event as well, to pre-set them when editing existing entries.

I believe Rob posted something to do exactly that very recently, I thought it was on this thread, but apparently not. I'll see if I can dig it up for you.

-- hugh
 
OK, this was the one I was thinking of:

http://fabrikar.com/index.php?option=com_smf&Itemid=9&topic=1860.0

It's actually about hiding and showing groups, but it would be trivial to modify it to use element ID's instead of group ID's.

Oh, and read the whole thread, 'cos we found a bug I haven't fixed yet (can't use // comments in the inline javascript!).

Also, I don't think there's anything there about the onload event to pre-set the group to show for existing data. Yell if you need help with that.

--- hugh
 
Hi there y'all

I have implemented a construction as described above (thanks Rob). It works fine in Firefox, but the second dropdown remains empty in IE6. It does remove the "Select a value" option, but the list is not populated with the query results.

Any ideas/recommendations would be greatly appreciated. All my users are on IE6 so it's kind of a "must have".

Thanks

Bas
 
Can you post your exact code here?

Have you stripped out all of the comments from the code? I still haven't fixed that little bug yet.

-- hugh
 
I will send that tomorrow, I dont have access to the server right now. In the user_ajax plugin I do have some comments left. Should I strip those as well?

Thx

Bas
 
Nope. It's only JavaScript code entered directly on an Element action in the Fabrik backend which is affected.

-- hugh
 
Hi Hugh

Thanks for looking into this. I love the Fabrik app, but i'd like to have this working on IE6 before I roll it out to my users.

Here's the code of the JS:
Code:
var cid = this.getValue();
var table = 'eam_naics02';
var key = 'code';
var label = 'description';
var foreignkey = 'parent';
var dropdown = 'eam_deals___naicslevel2';
var url = 'index2.php?option=com_fabrik&no_html=1&task=userPluginAjax&method=getFields';
url += '&table=' + table;
url += '&val='+cid;
url += '&key='+key;
url += '&label='+label;
url += '&foreignkey='+foreignkey;
new Ajax(url, {
method: 'get',
update: $(dropdown)
}

).request();

The user_ajax plugin:
Code:
<?php
/* MOS Intruder Alerts */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

class userAjax {
	
	function getFields(){
		global $database;
		//get all the variables passed in by the ajax objects url
		$table = mosGetParam($_REQUEST, 'table');
		$key = mosGetParam($_REQUEST, 'key', 'code');
		$label = mosGetParam($_REQUEST, 'label', 'description');
		$foreignKey = mosGetParam($_REQUEST, 'foreignkey', 'parent');
		$val = mosGetParam($_REQUEST, 'val', '1');
		
		//run a query on the database to get the matching fields
		$sql = "SELECT $key AS value, $label AS label FROM $table WHERE $foreignKey = '$val'";
		$database->setQuery($sql);
		$rows = $database->loadObjectList();
		
		//write the results of the query back to the browser - the javascript code will then assign
		//the text to the second drop down
		foreach($rows as $row){
			echo "<option value=\"$row->value\">$row->label</option>";
		}
	}
}

?>
 
I tried, Hugh. The 'post' method works fine on Mozilla but the same behaviour persists on IE6. Hope you have another recommendation for me.

Thanks

Bas
 
Unfortunately I have no way of testing against IE6, without completely re-installing one of my Windows boxes. Which I'm afraid I don't have time to do.

The only way I can take this any further is if you have a Windows XP Pro or W2k box with IE6 on it I can use Terminal Server to connect to and test from.

-- hugh
 
Thanks for the help & offer. Unfortunately our network doesn't allow that. Anyway, I removed the conditional dropdown from my table for now. Users will have to browse a bit more.... :D

Thanks for the help, I'll check in every now and then to see if an answer has miraculously appeared.
 
Thanks for the pointer, but I'm allergic to installing non-signed apps containing DLL's from unknown sources. And several people have already reported finding malware/viruses when downloading that package.

Bottom line, I'm focused on IE7/8 and FF2/3 now. IE6 is a legacy we just don't have time to support beyond "does it work with the basic Fabrik install".

Just testing Fabrik v1 and v2 against IE7/8 and FF2/3 is a handful (that's 4 browser versions on 2 Fabrik versions ... 8 permutations ...).

Not trying to be a hardass, just setting realistic expectations. If Fabrik was a $1000 commercial app, sure ... we'd test it and certify it for use on every browser ever made. But for OpenSource ... you get what you get. :)

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top