2

I have a product attribute but it is only relevant if another product attribute is selected (a dropdown yes/no) value.

In the admin view I would like show/hide my custom attribute depending on the yes/no value of the other system attribute.

I believe this is possible using javascript but im not sure how to get the javascript on the page with my attribute.

4 Answers 4

4

We can use 'depends' tag in system.xml in field which we have to show/hide depends on 'yes/no' field.

Below are syntax you can use:

<depends><enabled>1</enabled></depends>

In above syntax 'enabled' is a field with 'yes/no' values.

Above code is for system >> Configuration settings .

To add any field depends on 'yes/no' in product form you can use below code:

  $productField = $fieldset->addField('enabled', 'select', array(
        'label'     => Mage::helper('adminhtml')->__('Enabled'),
        'title'     => Mage::helper('adminhtml')->__('Enabled'),
        'name'      => 'enabled',
        'options'   => array(
            0 => Mage::helper('adminhtml')->__('No'),
            1 => Mage::helper('adminhtml')->__('Yes'),  
        ),
        'onclick' => 'toggleYesNo()',
    ));
    $productField->setAfterElementHtml('
        <script>
        function toggleFixedShipping() {
           $("idToShowHide").toggle();
        }
        </script>
    ');

Here 'idToShowHide' id will be show/hide field's ID.

I hope it solves your concern.

4

Check out app\code\core\Mage\Adminhtml\Block\Tax\Rate\Form.php for an example of how Magento does this, which is slightly different than Bijal Bhavsar's method.

First it defines it's Yes/No select

$fieldset->addField('zip_is_range', 'select', array(
    'name'    => 'zip_is_range',
    'label'   => Mage::helper('tax')->__('Zip/Post is Range'),
    'options' => array(
        '0' => Mage::helper('tax')->__('No'),
        '1' => Mage::helper('tax')->__('Yes'),
    )
));

Then another field, (which will be hidden sometimes)

$fieldset->addField('zip_from', 'text', array(
    'name'      => 'zip_from',
    'label'     => Mage::helper('tax')->__('Range From'),
    'required'  => true,
    'maxlength' => 9,
    'class'     => 'validate-digits'
));

Then it sets the dependance, right above _prepareForm()

$this->setChild(
    'form_after',
    $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('zip_is_range', 'zip_is_range')
        ->addFieldMap('zip_from', 'zip_from')
        ->addFieldDependence('zip_from', 'zip_is_range', '1')
);

Note on prefixes

If you at any point set a prefix like this: $form->setHtmlIdPrefix('mymodule_'); then you'll need to put the prefix in the dependence block like so:

$this->setChild(
    'form_after',
    $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('mymodule_zip_is_range', 'mymodule_zip_is_range')
        ->addFieldMap('mymodule_zip_from', 'mymodule_zip_from')
        ->addFieldDependence('mymodule_zip_from', 'mymodule_zip_is_range', '1')
);
1

You can add js files on the admin product page using the layout xml files: in tag: you can add either a js file in head tag:

<reference name="head">
   <action method="addItem"><type>skin_js</type><name>path to js</name></action>
</reference> 

Note: this should be in the magento skin adminhtml folder.

Or you could add a js block if you also need some php logic: For example:

<reference name="js">            
   <block type="core/template" name="custom.js" template="my path to js phtml"/>
</reference>

Note: this file should go in the adminhtml theme

0

If you want to display fields only if Select is YES Use below tag inside fields you want to display only on YES

You have to use depends tag, it can be used to declare dependencies to other fields. Is used to only show specific fields/groups when a given field has a value of 1

<field id="A_UNIQUE_FIELD_ID" translate="label" sortOrder="10" showInDefault="0" showInWebsite="0" showInStore="1" type="select">
   <label>Feature Flag Example</label>
   <comment>This field is an example for a basic yes or no select.</comment>
   <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<depends> <!-- This field will only be visible if the field with the id A_UNIQUE_FIELD_ID is set to value 1 -->
     <field id="A_UNIQUE_FIELD_ID">1</field>
</depends>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.