| View previous topic :: View next topic |
| Author |
Message |
roger06
Joined: 11 Feb 2008 Posts: 6
|
Posted: Wed Apr 02, 2008 7:30 pm Post subject: QuickForm2 setting 'selected' item in select list ? |
|
|
Hi
I'm creating <select> lists fine in QuickForm2 but can't work out how to set one of the items as selected.
Am creating thus:
| Code: | $specialism = $fsText->addElement(
'select', 'specialism', null , array('options' => $specialism_options, 'label' => 'Specialism' )
);
|
populating the data with an array:
| Code: | $specialism_options = array(
'acc' => 'Accountancy', 'off' => 'Office services', 'exec' => 'Executive search'
);
|
I'm assuming the selected values is something to do with the $attributes array which I've tried creating in place on the NULL
| Code: | | $specialism_att = array('selected' => 'exec'); |
but this has no affect...
Any ideas? As I can't believe doing something so essential should be so difficult!
Thanks |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Wed Apr 02, 2008 9:37 pm Post subject: |
|
|
In HTML_QuickForm a method setSelected() exists, but it is recommended to use $form->setDefaultValues().
According to the phpdoc comment in the HTML_QuickForm2_Element_Select_OptionContainer class, you can use the addOption() method to mark an item as selected:
| Code: |
/**
* Adds a new option
*
* Please note that if you pass 'selected' attribute in the $attributes
* parameter then this option's value will be added to <select>'s values.
*
* @param string Option text
* @param string 'value' attribute for <option> tag
* @param mixed Additional attributes for <option> tag (either as a
* string or as an associative array)
*/
public function addOption($text, $value, $attributes = null)
|
I haven't checked it, but according to what happens in this method, setDefaultValues() should still work in QF2. (This method might have another name in QF2, though.) |
|
| Back to top |
|
 |
roger06
Joined: 11 Feb 2008 Posts: 6
|
Posted: Thu Apr 03, 2008 8:07 pm Post subject: |
|
|
thank you...
I'd been looking at this from the wrong angle. I think the QF2 version is addDataSource and I was using that for other inputs - simply not clicking that I need this for <selects> as well!
Cheers - all working now! |
|
| Back to top |
|
 |
Jb boin
Joined: 20 May 2008 Posts: 6 Location: Grenoble, France
|
Posted: Thu May 29, 2008 5:48 pm Post subject: |
|
|
I am also looking for a solution but addoption is not a good one as it add a new option instead of just marking as selected an existing one and for adddatasource i havent foud how it could solve the problem.
Does anyone have a solution to put a selected value on QF2? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
|
| Back to top |
|
 |
Jb boin
Joined: 20 May 2008 Posts: 6 Location: Grenoble, France
|
Posted: Fri May 30, 2008 10:12 am Post subject: |
|
|
Thanks for the answer but i still dont see why and how to use adddatasource for this job, i found a workaround but its unelegant :
| Code: | function selected($object, $array, $selected) {
foreach ($array as $id => $name) {
if ($id == $selected) {
$object->addoption($name, $id, selected);
} else {
$object->addoption($name, $id);
}}
} |
And i call it this way :
| Code: | $usercountry = & $fsinfos->addelement('select', 'country', null, array('label' => 'Country:')); selected($usercountry, $countrieslistar, $userar[0][usercountry]);
|
|
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Fri May 30, 2008 12:18 pm Post subject: |
|
|
| Do you have experience with HTML_QuickForm 3.x? Setting default values (including selected values of select boxes) was done via setDefaultValues(). In QF2 this concept was made more flexible by data sources. And therefore, addDataSource() is the way to pass default values to forms with QF2. |
|
| Back to top |
|
 |
Jb boin
Joined: 20 May 2008 Posts: 6 Location: Grenoble, France
|
Posted: Mon Jun 02, 2008 12:07 pm Post subject: |
|
|
I never user QF 3.x and for the adddatasource can you give me an example to elect an item on a select because documentation isn't really clear on that.
Thanks by advance. |
|
| Back to top |
|
 |
|