magento adminhtml 字段可以依赖于多个字段或值吗? [英] can a magento adminhtml field depend on more then one field or value?
问题描述
在 http://alanstorm.com/magento_system_configuration_in_depth_tutorial 中,@AlanStorm 提供了非常好的系统配置教程.
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 <depends> tag to make a field show only when a specific value is set in another field.
我的问题是,如果字段 A 的值为 V1 或 V2,我如何使字段 B 可见.<depends> 还有其他选项吗?
My Q is how can I make fieldB visible if field A has either value V1 or V2. and are there any other options with the <depends> ?
另外,如果有人知道在 magento 的代码中实现了这一点,我也想自己看看代码.
Also If someone knows where in magento's code this is implemented I would also like to have a look at the code myself.
谢谢
推荐答案
有更好的方法,但您需要覆盖核心文件
There is a better way, but you will need to override the core files
覆盖下面文件下的方法appcodecoreMageAdminhtmlBlockWidgetFormElementDependence.php
Override the method under the following file appcodecoreMageAdminhtmlBlockWidgetFormElementDependence.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;
}
在 appcodecoreMageAdminhtmlBlockSystemConfigForm.php修改方法initFields
On the appcodecoreMageAdminhtmlBlockSystemConfigForm.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);
}
}
修改javascript文件jsmageadminhtmlform.js
Modify the javascript file jsmageadminhtmlform.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 上的多值依赖使用以下语法
Use the following syntax for multiple values dependency on your xml
<depends>
<field1>
<val1>text</val1>
<val2>radio</val2>
</field1>
</depends>
这篇关于magento adminhtml 字段可以依赖于多个字段或值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!