2011-01-31 56 views
5

http://alanstorm.com/magento_system_configuration_in_depth_tutorial @AlanStorm提供了一個非常好的系統配置教程。可以一個magento adminhtml字段依賴多於一個字段或值?

他還解釋瞭如何使用一個< depends>標記來使字段僅在特定值設置在另一個字段中時顯示。

我的問題是,如果字段A的值爲V1或V2,我該如何使fieldB可見。 和<還有其他選項嗎?

另外如果有人知道magento的代碼是在哪裏實現的,我還想自己看一下代碼。

感謝

回答

3

有一個更好的辦法,但你需要重寫核心文件

覆蓋以下文件 應用程序\代碼\核心下的方法\法師\ Adminhtml \塊\小工具\表\元素\ Dependence.php

public function addFieldDependence($fieldName, $fieldNameFrom, $refValues) 
{ 
    /* 
    if (is_array($refValues)) { 
     Mage::throwException('Dependency from multiple values is not implemented yet. Please fix to your widget.xml'); 
    } 
    */ 
    $this->_depends[$fieldName][$fieldNameFrom] = $refValues; 
    return $this; 
} 

在應用程序\代碼\核心\法師\ Adminhtml \塊\ SYSTEM \ CONFIG \ form.php的 修改方法initFields

if ($e->depends) { 
       foreach ($e->depends->children() as $dependent) { 
        $dependentId = $section->getName() . '_' . $group->getName() . '_' . $fieldPrefix . $dependent->getName(); 
        if ($dependent->count()) { 
         $dependentValue = (array) $dependent; 
         $dependentValue = array_values($dependentValue); 
        } else { 
         $dependentValue = (string) $dependent; 
        } 

        $this->_getDependence() 
         ->addFieldMap($id, $id) 
         ->addFieldMap($dependentId, $dependentId) 
         ->addFieldDependence($id, $dependentId, $dependentValue); 
       } 
      } 

修改JavaScript文件JS \法師\ adminhtml \ form.js

trackChange : function(e, idTo, valuesFrom) 
{ 
    // define whether the target should show up 
    var shouldShowUp = true; 
    for (var idFrom in valuesFrom) { 

     if (valuesFrom.hasOwnProperty(idFrom)) { 
      if (typeof(valuesFrom[idFrom])=="object") { 
       shouldShowUp = false; 
       for(var idVal in valuesFrom[idFrom]) { 
        if (valuesFrom[idFrom].hasOwnProperty(idVal)) { 
         if (typeof(idVal)!="undefined" && ($(idFrom).value == valuesFrom[idFrom][idVal])) { 
          shouldShowUp = true; 
         } 
        } 
       } 
      } else if (typeof(valuesFrom[idFrom])=="string") { 
       if ($(idFrom).value != valuesFrom[idFrom]) { 
        shouldShowUp = false; 
       } 
      } 
     } 
     /* 
     if ($(idFrom).value != valuesFrom[idFrom]) { 
      shouldShowUp = false; 
     } 
     */ 
    } 

    // toggle target row 
    if (shouldShowUp) { 
     $(idTo).up(this._config.levels_up).select('input', 'select', 'td').each(function (item) { 
      if (!item.type || item.type != 'hidden') { // don't touch hidden inputs, bc they may have custom logic 
       item.disabled = false; 
      } 
     }); 
     $(idTo).up(this._config.levels_up).show(); 
    } else { 
     $(idTo).up(this._config.levels_up).select('input', 'select', 'td').each(function (item){ 
      if (!item.type || item.type != 'hidden') { // don't touch hidden inputs, bc they may have custom logic 
       item.disabled = true; 
      } 
     }); 
     $(idTo).up(this._config.levels_up).hide(); 
    } 
} 

用於多值依賴以下語法上的XML

      <depends> 
          <field1> 
           <val1>text</val1> 
           <val2>radio</val2> 
          </field1> 
         </depends> 
3

