How set atributte on individual option of select

5 years ago

alnux
Member | 106
+
+1
-

Hi again,

My questions is how can i set attribute with Form:setAttribute() method an option of select box

the next html code is generate by

$form->addSelect('parent_id', 'Recurso Padre:', $recursos, 15)->setAttribute('class', 'form-control');
<select name="parent_id" size="15" class="form-control" id="frm-crearEditar-parent_id">
<option value="0"></option>
<option value="1">Permiso Acl</option>
<option value="2">-- Sub de Acl Permiso</option>
<option value="3">---- Sub de sub de Acl Permiso</option>
<option value="4"> Acl Permiso 2</option>
<option value="5" selected>-- Sub de Acl Permiso2</option>
<option value="10">---- Nuevo recurso</option>
</select>

is there any way to add a " disabled " attribute to an option tag from Present class??

<option value="3" disabled >---- Sub de sub de Acl Permiso</option>

thanks

Last edited by alnux (2014-05-30 19:28)

5 years ago

alnux
Member | 106
+
+2
-

I found a solution and share you

$arr = array(
    'item' => Nette\Utils\Html::el('option')->value('item')->setHtml('popisek'),
    'item2' => Nette\Utils\Html::el('option')->value('item2')->setHtml('popisek')->disabled(TRUE),
);

$form->addSelect('select', 'select', $arr)
// případně
$form['select']->setItems($arr);

thanks to Aurielle from link

5 years ago

Majkl578
Moderator | 1379
+
0
-

Unfortunately, this is something “view” part of application should take care of. Sadly there is probably no better way in Nette.