0

I have made a custom admin form which has a drop-down. Based on the option selected in the dropdown, I want to change the value of the text field below using AJAX. enter image description here

In the above pic, I want to change value of Order State field based on option selected in Order Status field.

Edit/Tab/Main.php:

    protected function _prepareForm()
    {
        /* @var $model \Iksula\Orderitemstatestatus\Model\BlogPosts */
        $model = $this->_coreRegistry->registry('orderitemstatestatusmapping');

        $isElementDisabled = false;

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();

        $form->setHtmlIdPrefix('page_');

        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Item Information')]);

        if ($model->getId()) {
            $fieldset->addField('id', 'hidden', ['name' => 'id']);
        }



        $fieldset->addField(
            'item_state_id',
            'select',
            [
                'label' => __('Item State'),
                'title' => __('Item State'),
                'name' => 'item_state_id',
                'required' => true,
                'options' => $this->grid->getItemState(),
                'disabled' => $isElementDisabled
            ]
        );



        $fieldset->addField(
            'item_status_id',
            'select',
            [
                'label' => __('Item Status'),
                'title' => __('Item Status'),
                'name' => 'item_status_id',
                'required' => true,
                'options' => $this->grid->getItemStatus(),
                'disabled' => $isElementDisabled
            ]
        );



        $orderStatus = $fieldset->addField(
            'order_status',
            'select',
            [
                'label' => __('Order Status'),
                'title' => __('Order Status'),
                'name' => 'order_status',
                'required' => true,
                'options' => $this->grid->getOrderStatus(),
                'disabled' => $isElementDisabled
            ]
        );          

        $fieldset->addField(
            'order_state',
            'text',
            [
                'name' => 'order_state',
                'label' => __('Order State'),
                'title' => __('Order State'),
                'required' => true,
                'disabled' => true,
            ]
        );      $model->setData('order_state', self::getStateValue());  
        if (!$model->getId()) {
            $model->setData('is_active', $isElementDisabled ? '0' : '1');
        }
        $form->setValues($model->getData());
        $this->setForm($form);
        return parent::_prepareForm();
    }

    /**
     * Prepare label for tab
     *
     * @return \Magento\Framework\Phrase
     */
    public function getTabLabel()
    {
        return __('Item Information');
    }

    public function getStateValue(){
        return "value dependent on dropdown select";
    }

    /**
     * Prepare title for tab
     *
     * @return \Magento\Framework\Phrase
     */
    public function getTabTitle()
    {
        return __('Item Information');
    }

    /**
     * {@inheritdoc}
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Check permission for passed action
     *
     * @param string $resourceId
     * @return bool
     */
    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }

    public function getTargetOptionArray(){
        return array(
                    '_self' => "Self",
                    '_blank' => "New Page",
                    );
    }

2 Answers 2

1

you can do so, by simply adding javascript. In order to add javascript, you can use this in main.php file like

$field->setAfterElementHtml('<script>
//<![CDATA[

// here goes your custom Javascript

//]]>
</script>');

by using inspect element, check the id of those two fields and use them in your custom js script.

1
  • I am newbie in magento so was not aware about this method. Thanks.
    – Toji
    Commented Oct 22, 2018 at 5:42
0

You can easily do it by using javascipt. You can make one field and on changing option u will be able to see different fields. You can take the help of following link

magento 2.1 How do i create form component field custom depends on another field value

1
  • This won't work in layout grid but will work for forms made with UI components. Thanks for answering.
    – Toji
    Commented Oct 22, 2018 at 5:43

Your Answer

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

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