Here am added a dropdown for a list of type. based on selected type I need to show or hide the another field. how to achieve it. Here is my code.

$mainTypeField = $fieldset->addField(
    'school_type',
    'select',
    [
        'label' => __('School Type'),
        'title' => __('School Type'),
        'name' => 'school_type',
        'options' => $this->getTypeOption(),
        'disabled' => $isElementDisabled
    ]
);  
$mainTypeField = $fieldset->addField(
    'school_city',
    'select',
    [
        'label' => __('City'),
        'title' => __('City'),
        'name' => 'school_city',
        'class' => 'school_city',
        'options' => $this->getCityOption(),
        'disabled' => $isElementDisabled
    ]
);


$mainTypeField->setAfterElementHtml('<script>
//<![CDATA[
    require(["jquery"], function ($) {

        var prd = $("#page_type").val();
                if(prd == 3)
                {
                    $("#page_prd").show();
                }
                else
                {
                    $("#page_prd").hide();  
                }

        $(document).ready(function() {
            $("select[name=school_type]").change(function () {
                
                var prd = $("#page_type").val();

                if(prd == 3)
                {
                    $("#page_prd").show();
                }
                else
                {
                    $("#page_prd").hide();  
                }
            });
    });
    
//]]>
</script>');

 public function getTypeOption(){
        $data_array=array(); 
            $data_array[0]='Type 1';
            $data_array[1]='Type 2';
            $data_array[2]='Type 3';
            $data_array[3]='Type 4';
        return($data_array);
    }

Can I get Help.? Thank You in advance

有帮助吗?

解决方案

$action = $fieldset->addField(
    "main_field",
    "select",
    [
        "label" => __("School Type"),
        "class" => "required-entry",
        "name" => "main_field",
        "values" => [
            ["value" => 0,"label" => __("Type 1")],
            ["value" => 1,"label" => __("Type 2")],
        ]
    ]
);

$school_name = $fieldset->addField(
    "school_name",
    "select",
    [
        "label" => __("school name"),
        "class" => "required-entry",
        "name" => "school_name",
        'options' => $this->getOptionArray()
    ]
);

$school_board = $fieldset->addField(
    "school_board",
    "select",
    [
        "label" => __("school board"),
        "class" => "required-entry",
        "name" => "school_board",
        'options' => $this->getBoardOption()
    ]
);

$school_city = $fieldset->addField(
    "school_city",
    "select",
    [
        "label" => __("school city"),
        "class" => "required-entry",
        "name" => "school_city",
        'options' => $this->getCityOption()
    ]
);

$anotherField = $fieldset->addField(
    "dependent_field",
    "textarea",
    [
        "label" => __("Dependent Field"),
        "name" => "dependent_field"
    ]
);

$this->setChild(
    'form_after',
    $this->getLayout()->createBlock('\Magento\Backend\Block\Widget\Form\Element\Dependence')
    ->addFieldMap($action->getHtmlId(), $action->getName())
    ->addFieldMap($anotherField->getHtmlId(), $anotherField->getName())
    ->addFieldDependence($anotherField->getName(), $action->getName(), 1)
    ->addFieldMap($school_name->getHtmlId(), $school_name->getName())
    ->addFieldMap($school_board->getHtmlId(), $school_board->getName())
    ->addFieldMap($school_city->getHtmlId(), $school_city->getName())
    ->addFieldDependence($school_name->getName(), $action->getName(), 0)
    ->addFieldDependence($school_board->getName(), $action->getName(), 0)
    ->addFieldDependence($school_city->getName(), $action->getName(), 0)
);

For more detail, please refer to the link

其他提示

Try this code

    $main = $fieldset->addField(
        "school_type",
        "select",
        [
            'label' => __('School Type'),
            'title' => __('School Type'),
            "class"     =>      "required-entry",
            "name"      =>      "school_type",
            "values"    =>      [
                ["value" => 0,"label" => __("Hide")],
                ["value" => 1,"label" => __("Show")],
            ]
        ]
    );
    $sub = $fieldset->addField(
        "school_city",
        "select",
        [
             'label' => __('City'),
            'title' => __('City'),
            "class"     => "required-entry",
            "required"  => true,
            "name"      => "school_city",
            "values"    =>      [
                ["value" => 0,"label" => __("City - 1")],
                ["value" => 1,"label" => __("City - 2")],
            ]
        ]
    );
    $this->setChild(
        'form_after',
        $this->getLayout()->createBlock('\Magento\Backend\Block\Widget\Form\Element\Dependence')
            ->addFieldMap($main->getHtmlId(), $main->getName())
            ->addFieldMap($sub->getHtmlId(), $sub->getName())
            ->addFieldDependence($sub->getName(), $main->getName(), 1)
    );
许可以下: CC-BY-SA归因
scroll top