Joomla! Programmierung/Framework/JHtmlSelect/option
Aus Joomla! Dokumentation
Erzeugt ein Objekt, das zur Weiterverarbeitung an eine Listenmethode übergeben werden kann.
Inhaltsverzeichnis |
[Bearbeiten] Syntax
option( $value, $text, $value_name, $text_name, $disable )
| Parameter | Datentyp | Beschreibung | Standardwert |
|---|---|---|---|
| $value | string | Wert des <option> Feldes. | |
| $text | string | Text des <option> Feldes. | |
| $value_name | string | Name des Objektattributes, dass den Wert enthält. | value |
| $text_name | string | Name des Objektattributes, dass den Text enthält. | text |
| $disable | bool | Setzt das <option> Feld auf disable="disable"
|
false |
[Bearbeiten] Beispiele
[Bearbeiten] Werte in Objekt umwandeln
$wert = array('wert1', 'wert2', 'wert3', 'wert4'); $text = array('text1', 'text2', 'text3', 'text4'); $option = array(); foreach ($wert as $key => $value) { $option[] = JHtml::_('select.option', $wert[$key], $text[$key]); } print_r($option);
Ausgabe:
Array
(
[0] => stdClass Object
(
[value] => wert1
[text] => text1
[disable] =>
)
[1] => stdClass Object
(
[value] => wert2
[text] => text2
[disable] =>
)
[2] => stdClass Object
(
[value] => wert3
[text] => text3
[disable] =>
)
[3] => stdClass Object
(
[value] => wert4
[text] => text4
[disable] =>
)
)[Bearbeiten] Quellcode
public static function option($value, $text = '', $optKey = 'value', $optText = 'text', $disable = false) { $options = array('attr' => null, 'disable' => false, 'option.attr' => null, 'option.disable' => 'disable', 'option.key' => 'value', 'option.label' => null, 'option.text' => 'text'); { // Merge in caller's options } else { // Get options from the parameters $options['option.key'] = $optKey; $options['option.text'] = $optText; $options['disable'] = $disable; } $obj->$options['option.key'] = $value; /* * If a label is provided, save it. If no label is provided and there is * a label name, initialise to an empty string. */ $hasProperty = $options['option.label'] !== null; { $labelProperty = $hasProperty ? $options['option.label'] : 'label'; $obj->$labelProperty = $options['label']; } elseif ($hasProperty) { $obj->$options['option.label'] = ''; } // Set attributes only if there is a property and a value if ($options['attr'] !== null) { $obj->$options['option.attr'] = $options['attr']; } // Set disable only if it has a property and a value if ($options['disable'] !== null) { $obj->$options['option.disable'] = $options['disable']; } return $obj; }