From c0f164dcc7a9a5a1b680a6d90a37e325d778c106 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Tue, 12 Oct 2010 17:47:20 +0000 Subject: [PATCH] support optgroups in select --- lam/lib/html.inc | 70 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/lam/lib/html.inc b/lam/lib/html.inc index 40390485..6de58d1c 100644 --- a/lam/lib/html.inc +++ b/lam/lib/html.inc @@ -667,6 +667,8 @@ class htmlSelect extends htmlElement { private $selectedElements; /** descriptive elements */ private $hasDescriptiveElements = false; + /** contains optgroups */ + private $containsOptgroups = false; /** sorting enabled */ private $sortElements = true; /** right to left text direction */ @@ -676,9 +678,20 @@ class htmlSelect extends htmlElement { /** * Constructor. - * + * + *
Examples: + *
+ *
$select = new htmlSelect('myName', array('value1', 'value2'), array('value1')); + *
+ *
$select = new htmlSelect('myName', array('label1' => 'value1', 'label2' => 'value2'), array('value1')); + *
$select->setHasDescriptiveElements(true); + *
+ *
$select = new htmlSelect('myName', array('optgroupLabel' => array('value1', 'value2')), array('value1')); + *
$select->setHasDescriptiveElements(true); + *
$select->setContainsOptgroups(true); + * * @param String $name element name - * @param array $elements list of elememts (label => value) + * @param array $elements list of elements array(label => value) or array(value1, value2) or array('optgroup' => array(...)) * @param array $selectedElements list of selected elements (optional, default none) * @param int $size size (optional, default = 1) */ @@ -721,22 +734,49 @@ class htmlSelect extends htmlElement { } echo '\n"; $tabindex++; + if ($this->containsOptgroups) { + foreach ($this->elements as $label => $elements) { + if (sizeof($elements) > 0) { + echo ''; + $this->printOptionsHTML($elements); + echo ''; + } + } + } + else { + $this->printOptionsHTML($this->elements); + } + echo "\n"; + if ($this->multiSelect) { + return array($this->name => 'multiselect'); + } + else { + return array($this->name => 'select'); + } + } + + /** + * Prints the HTML code of the option tags. + * + * @param array $elements list of options + */ + private function printOptionsHTML($elements) { // sorting if ($this->sortElements) { if ($this->hasDescriptiveElements) { - $labels = array_keys($this->elements); + $labels = array_keys($elements); natcasesort($labels); $newElements = array(); foreach ($labels as $label) { - $newElements[$label] = $this->elements[$label]; + $newElements[$label] = $elements[$label]; } - $this->elements = $newElements; + $elements = $newElements; } else { - natcasesort($this->elements); + natcasesort($elements); } } - foreach ($this->elements as $key => $value) { + foreach ($elements as $key => $value) { $selected = ''; if ($this->hasDescriptiveElements) { if (in_array($value, $this->selectedElements)) { @@ -751,13 +791,6 @@ class htmlSelect extends htmlElement { echo "" . htmlspecialchars($value) . "\n"; } } - echo "\n"; - if ($this->multiSelect) { - return array($this->name => 'multiselect'); - } - else { - return array($this->name => 'select'); - } } /** @@ -769,6 +802,15 @@ class htmlSelect extends htmlElement { $this->hasDescriptiveElements = $hasDescriptiveElements; } + /** + * Specifies if the elements are divided into optgroups. + * + * @param boolean $containsOptgroups activates optgroups + */ + public function setContainsOptgroups($containsOptgroups) { + $this->containsOptgroups = $containsOptgroups; + } + /** * Specifies if multi-selection is enabled (default: disabled). *