Magento 2:当字段不在同一组中时,字段取决于system.xml


10

如何<depends>为不在同一组字段中的字段设置

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="section" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Name</label>
            <tab>tabname</tab>
            <resource>Namespace_ModuleName::method</resource>
            <group id="group" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Configuration</label>
                <field id="field" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
            <group id="connection" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Connection Configuration</label>
                <field id="disable_certificate_check" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Check</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <depends>
                        <field id="field">1</field>
                    </depends>
                </field>
            </group>
        </section>
    </system>
</config>

Answers:


40

“依赖”节点中的字段ID必须包含您要依赖的字段的部分,组和字段ID

<depends>
    <field id="section_id/group_id/field_id">1</field>
</depends>

是的,尝试相同的方法,但是当从第一个组中选择“否”然后隐藏两个组时,请进一步详细说明您的答案。
Dhrumin

1
@ St3phan,我们如何隐藏整个团队?
mshakeel

@mshakeel,我不知道是否可以,但是让我有一点时间进行测试。
St3phan

1
@ St3phan,使用Java语言完成了(已回答)。分享,如果您找到合适的方法。
mshakeel

当然,您可以使用jQuery代码。
St3phan

1

隐藏配置组

对于magento 2.1.x,您可以使用以下jQuery切换相关的配置组:

<comment><![CDATA[
<script type="text/javascript">//<![CDATA[
    require(['jquery'], function(){
        if (jQuery('#field_id').val() == 'value_to_compare') {
            toggleDependantGroups(true);
        }

        jQuery('#field_id').change(function() {
            if (jQuery(this).val() == 'value_to_compare') {
                toggleDependantGroups(true);
            } else {
                toggleDependantGroups(false);
            }
        });

        function toggleDependantGroups(hide=true)
        {
            if (hide) {
                jQuery('#section-id').closest('div.section-config').hide();
                jQuery('#section-id').closest('div.section-config').hide();
                jQuery('#last-visible-section-id').closest('div.section-config').css('border-bottom-width', '0px');
            } else {
                jQuery('#section-id').closest('div.section-config').show();
                jQuery('#section-id').closest('div.section-config').show();
                jQuery('#last-visible-section-id').closest('div.section-config').css('border-bottom-width', '1px');
            }
        }
    });
</script>]]>

如有必要,请替换ID。


1
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="your_id" translate="label" sortOrder="1000">
            <label>your_label</label>
        </tab>
        <section id="your_id" translate="label" type="text" sortOrder="340" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>your_label</label>
            <tab>your_tab</tab>
            <resource>Your_Module::config</resource>
            <group id="your_id" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>your_label</label>
                <field id="your_id" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>your_label</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="your_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>your_label</label>
                    <depends>
                        <field id="*/*/active">1</field>
                    </depends>
                </field>
                <field id="your_id" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>your_label</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                     <depends>
                        <field id="*/*/active">1</field>
                    </depends>
                </field>
            </group>
            <group id="your_id" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>your_label</label>
                <field id="your_id" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>your_label</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>               
                <depends>
                    <field id="section_id/group_id/field_id">1</field>
                </depends>
            </group>



        </section>
    </system>
</config>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.