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?

diazwatson
2,4782 gold badges28 silver badges39 bronze badges
asked Aug 3, 2017 at 7:44
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>'; ?> Commented 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()
answered Aug 3, 2017 at 8:53
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.