magento adminhtml字段可以依赖于多个字段或值吗?

epe*_*leg 5 magento magento-1.4

http://alanstorm.com/magento_system_configuration_in_depth_tutorial中, @ AlanStorm提供了一个非常好的系统配置教程.

他还解释了如何使用<depends>标签仅在另一个字段中设置特定值时才显示字段.

我的Q是如果字段A具有值V1或V2,我如何使fieldB可见.<depends>还有其他选项吗?

此外,如果有人知道magento的代码在哪里实现,我也希望自己看一下代码.

谢谢

Her*_*tin 14

如果我正确理解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>
Run Code Online (Sandbox Code Playgroud)

请注意,depends_from_field_value_1depends_from_field_value_2用逗号分隔,这是在中声明的确切符号separator=","


小智 3

有更好的方法,但是您需要覆盖核心文件

重写以下文件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;
}
Run Code Online (Sandbox Code Playgroud)

在app\code\core\Mage\Adminhtml\Block\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);
                }
            }
Run Code Online (Sandbox Code Playgroud)

修改javascript文件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();
    }
}
Run Code Online (Sandbox Code Playgroud)

使用以下语法来实现对 xml 的多个值依赖

                            <depends>
                            <field1>
                                <val1>text</val1>
                                <val2>radio</val2>
                            </field1>
                        </depends>
Run Code Online (Sandbox Code Playgroud)