2

In Magento 2 at admin end from my custom module I have two options 1) Level and 2) Segment in the drop down.

As well I have another multiselect dropdowns like Levels and segments

Now if i choose level i need to display levels multiselect dropdown As well if i choose segment i need to display segments multiselect dropdown.

Please find the screenshots below.

I have referred one concept i.e field dependency at admin end for dropdown options how can we do this concept here.

Please check the below code

<?php


namespace Stackexchange\Custom\Block\Adminhtml\Earningpointrule\Edit\Tab;

class Target extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    /**
     * Wysiwyg config
     * 
     * @var \Magento\Cms\Model\Wysiwyg\Config
     */
    protected $_wysiwygConfig;

    /**
     * Country options
     * 
     * @var \Magento\Config\Model\Config\Source\Locale\Country
     */
    protected $_countryOptions;

    /**
     * Country options
     * 
     * @var \Magento\Config\Model\Config\Source\Yesno
     */
    protected $_booleanOptions;

    /**
     * constructor
     * 
     * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
     * @param \Magento\Config\Model\Config\Source\Locale\Country $countryOptions
     * @param \Magento\Config\Model\Config\Source\Yesno $booleanOptions
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
        \Magento\Config\Model\Config\Source\Locale\Country $countryOptions,
        \Magento\Config\Model\Config\Source\Yesno $booleanOptions,
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        array $data = []
    )
    {
        $this->_wysiwygConfig            = $wysiwygConfig;
        $this->_countryOptions           = $countryOptions;
        $this->_booleanOptions           = $booleanOptions;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {

        $post = $this->_coreRegistry->registry('stackexchange_custom_earningpointrule');
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('earningpointrule_');
        $form->setFieldNameSuffix('earningpointrule');
        $fieldset = $form->addFieldset(
            'base_fieldset',
            [
                'legend' => __('Target'),
                'class'  => 'fieldset-wide'
            ]
        );


       $parentField = $fieldset->addField('target_type', 
        'select', [
             'name' => 'target_type',
            'label' => __('Target Type'),
            'values' => $this->getTargetTypeOptions(),

        ]);         



        $childFieldOne= $fieldset->addField(
            'target_multiselect_levels',
            'multiselect',
            [
                'name'     => 'target_multiselect_levels',                
                'title' => __('Target Multiselect Levels'),
                'label' => __('Levels'),
                'required'=>true,               
                'values' => $this->getTagetTypeMultiselectLevels(),

            ]
        );

        $childFieldTwo=$fieldset->addField(
            'target_multiselect_segments',
            'multiselect',
            [
                'name'     => 'target_multiselect_segments',                
                'title' => __('Target Multiselect Segments'),
                'label' => __('Segments'),
                'required'=>true,
                'values' => $this->getTagetTypeMultiselectSegments(),

            ]
        );

        $dependence1 = $this->getLayout()->createBlock(
        'Magento\Backend\Block\Widget\Form\Element\Dependence')
        ->addFieldMap($parentField->getHtmlId(), $parentField->getName())
        ->addFieldMap($childFieldOne->getHtmlId(), $childFieldOne->getName())
        ->addFieldDependence(
            $childFieldOne->getName(),
            $parentField->getName(),
            '1'
            );

            $dependence2 = $this->getLayout()->createBlock(
        'Magento\Backend\Block\Widget\Form\Element\Dependence')
        ->addFieldMap($parentField->getHtmlId(), $parentField->getName())
        ->addFieldMap($childFieldTwo->getHtmlId(), $childFieldTwo->getName())
        ->addFieldDependence(
            $childFieldTwo->getName(),
            $parentField->getName(),
            '2'
            );


        $this->setChild('form_after', $dependence1);    
        $this->setChild('form_after', $dependence2);            


        $postData = $this->_session->getData('stackexchange_custom_earningpointrule_data', true);
        if ($postData) {
            $post->addData($postData);
        } else {
            if (!$post->getId()) {
                $post->addData($post->getDefaultValues());
            }
        }

        $form->addValues($post->getData());
        $this->setForm($form);

        return parent::_prepareForm();
    }

    /**
     * Prepare label for tab
     *
     * @return string
     */
    public function getTabLabel()
    {
        return __('Target');
    }

    /**
     * Prepare title for tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return $this->getTabLabel();
    }

    /**
     * Can show tab in tabs
     *
     * @return boolean
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * Tab is hidden
     *
     * @return boolean
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * @return array
     */
    public function getTargetTypeOptions() {

         return $applyOptions = [
                ['label' => __('Level'),   'value' =>  1],
                ['label' => __('Segment'), 'value' =>  2]
            ];
    }

    /**
     * @return array
     */
    public function getTagetTypeMultiselectLevels() {

    return [
            ['value' => 1, 'label' => 'Level 1'],
            ['value' => 2, 'label' => 'Level 2'],
            ['value' => 3, 'label' => 'Level 3']

           ]; 

    }


    /**
     * @return array
     */
    public function getTagetTypeMultiselectSegments() {



    return [
            ['value' => 1, 'label' => 'segment 1'],
            ['value' => 2, 'label' => 'segment 2'],
            ['value' => 3, 'label' => 'segment 3']

           ];
    }



    }



}

Please suggest me thanks

0

you can use javascript to do that i have done this in my ui_component form block form musve same way to do this

$parentField = $fieldset->addField('target_type', 
        'select', [
             'name' => 'target_type',
             'component' => 'Vendor_ModuleName/js/form/element/options',
            'label' => __('Target Type'),
            'values' => $this->getTargetTypeOptions(),

        ]);         



        $childFieldOne= $fieldset->addField(
            'target_multiselect_levels',
            'multiselect',
            [
                'name'     => 'target_multiselect_levels',                
                'title' => __('Target Multiselect Levels'),
                'label' => __('Levels'),
                'visible' => false
                'required'=>true,               
                'values' => $this->getTagetTypeMultiselectLevels(),

            ]
        );

        $childFieldTwo=$fieldset->addField(
            'target_multiselect_segments',
            'multiselect',
            [
                'name'     => 'target_multiselect_segments',                
                'title' => __('Target Multiselect Segments'),
                'label' => __('Segments'),
                'visible' => false
                'required'=>true,
                'values' => $this->getTagetTypeMultiselectSegments(),

            ]
        );

now go and create

Vendor\Module\view\adminhtml\web\js\options.js

define([
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',
    'Magento_Ui/js/modal/modal'
], function (_, uiRegistry, select, modal) {
    'use strict';
    return select.extend({

    initialize: function () {
        var value=this._super().initialValue; 
        console.log('Selected Value: ' + value);
        var field1 = uiRegistry.get('index = target_multiselect_levels');
        var field2 = uiRegistry.get('index = target_multiselect_segments');
        if (value=='level' ) {
            field1.show();
            field2.hide();
        } else {
            field1.hide();
            field2.show();
        }



        return this._super();
    },
    onUpdate: function (value) { 
        console.log('Selected Value: ' + value);

        var field1 = uiRegistry.get('index = target_multiselect_levels');
        var field2 = uiRegistry.get('index = target_multiselect_segments');
        if (value=='level' ) {
            field1.show();
            field2.hide();
        } else {
            field1.hide();




        return this._super();
    },
});
});

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.