I have a working list based on an sql query. Now I would like to add some spacers (not selectable items).
When doing this manually in xml you can add the disabled attr to that item, but how can I do that when filling the list like so:
$cities = array(
array('value' => 1, 'text' => 'New York'),
array('value' => 2, 'text' => 'Chicago'),
array('value' => 3, 'text' => 'San Francisco'),
);
I have tried to add 'disabled' => 'true'
to the item array and some different versions of 'attribute' => 'disabled'
, but none of them worked so far.
EDIT: What it should look like:
<option value="" disabled>---Letters---</option>
<option value="x">X</option>
<option value="y">Y</option>
<option value="z">Z</option>
<option value="" disabled>---Numbers---</option>
<option value="1">1</option>
<option value="2">2</option>
...
In the rendered output, Letters and Numbers should just be a "divider" in the select list that is not selectable - "optgroups" are the right keyword, yes.
Edit 2: Okay I've got it now, partly, with the information from mickmackusa:
public function getOptions() {
$type = $this->element['context'];
$component = $this->element['component'] ? $this->element['component'] : 'com_content';
$context = $component . '.' . $type;
$jcFields = FieldsHelper::getFields($context, '*', true); // get custom field names by article id
$options = array();
$options[] = JHTML::_('select.optgroup', '---Article---');
$options[] = JHTML::_( 'select.option', 'nxItemId', '***Item ID***' );
$options[] = JHTML::_('select.optgroup', '---Customfields---');
foreach ($jcFields as $jcField){
$options[] = JHTML::_( 'select.option', $jcField->name, $jcField->title );
}
return $options;
}
The second optgroup header (---Customfields---) will not be rendered.
<optgroup>
tags? They aren't selectable.JFormFieldList
, you should tryJFormFieldGroupedList
instead.