API16

Difference between revisions of "JHtmlSelect/options"

From Joomla! Documentation

< API16:JHtmlSelect
(New page: ===Description=== Generates the option tags for an HTML select list (with no select tag surrounding the options). <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>[...)
 
m (preparing for archive only)
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
Generates the option tags for an HTML select list (with no select tag surrounding the options).
 
Generates the option tags for an HTML select list (with no select tag surrounding the options).
  
<span class="editsection" style="font-size:76%;">
 
<nowiki>[</nowiki>[[Description:JHtmlSelect/options|Edit Descripton]]<nowiki>]</nowiki>
 
</span>
 
  
{{Description:JHtmlSelect/options}}
+
 
 +
<! removed transcluded page call, red link never existed >
  
 
===Syntax===
 
===Syntax===
Line 194: Line 192:
 
</source>
 
</source>
  
<span class="editsection" style="font-size:76%;">
+
 
<nowiki>[</nowiki>[[SeeAlso:JHtmlSelect/options|Edit See Also]]<nowiki>]</nowiki>
+
<! removed transcluded page call, red link never existed >
</span>
 
{{SeeAlso:JHtmlSelect/options}}
 
  
 
===Examples===
 
===Examples===
<CodeExamplesForm />
+
=== Code Examples ===
 
<dpl>
 
<dpl>
 
  noresultsheader=\n
 
  noresultsheader=\n
 
  category=options
 
  category=options
 
  category=JHtmlSelect
 
  category=JHtmlSelect
  category=CodeExample
+
  namespace=CodeExample
 
  category=MethodExample
 
  category=MethodExample
 
  include=*
 
  include=*
 
  format= ,,,
 
  format= ,,,
 
</dpl>
 
</dpl>
 +
[[Category:Archived pages API16]]

Latest revision as of 20:47, 24 March 2017

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Generates the option tags for an HTML select list (with no select tag surrounding the options).


<! removed transcluded page call, red link never existed >

Syntax[edit]

static options($arr, $optKey= 'value', $optText= 'text', $selected=null, $translate=false)
Parameter Name Default Value Description
$arr An array of objects, arrays, or values.
$optKey 'value' If a string, this is the name of the object variable for the option value. If null, the index of the array of objects is used. If an array, this is a set of options, as key/value pairs. Valid options are:
$optText 'text'
$selected null
$translate false

Returns[edit]

string HTML for the select list

Defined in[edit]

libraries/joomla/html/html/select.php

Importing[edit]

jimport( 'joomla.html.html.select' );

Source Body[edit]

public static function options(
        $arr, $optKey = 'value', $optText = 'text', $selected = null, $translate = false
) {
        $options = array_merge(
                JHtml::$formatOptions,
                self::$_optionDefaults['option'],
                array(
                        'format.depth' => 0,
                        'groups' => true,
                        'list.select' => null,
                        'list.translate' => false,
                )
        );
        if (is_array($optKey)) {
                // Set default options and overwrite with anything passed in
                $options = array_merge($options, $optKey);
        } else {
                // Get options from the parameters
                $options['option.key'] = $optKey;
                $options['option.text'] = $optText;
                $options['list.select'] = $selected;
                $options['list.translate'] = $translate;
        }

        $html = '';
        $baseIndent = str_repeat($options['format.indent'], $options['format.depth']);

        foreach ($arr as $elementKey => &$element)
        {
                $attr = '';
                $extra = '';
                $label = '';
                $id = '';
                if (is_array($element))
                {
                        $key = $options['option.key'] === null
                                ? $elementKey : $element[$options['option.key']];
                        $text = $element[$options['option.text']];
                        if (isset($element[$options['option.attr']])) {
                                $attr = $element[$options['option.attr']];
                        }
                        if (isset($element[$options['option.id']])) {
                                $id = $element[$options['option.id']];
                        }
                        if (isset($element[$options['option.label']])) {
                                $label = $element[$options['option.label']];
                        }
                        if (isset($element[$options['option.disable']]) && $element[$options['option.disable']]) {
                                $extra .= ' disabled="disabled"';
                        }
                } elseif (is_object($element)) {
                        $key = $options['option.key'] === null
                                ? $elementKey : $element->$options['option.key'];
                        $text = $element->$options['option.text'];
                        if (isset($element->$options['option.attr'])) {
                                $attr = $element->$options['option.attr'];
                        }
                        if (isset($element->$options['option.id'])) {
                                $id = $element->$options['option.id'];
                        }
                        if (isset($element->$options['option.label'])) {
                                $label = $element->$options['option.label'];
                        }
                        if (isset($element->$options['option.disable']) && $element->$options['option.disable']) {
                                $extra .= ' disabled="disabled"';
                        }
                } else {
                        // This is a simple associative array
                        $key = $elementKey;
                        $text = $element;
                }

                /*
                 * The use of options that contain optgroup HTML elements was
                 * somewhat hacked for J1.5. J1.6 introduces the grouplist() method
                 * to handle this better. The old solution is retained through the
                 * "groups" option, which defaults true in J1.6, but should be
                 * deprecated at some point in the future.
                 */
                $key = (string) $key;
                if ($options['groups'] && $key == '<OPTGROUP>') {
                        $html .= $baseIndent . '<optgroup label="'
                                . ($options['list.translate'] ? JText::_($text) : $text)
                                . '">' . $options['format.eol'];
                        $baseIndent = str_repeat($options['format.indent'], ++$options['format.depth']);
                } else if ($options['groups'] && $key == '</OPTGROUP>') {
                        $baseIndent = str_repeat($options['format.indent'], --$options['format.depth']);
                        $html .= $baseIndent . '</optgroup>' . $options['format.eol'];
                } else {
                        // if no string after hypen - take hypen out
                        $splitText = explode(' - ', $text, 2);
                        $text = $splitText[0];
                        if (isset($splitText[1])) {
                                $text .= ' - ' . $splitText[1];
                        }

                        if ($options['list.translate'] && !empty($label)) {
                                $label = JText::_($label);
                        }
                        if ($options['option.label.toHtml']) {
                                $label = htmlentities($label);
                        }
                        if (is_array($attr)) {
                                $attr = JArrayHelper::toString($attr);
                        } else {
                                $attr = trim($attr);
                        }
                        $extra = ($id ? ' id="' . $id . '"' : '')
                                . ($label ? ' label="' . $label . '"' : '')
                                . ($attr ? ' ' . $attr : '')
                                . $extra
                        ;
                        if (is_array($options['list.select']))
                        {
                                foreach ($options['list.select'] as $val)
                                {
                                        $key2 = is_object($val) ? $val->$options['option.key'] : $val;
                                        if ($key == $key2) {
                                                $extra .= ' selected="selected"';
                                                break;
                                        }
                                }
                        } elseif ((string)$key == (string)$options['list.select']) {
                                $extra .= ' selected="selected"';
                        }

                        if ($options['list.translate']) {
                                $text = JText::_($text);
                        }

                        // Generate the option, encoding as required
                        $html .= $baseIndent . '<option value="'
                                . ($options['option.key.toHtml'] ? htmlspecialchars($key, ENT_COMPAT, 'UTF-8') : $key) . '"'
                                . $extra . '>'
                                . ($options['option.text.toHtml'] ? htmlentities(html_entity_decode($text), ENT_COMPAT, 'UTF-8') : $text)
                                . '</option>'
                                . $options['format.eol']
                        ;
                }
        }

        return $html;
}


<! removed transcluded page call, red link never existed >

Examples[edit]

Code Examples[edit]