问题在于Magento的Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Select类中和Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Options类。
回顾过滤器类的getHtml()方法,看起来他们确实编写了此方法,但预期支持<optgroup>
,但是失败的地方在于该类的_getOptions()方法。在这里,我们可以看到它们将它构建成一个2维数组,无论选项数据是以什么格式提供的。就您的情况而言,我假设您的选项已经以包含“label”,“value”结构到你的$options
变量。
另一方面,Magento的Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Options类似乎没有被写入支持使用optgroup
的选项。
为了解决这个问题,我们可以用一些额外的逻辑重写这些类来支持我们正在寻找的东西。
注:您还没有关于您指定(adminhtml_categorymapping_widget_grid_column_renderer_options)您的渲染器类中的任何信息,所以我不知道,你可以用类是从类型的任何其他网格列单独做什么“选项”。接下来我的解决方案只允许这种类型的列支持选项组。
步骤1:创建我们的扩展定义文件: app/code/etc/modules/StackOverflow_Question27633881.xml
<?xml version="1.0"?>
<config>
<modules>
<StackOverflow_Question27633881>
<active>true</active>
<codePool>local</codePool>
</StackOverflow_Question27633881>
</modules>
</config>
步骤2:创建我们的扩展的配置文件,其中包含的指令改写上述两个法师类: app/code/local/StackOverflow/Question27633881/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<StackOverflow_Question27633881>
<version>1.0.0</version>
</StackOverflow_Question27633881>
</modules>
<global>
<blocks>
<!-- Rewrites -->
<adminhtml>
<rewrite>
<widget_grid_column_filter_select>StackOverflow_Question27633881_Block_Adminhtml_Widget_Grid_Column_Filter_Select</widget_grid_column_filter_select>
<widget_grid_column_renderer_options>StackOverflow_Question27633881_Block_Adminhtml_Widget_Grid_Column_Renderer_Options</widget_grid_column_renderer_options>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
Ste第3页:用更新的逻辑 app/code/local/StackOverflow/Question27633881/Block/Adminhtml/Widget/Grid/Column/Filter/Select.php
<?php
class StackOverflow_Question27633881_Block_Adminhtml_Widget_Grid_Column_Filter_Select
extends Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Select
{
/**
* (non-PHPdoc)
* @see Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Select::_getOptions()
*/
protected function _getOptions()
{
$emptyOption = array('value' => null, 'label' => '');
$optionGroups = $this->getColumn()->getOptionGroups();
if ($optionGroups) {
array_unshift($optionGroups, $emptyOption);
return $optionGroups;
}
$colOptions = $this->getColumn()->getOptions();
// Options have already been setup in a way that is "label"/"value" format. Don't mess with it any further
if (isset($colOptions[0]) && isset($colOptions[0]['label'])) {
return $colOptions;
}
if (!empty($colOptions) && is_array($colOptions)) {
$options = array($emptyOption);
foreach ($colOptions as $value => $label) {
$options[] = array('value' => $value, 'label' => $label);
}
return $options;
}
return array();
}
}
第4步创建我们的新的过滤器类:与更新逻辑建立我们新的呈现类: app/code/local/StackOverflow/Question27633881/Block/Adminhtml/Widget/Grid/Column/Renderer/Options.php
<?php
class StackOverflow_Question27633881_Block_Adminhtml_Widget_Grid_Column_Renderer_Options
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Options
{
/**
* Render a grid cell as options
*
* @param Varien_Object $row
* @return string
*/
public function render(Varien_Object $row)
{
$options = $this->getColumn()->getOptions();
$showMissingOptionValues = (bool)$this->getColumn()->getShowMissingOptionValues();
if (!empty($options) && is_array($options)) {
// Call our new function if necessary.
if ( isset($options[0]) && isset($options[0]['value'])
|| isset($options[1]) && isset($options[1]['value'])
) {
$options = $this->_getFlatOptions($options);
}
$value = $row->getData($this->getColumn()->getIndex());
if (is_array($value)) {
$res = array();
foreach ($value as $item) {
if (isset($options[$item])) {
$res[] = $this->escapeHtml($options[$item]);
}
elseif ($showMissingOptionValues) {
$res[] = $this->escapeHtml($item);
}
}
return implode(', ', $res);
} elseif (isset($options[$value])) {
return $this->escapeHtml($options[$value]);
} elseif (in_array($value, $options)) {
return $this->escapeHtml($value);
}
}
}
/**
* Our new function that will turn a set of options with option groups, to a flat array of options.
*
* @param array $grouped_options
*/
protected function _getFlatOptions($grouped_options)
{
$options = array();
foreach ($grouped_options as $option) {
if ('' == $option['value'] || null === $option['value']) {
continue;
}
if (is_string($option['value'])) {
$options[$option['value']] = $option['label'];
} elseif (is_array($option['value'])) {
foreach ($option['value'] as $opt) {
$options[$opt['value']] = $opt['label'];
}
}
}
return $options;
}
}
之后,冲洗Magento的Configuration
缓存如果启用则键入。