How to add a multiselect field to system config field table?
-
09-10-2020 - |
题
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.
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 ...
解决方案
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
.