Depending on the degree of logic needed, it can be done in either or both (I would tend to say both, unless you only have a few possible combinations, and don’t mind overloading the page with extra elements that won’t appear).
One thing to realize here — if your pickers are replaced on screen, then any handler that is bound to them will fail after the update. Here’s an example:
<select name=“foo” id=“foo” size=“1”>
<option value=“a”>A</option>
</select>
$('foo').observe('change', function(){
// do something clever here, maybe call the next picker after that
});
The first time the page loads, the observer will work. But if you update that select using an Ajax callback that replaces the whole element, then unless you re-initialize your observer, it will never work again. That’s because the initial call that created the observer function bound it to that actual picker element, not just any picker with that name.
There’s a way around this, actually two ways I can think of off the top of my head. First is to use a lazy (late-evaluating) observer, like this:
document.on('change', '#foo', function(evt, elm){
// do your cleverness here
});
Now, no matter what happens to the picker, if it fires a change event and the document catches it, the method will do its thing, because the observer is bound to the document, which doesn’t change.
The other way around this is to instantiate the picker, and bind an observer to it, but then never remove that select element. To replace all the option elements of a select without getting rid of the outer select (and thereby breaking the bond with its observer), you need to follow this pattern (ignore anyone who tells you you can just update the contents of the picker with a new set of options using jQuery or Prototype — that will only work in a frustratingly tiny minority of the browsers out there).
$('foo').options.length = 0; // erase existing options
$H({a: 'A', b: 'B', c: 'C'}).each(function(pair){
$('foo').options[$('foo').options.length] = new Option( pair.value, pair.key );
});
In this example, I’m using a hard-coded hash of variables, but replace that with an Ajax request to your server to get the proper set of values based on the initial value, and you get a very flexible setup that can be administered through the database (you don’t need to edit the HTML or JavaScript to change the possible options). As long as your server can return JSON, then you are all set.
Walter
On Nov 8, 2014, at 12:14 PM, Todd email@hidden wrote:
For this tutorial/community site I’m working on I need a (front-end) form to display (or not display) form controls based on user selections. I believe this is referred to as “chaining”? Correct me if I’m wrong.
Is this sort of logic usually done server-side or with js? I’m not sure if I need to look at the PHP framework I’m using to build the site or somewhere else.
Todd
dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options
dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options