3

I have a select field like this in my costum magento admin form

$fieldset->addField(
    'category_id',
    'select',
    array(
        'id'    => 'lazadaCategory',
        'label'  => Mage::helper('test_sellercenter')->__('Category '),
        'name'   => 'status',
        'values' => Mage::helper('test_sellercenter/dropdown')->getLazadaCategories(),
        'class' => 'required-entry',
    )
);

and when the select value change i need to add another fields below this select field (not like toogle show/hide just add), for example like this field

$fieldset->addField(
    'listing_name',
    'text',
    array(
        'label' => Mage::helper('test_sellercenter')->__('Listing Name'),
        'name'  => 'listing_name',
        'required'  => true,
        'class' => 'required-entry',

    )
);

is there a way to do this in magento? for example like this add field example

1

For that you can use field dependencies feature of Magento, like here is the example

$status = $fieldSet->addField('status', 'select', array(
    'label'     => Mage::helper('module')->__('Status'),
    'name'      => 'status',
    'values'    => array(
        'Approved' => 'Approved',
        'Denied'   => 'Denied'
    )
));

$denyReason = $fieldSet->addField('denial_reason', 'textarea', array(
    'label'     => Mage::helper('module')->__('Denial Reason'),
    'name'      => 'denial_reason'
));

And add your dependencies

$this->setForm($form);
$this->setChild('form_after', $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
    ->addFieldMap($status->getHtmlId(), $status->getName())
    ->addFieldMap($denyReason->getHtmlId(), $denyReason->getName())                     
    ->addFieldDependence(
        $denyReason->getName(),
        $status->getName(),
        'Denied'
    )
);

$denyReason field only display If status is set to Denied. According to this you can define your field dependencies.

|improve this answer|||||
  • that's just like show/hide , what i want is like this link – Idham Choudry Dec 22 '16 at 10:15
  • It was my bad ,, sorry :) – Keyur Shah Dec 22 '16 at 10:16
  • No problem :), i hope you can help me with this question :D – Idham Choudry Dec 22 '16 at 10:20
0

use setAfterElementHtml function. Suppose, you want to show/hide the following field:

$fieldset->addField('field_to_hide', text, array(
    'label'  => $this->__('Test'),
));

Here is the dropdown/select field. It’s a yes/no field. Suppose, upon selecting ‘Show’, you will need to show the above mentioned field. And, upon selecting ‘Hide’, you will need to hide the above field.

Note the onChange parameter here. It’s calling showHideField() JS function.

$selectField = $fieldset->addField('select_field', 'select', array(
    'label' => 'Show/Hide Selection',
    'name' => 'select_field',           
    'values' => array(
        array(
            'value' => 1,
            'label' => 'Show',
        ),
        array(
            'value' => 0,
            'label' => 'Hide',
        ),
    ),
    'onChange'  => 'showHideField()',
));

The setAfterElementHtml is called which contains Javascript code to show/hide field

$selectField->setAfterElementHtml('
    <script>
    function showHideField() {                                              
        $("field_to_hide").toggle()
    }
    </script>
');
|improve this answer|||||
  • i dont want to show/hide field, i need to add more field when onchange function triggered – Idham Choudry Dec 23 '16 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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