0

how to show selected value in edit form in Magento admin using UI component many-to-many relation

public function toOptionArray()
    {
        if (!$this->_options) {

               $postCollection=$this->itemCollectionFactory->create()->load();

            $items = $this->collection->getItems();
             foreach ($items as $id){
                 $recipes_id=$id['recipes_id'];

             }

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
            $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
            $connection = $resource->getConnection();

            $values = $connection->fetchAll('SELECT * FROM `dac_recipes_category`,`dac_recipes_category_recipes_mapping` WHERE dac_recipes_category_recipes_mapping.recipes_id ='.$recipes_id.' AND dac_recipes_category.recipes_category_id=dac_recipes_category_recipes_mapping.recipes_category_id');




            foreach ($postCollection->getItems() as $da){

               foreach ($values as $selected){

                   if($selected['recipes_category_id']==$da->getRecipesCategoryId())
                   {
                       array_push($this->_options, ['value' => $da->getRecipesCategoryId(),'selectedOptions' => "selected", 'label' => __($da->getRecipesCategoryName())]);

                      break;
                   }
                   continue;
                }

                array_push($this->_options, ['value' => $da->getRecipesCategoryId(), 'label' => __($da->getRecipesCategoryName())]);


            }

enter image description here

Admin Form

 <field name="recipes_category_name">
            <argument name="data" xsi:type="array">
                <!-- Call to collection of 2nd module -->
                <item name="options" xsi:type="object">Dac\Recipes\Block\CategoryList</item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Category</item>
                    <!--<item name="formElement" xsi:type="string">select</item>-->
                    <item name="formElement" xsi:type="string">multiselect</item>
                    <item name="source" xsi:type="string">mode</item>
                    <item name="dataScope" xsi:type="string">recipes_category_name</item>
                    <item name="sortOrder" xsi:type="number">17</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>

console log form

<select multiple="" class="admin__control-multiselect" data-bind="
    attr: {
        name: inputName,
        id: uid,
        size: size ? size : '6',
        disabled: disabled,
        'aria-describedby': noticeId,
        placeholder: placeholder
    },
    hasFocus: focused,
    optgroup: options,
    selectedOptions: value,
    optionsValue: 'value',
    optionsText: 'label'" name="recipe[recipes_category_name]" id="B2D6PC1" size="6" aria-describedby="notice-B2D6PC1"><option data-title="Category 1" value="1">Category 1</option><option data-title="Category 2" value="2">Category 2</option><option data-title="Category 2" value="2">Category 2</option><option data-title="Category 3" value="3">Category 3</option><option data-title="Category 3" value="3">Category 3</option><option data-title="Category 4" value="4">Category 4</option></select>
  • try 'optionsValue'. – kunj Jun 4 '18 at 11:27
  • @kunj Thanks for reply , array_push($this->_options, ['value' => $da->getRecipesCategoryId(),'optionsValue' => "selected", 'label' => __($da->getRecipesCategoryName())]); not working – Nitesh Kumar Singh Jun 4 '18 at 11:30
  • I mean array with categories Id. array_push($this->_options, ['value' => $da->getRecipesCategoryId(),'optionsValue' => [12,1,14], 'label' => __($da->getRecipesCategoryName())]); – kunj Jun 4 '18 at 11:33
  • Try but not working @kunj – Nitesh Kumar Singh Jun 4 '18 at 11:47
  • array_push($this->_options, ['value' => $da->getRecipesCategoryId(),'optionsValue' => [$da->getRecipesCategoryId()], 'label' => __($da->getRecipesCategoryName())]); – Nitesh Kumar Singh Jun 4 '18 at 11:48

Your Answer

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

Browse other questions tagged or ask your own question.