我不知道在哪裏艾倫的文章中的解釋,但我如何做到這一點:這只是一些JavaScript。
在你的組中,你把一個評論標籤與JavaScript嵌入到。
例如,這裏是我的代碼檢查,以顯示(或沒有),一個又一個一個字段的值:

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <sections> 
     <points_options translate="label" module="points"> 
      <tab>general</tab> 
      <label>Loyalty Points</label> 
      <frontend_type>text</frontend_type> 
      <sort_order>1002</sort_order> 
      <show_in_default>1</show_in_default> 
      <show_in_website>1</show_in_website> 
      <show_in_store>1</show_in_store> 
      <groups> 
       <config_points translate="label"> 
        <label>Configuration</label> 
        <comment><![CDATA[ 
         <script type="text/javascript"> 
          checkExpirationPeriod = function() { 
           if ($('points_options_config_points_expiration_period').getValue() > 0) { 
            $('points_options_config_points_expiration_reminder').up(1).appear(); 
           } else { 
            $('points_options_config_points_expiration_reminder').up(1).fade(); 
           } 
          } 

          Event.observe(window, 'load', function() { 
           Event.observe('points_options_config_points_expiration_period', 'change', checkExpirationPeriod); 
           checkExpirationPeriod(); 
          }) 
         </script> 
        ]]></comment> 

,你可以看到,我寫了一個小功能,檢查一個字段的值確定是否顯示另一個。然後,我將onchange事件鏈接到函數,並在加載頁面時觸發該函數顯示正確的字段。
爲了您的需要,只需在js函數中添加條件即可。
希望幫助

+0

+1所申報的確切符號分開骯髒的「評論代碼」詭計。 – epeleg 2011-01-31 20:05:19

+0

然而,看起來我沒有妥善處理我Q上的逃跑,並且兩個地方都看不到。所以我現在修復了問題,並且我希望有人能夠利用標籤提供幫助。請注意,「註釋中的代碼」技巧使翻譯註釋變得困難(如果您將此代碼置於實際上沒有真正意見的字段的註釋中,這可能不是問題)。 – epeleg 2011-01-31 20:09:41

0

安德魯的回答幾乎是沒有的伎倆。我現在在1.6.2.0和我修改app\code\core\Mage\Adminhtml\Block\System\Config\Form.phpinitFields()方法如下:

if ($e->depends) { 
    foreach ($e->depends->children() as $dependent) { 
     Mage::log((array)$dependent); 
     $dependentId = $section->getName() 
      . '_' . $group->getName() 
      . '_' . $fieldPrefix 
      . $dependent->getName(); 

     if ($dependent->hasChildren()) { 
      $dependentValue = (array) $dependent; 
      $dependentValue = array_values($dependentValue); 
     } else { 
      $dependentValue = (string) $dependent; 
     } 

     $shouldBeAddedDependence = true; 
     $dependentFieldName  = $fieldPrefix . $dependent->getName(); 
     $dependentField   = $group->fields->$dependentFieldName; 
     /* 
     * If dependent field can't be shown in current scope and real dependent config value 
     * is not equal to preferred one, then hide dependence fields by adding dependence 
     * based on not shown field (not rendered field) 
     */ 
     if (!$this->_canShowField($dependentField)) { 
      $dependentFullPath = $section->getName() 
       . '/' . $group->getName() 
       . '/' . $fieldPrefix 
       . $dependent->getName(); 
      if (is_array($dependentValue)) { 
       foreach ($dependentValue as $dependentOption) { 
        $shouldBeAddedDependence |= $dependentOption != Mage::getStoreConfig(
         $dependentFullPath, 
         $this->getStoreCode() 
        ); 
       } 
      } else { 
       $shouldBeAddedDependence = $dependentValue != Mage::getStoreConfig(
        $dependentFullPath, 
        $this->getStoreCode() 
       ); 
      } 
     } 
     if($shouldBeAddedDependence) { 
      $this->_getDependence() 
       ->addFieldMap($id, $id) 
       ->addFieldMap($dependentId, $dependentId) 
       ->addFieldDependence($id, $dependentId, $dependentValue); 
     } 
    } 
} 

而且它並不需要編輯的核心文件。 I overrode the admin theme插入我自己的form.js版本,並使用自定義模塊的​​3210重寫了這兩個PHP類。

13

如果我正確理解Magento 1.7.0.1的發行說明,此功能性已實施(http://goo.gl/ZgHG0)。我已經在Magento CE 1.7.0.2上成功地進行了測試。

你必須在場上扶養這樣的聲明分離參數:

<depends> 
    <depends_from_field separator=","> 
     depends_from_field_value_1,depends_from_field_value_2 
    </depends_from_field> 
</depends> 

注意depends_from_field_value_1depends_from_field_value_2一個逗號,即在separator=","

相關問題