5

I want to add a table to a config section, where one column is a multiselect field.

Code is simliar to https://www.integer-net.com/how-to-create-tables-in-magento-system-configuration/, but Magento only supports select (not multiselect) fields.

enter image description here Similar to this, just add mutliselect field instead of text field


Any ideas how to solve it?

Btw ... I'm pretty sure that a working solution was already posted at github/gist a long time ago as a Hackathon project, but I can't find it anymore ...

1 Answer 1

6
+50

I haven't tested this, so I'm just taking a wild guess.
The link you mentioned from integer-net explains how to use a select field using a class Namespace_Module_Block_Config_Adminhtml_Form_Field_Country as renderer.

Using the same example, just add a new method to the renderer class (mentioned above)

public function getExtraParams()
{
    return ' multiple="multiple" ';
}

On the other hand, this might not work since for multiple selects you have too have the input name like name[] instead of name so you can receive an array via post or get from it.

If it does not work try to replace

   $this->addColumn('country_id', array(
        'label' => Mage::helper('namespace_module')->__('Country'),
        'renderer' => $this->_getRenderer(),
    ));

with

   $this->addColumn('country_id[]', array(
        'label' => Mage::helper('namespace_module')->__('Country'),
        'renderer' => $this->_getRenderer(),
    ));

Again, not sure this will work.
I'm just throwing out ideas that are simple to test and don't involve a lot of coding. Maybe you get lucky and one works.


Update

In the same class where you added the method getExtraParams try adding / changing the method setInputName.
Make it look like this:

public function setInputName($value)
{
    return $this->setName($value.'[]');
}

second Update

change the method _prepareArrayRow in your shippingCosts block (in the example) to this:

protected function _prepareArrayRow(Varien_Object $row)
{
    $countries = $row->getData('country_id');
    foreach ($countries as $country) {
        $row->setData(
            'option_extra_attr_' . $this->_getRenderer()
                ->calcOptionHash($country),
            'selected="selected"'
        );
    }
}

and it should work. And put back the column name from country_id[] to country_id.

8
  • 1
    +500 if this had worked out of the box! Good: multiselect field is displayed. Bad: I can't save multiple values. Only lasted one is saved. a:1:{s:18:"_1489342298412_412";a:2:{s:10:"country_id";s:2:"VN";s:11:"email_delay";s:3:"999";}}
    – sv3n
    Commented Jul 27, 2017 at 23:07
  • I thought it might happen. Didn't it work with category_id[] ?
    – Marius
    Commented Jul 28, 2017 at 5:50
  • Was I typo. Still had country_id instead of country_id[]. But with adding [] fields get not rendered anymore.
    – sv3n
    Commented Jul 28, 2017 at 10:25
  • 1
    @sv3n See my update. If that does not help, I will stop shooting in the dark and actually try to reproduce it on Monday.
    – Marius
    Commented Jul 28, 2017 at 19:55
  • 1
    I will try this on Monday. Until now I just did speculations. It's time to code :)
    – Marius
    Commented Jul 29, 2017 at 22:26

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.