2014-12-24 24 views
0

我在網格過濾器中有一個類別列。我需要添加到該類別列下拉菜單中。我也創建了一個類型類,但是當在prepareColumn函數中調用這些類時,這些類並不適用。創建列如下:Magento問題應用<optgroup>進入gride過濾器下拉

$options = Mage::getModel('productsync/categorymapping_system_config_source_catalog_categorypaths') 
        ->toOptionArray(); 


    $this->addColumn(
     'category', 
     array(
      'header' => Mage::helper('productsync')->__('Category'), 
      'width' => '1', 
      'type'  => 'options', 
      'index' => 'category_id', 
      'editable' => true, 
      'options' => $options, 
      'renderer' => 'productsync/adminhtml_categorymapping_widget_grid_column_renderer_options', 
      'filter_condition_callback' 
          => array($this, '_filterCategoriesCondition'), 
     ) 
    ); 

任何人都可以請告訴我這是什麼問題。

謝謝

回答

0

問題在於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緩存如果啓用則鍵入。