0

Magento offers a function to generate a select from the backend: https://magently.com/blog/dropdown-selects-in-magento/

$select = $this->getLayout()->createBlock( 'core/html_select' )
    ->setName( 'addresses' )
    ->setOptions( $options );

This will give me something like this:

<select name="addresses">
    <option value="address1">address1</option>
    <option value="address2">address2</option>
</select>

but I want to set a class for the options:

<select name="addresses">
    <option value="address1" class="a">address1</option>
    <option value="address2" class="b">address2</option>
</select>

My options look like this:

$options[] = array(
    'value' => $address->getId(),
    'label' => $address->format( 'oneline' ),
    'parvw' => $address->getParvw()
);

parvw is the class parameter for the option

How can I set the class options?

1
  • Not sure how to do that with that function. I can think of a few workarounds that may be of use tho: If the purpose is to style them you can use css like:option[value=_address1] or jquery $('option > select > option[value="address1"]'). Or just a for each loop like: <?php $select = '<select name="addresses">'; foreach ($options as $option) { $select .= '<option value="' . $option['value'] . '" class="' . $option['parvw'] . '">' . $option['value'] . '</option>'; } $select .= '</select>'; ?>
    – harri
    Aug 3, 2017 at 8:42

1 Answer 1

3

Magento allow us to add additional params to the options of a select.

Here is how you need to modify your options:

$options[] = array(
    'value' => $address->getId(),
    'label' => $address->format( 'oneline' ),
    'params' => array ('class' => 'yourclass1')
);

$options[] = array(
    'value' => $address->getId(),
    'label' => $address->format( 'oneline' ),
    'params' => array ('class' => 'yourclass2')
);

For more details you can check the functions

  • Mage_Core_Block_Html_Select::setOptions()
  • Mage_Core_Block_Html_Select::_toHtml()
  • Mage_Core_Block_Html_Select::_optionToHtml()
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.