1

I have created a multi-selectbox using Ui component and getting data from a model to display in Option-fields.

Data is saving in my custom table. but when i click on the same product to edit, the selected options are not showing as selected.

product_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customproducts">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Add values</item>
                <item name="provider" xsi:type="string">product</item>
                <item name="dataScope" xsi:type="string">data.product</item>
                <item name="sortOrder" xsi:type="number">2</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="opened" xsi:type="boolean">false</item>
                <item name="ns" xsi:type="string">product_form</item>
            </item>
        </argument>


    <field name="myvalues">
       <argument name="data" xsi:type="array">
           <item name="options" xsi:type="object">Myvendor\Mymodule\Model\Source\Myvalues</item>
           <item name="config" xsi:type="array">
               <item name="dataType" xsi:type="string">text</item>
               <item name="label" translate="true" xsi:type="string">Values</item>
               <item name="formElement" xsi:type="string">multiselect</item>

           </item>
       </argument>
    </field> 
 </form>

model\source\Myvalues.php

 use Magento\Backend\Block\Template\Context;
    use Myvendor\Mymodule\Model\ResourceModel\Item\Collection;
    use Myvendor\Mymodule\Model\ResourceModel\Item\CollectionFactory;

    class Myvalues implements \Magento\Framework\Data\OptionSourceInterface
    {
        protected $_request;
        public function __construct(
             \Magento\Framework\App\RequestInterface $request,  
            Context $context,
            CollectionFactory $collectionFactory,
            array $data = []
        ){
            $this->collectionFactory = $collectionFactory;
            $this->_request = $request;
        }

        public function getMyvalues(){
            return $this->collectionFactory->create()->getItems();
        }

        public function toOptionArray(){
            $productId = $this->_request->getParam('id');
            $vals      =  $this->getMyvalues();
            $val_array = [];
            foreach($vals as $valrows){
                $val_array[] =["value" => $valrows->getId(),'label' => $valrows->getName()];
            }
            //print_r($data_arr);
            return $val_array;
        }

}
  • Can you please add your code in question – Rutvee Sojitra Aug 7 '18 at 8:10
  • @RutveeSojitra. I have edited my question with code. i'm Using observer to save the values in my custom table. and it is saved perfectly but while editing the same product the values which are stored in my custom table are not reflecting back in this multiselect box. – Girish Aug 8 '18 at 5:37

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.