API16:JHtmlSelect/groupedlist
From Joomla! Documentation
Description
Generates a grouped HTML selection list from nested arrays.
<! removed transcluded page call, red link never existed >
Syntax
static groupedlist($data, $name, $options=array())
| Parameter Name | Default Value | Description |
|---|---|---|
| $data | An array of groups, each of which is an array of options. | |
| $name | The value of the HTML name attribute | |
| $options | array() | Options, an array of key/value pairs. Valid options are: |
Returns
string HTML for the select list
Defined in
libraries/joomla/html/html/select.php
Importing
jimport( 'joomla.html.html.select' );
Source Body
public static function groupedlist($data, $name, $options = array())
{
// Set default options and overwrite with anything passed in
$options = array_merge(
JHtml::$formatOptions,
array(
'format.depth' => 0,
'group.items' => 'items',
'group.label' => 'text',
'group.label.toHtml' => true,
'id' => false,
),
$options
);
// Apply option rules
if ($options['group.items'] === null)
{
$options['group.label'] = null;
}
$attribs = '';
if (isset($options['list.attr']))
{
if (is_array($options['list.attr']))
{
$attribs = JArrayHelper::toString($options['list.attr']);
}
else
{
$attribs = $options['list.attr'];
}
if ($attribs != '')
{
$attribs = ' ' . $attribs;
}
}
$id = $options['id'] !== false ? $options['id'] : $name;
$id = str_replace(array('[', ']'), '', $id);
// Disable groups in the options.
$options['groups'] = false;
$baseIndent = str_repeat($options['format.indent'], $options['format.depth']++);
$html = $baseIndent . '<select' . ($id !== '' ? ' id="' . $id . '"' : '')
. ' name="' . $name . '"'
. $attribs . '>'
. $options['format.eol']
;
$groupIndent = str_repeat($options['format.indent'], $options['format.depth']++);
foreach($data as $dataKey => $group)
{
$label = $dataKey;
$id = '';
if ($options['group.items'] == null)
{
// Sub-list is an associative array
$subList = $group;
$noGroup = is_int($dataKey);
}
elseif (is_array($group))
{
// Sub-list is in an element of an array.
$subList = $group[$options['group.items']];
$noGroup = false;
if (isset($group[$options['group.label']]))
{
$label = $group[$options['group.label']];
}
if (isset($options['group.id']) && isset($group[$options['group.id']]))
{
$id = $group[$options['group.id']];
}
}
elseif (is_object($group))
{
// Sub-list is in a property of an object
$subList = $group->$options['group.items'];
$noGroup = false;
if (isset($group->$options['group.label']))
{
$label = $group->$options['group.label'];
}
if (isset($options['group.id']) && isset($group->$options['group.id']))
{
$id = $group->$options['group.id'];
}
}
else
{
throw new JException('Invalid group contents.', 1, E_WARNING);
}
if($noGroup)
{
$html.=self::options($subList, $options);
}
else
{
$html .= $groupIndent
. '<optgroup' . (empty($id) ? '' : ' id="' . $id . '"')
. ' label="'
. ($options['group.label.toHtml'] ? htmlspecialchars($label, ENT_COMPAT, 'UTF-8') : $label)
. '">'
. $options['format.eol']
. self::options($subList, $options)
. $groupIndent . '</optgroup>'
. $options['format.eol']
;
}
}
$html .= $baseIndent . '</select>' . $options['format.eol'];
return $html;
}
<! removed transcluded page call, red link never existed >
Examples
Code Examples