can a magento adminhtml field depend on more then one field or value?

前端 未结 4 1606
名媛妹妹
名媛妹妹 2020-12-17 03:40

In http://alanstorm.com/magento_system_configuration_in_depth_tutorial @AlanStorm gives a very good tutorial for system configuration.

He also explains how to use a

4条回答
  •  悲&欢浪女
    2020-12-17 04:03

    There is a better way, but you will need to override the core files

    Override the method under the following file app\code\core\Mage\Adminhtml\Block\Widget\Form\Element\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;
    }
    

    On the app\code\core\Mage\Adminhtml\Block\System\Config\Form.php Modify the method 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);
                    }
                }
    

    Modify the javascript file js\mage\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();
        }
    }
    

    Use the following syntax for multiple values dependency on your xml

                                
                                
                                    text
                                    radio
                                
                            
    

提交回复
热议问题