1

I need to select one or more option before rendering this page. I tried several ways but unable to find an exact way to solve my issue.

I need to select Tea and Other Tea Option before loading.

enter image description here

Form.php

   protected function _prepareForm()
{
    $model = $this->_coreRegistry->registry('row_data');
    
    $form = $this->_formFactory->create(['data' => ['id' => 'edit_form', 'enctype' => 'multipart/form-data', 'action' => $this->getData('action'), 'method' => 'post']]);
    
    if ($model) {
        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Review Details'), 'class' => 'fieldset-wide']);
        $fieldset->addField('entity_id', 'hidden', ['name' => 'entity_id']);
    } else {
        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Review Details'), 'class' => 'fieldset-wide']);
    }
    $fieldset->addField(
        'shop_title',
        'text',
        [
            'name' => 'seller_name',
            'label' => __('Seller Name'),
            'title' => __('Seller Name'),
            'class' => 'required-entry',
            'required' => true,
            'disabled' => $model ? true : false,
        ]
    );

    $fieldset->addField(
        'categoryList',
        'multiselect',
        [
            'name' => 'categoryList[]',
            'label' => __('Categories'),
            'title' => __('Categories'),
            'values' => $model->getData('categoryList'),
            'disabled' => false

        ]
    );

    $form->setValues($model ? $model->getData() : '');
    $form->setUseContainer(true);
    $this->setForm($form);
    return parent::_prepareForm();
}

$model->getData('categoryList') Array

array (
      0 => 
      array (
        'label' => 'Wood & Wooden Products',
        'value' => '56',
      ),
      1 => 
      array (
        'label' => 'Food, Feed & Beverages',
        'value' => '35',
      ),
      2 => 
      array (
        'label' => 'Tea',
        'value' => '32',
      ),
      3 => 
      array (
        'label' => 'Other Tea',
        'value' => '167',
      ),
    )

array generate as below

  foreach ($categoryListArray as $catId)
  {
     $categoryObj = $this->categoryRepository->get($catId);
     $name = $categoryObj->getName();
     $options[] = ['label' => $name, 'value' => $catId ,];

  }

2 Answers 2

2

in jquery:

first load js in your page then try below code

require([
        'jquery',
        'mage/backend/form'
    ], function($){

        //simple way
        $("#categoryList").val(["32", "167"]);   

        //Iterate through the loop using the value in a dynamic selector that utilizes the attribute selector.

        var values="32,167";
        $.each(values.split(","), function(i,e){
        $("#categoryList option[value='" + e + "']").prop("selected", true);
        });

    });
1
  • Thank you very much. I also found the solution for it bit different way in your answer.
    – Singhe
    Jun 9, 2021 at 15:37
2

I Found the Solution for this.

$premiumCategory = json_encode($model->getData('premium_category'));
//output of $premiumCategory = ["32","167"]; 

$fieldset->addField(
            'categoryList',
            'multiselect',
            [
                'name' => 'categoryList[]',
                'label' => __('Categories'),
                'title' => __('Categories'),
                'values' => $model->getData('categoryList'),
                'disabled' => false,
                'style'     => 'width: 100%',
                'after_element_html' => '<script>
                 require([
                    "jquery"
                ], function(jQuery){
                        var arr = ' . $premiumCategory . ';
                       jQuery.each( arr, function( i, val ) {
                           jQuery("#categoryList option[value=\'" + val + "\']").attr("selected","selected");
                        });
                   });
                    </script>',
            ]
        );

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.