0

I'm using toggle button in my admin form and saved successfully but when I am editing admin grid then toggle button status not changed according to status (true or false)

enter image description here

enter image description here

Update: I have write the code for toggle button this given path : [XTeam/Slider/view/adminhtml/ui_component/ui_form.xml].

<field name="status" formElement="checkbox">
      <settings>
            <label translate="true">Enabled</label>
            <tooltip>
                  <description translate="true">Status for Showing Imgee in Slider.</description>
            </tooltip>
      </settings>
      <formElements>
            <checkbox>
                  <settings>
                        <prefer>toggle</prefer>
                        <valueMap>
                            <map name="false" xsi:type="boolean">false</map>
                            <map name="true" xsi:type="boolean">true</map>
                        </valueMap>
                   </settings>
            </checkbox>
      </formElements>
</field>

And this my controller where save and update the form. path: [XTeam/Slider/Controller/Adminhtml/Index/Submit.php]

<?php

namespace XTeam\Slider\Controller\Adminhtml\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;

class Submit extends \Magento\Framework\App\Action\Action
{
    protected $_dataExample;
    public $resourcemodel;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \XTeam\Slider\Model\Adminhtml\DataExampleFactory  $dataExample,
        \XTeam\Slider\Model\Adminhtml\ResourceModel\DataExample $resourcemodel
    ) {
        parent::__construct($context);
        $this->_dataExample = $dataExample;
        $this->resourcemodel = $resourcemodel;
    }
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        // print_r($post);
        $id = $this->getRequest()->getParam("id");
        if ($id) {
            $id = $post['id'];
            $model = $this->_dataExample->create();
            $this->resourcemodel->load($model, $id, 'id');
            $ImageUrl = $post['image'][0]['url'] ?? '';
            $ImagePath = '';
            if ($ImageUrl != '') {
                $ImagePath = explode('media/', $ImageUrl);
                $ImagePath = $ImagePath[1];
            }
            $model->setData('image', $ImagePath);
            $model->setData('title', $post['title']);
            $model->setData('label', $post['label']);
            $model->setData('status', $post['status']);
            $saveData = $this->resourcemodel->save($model);
            if ($saveData) {
                $this->messageManager->addSuccess(__('Insert Record Successfully !'));
            }
            return $this->resultRedirectFactory->create()->setPath('adminslider/index/index');
        } else {
            $model = $this->_dataExample->create();
            $ImageUrl = $post['image'][0]['url'];
            $ImagePath = '';
            if ($ImageUrl != '') {
                $ImagePath = explode('media/', $ImageUrl);
                $ImagePath = $ImagePath[1];
            }
            $model->setTitle($post['title'])
                ->setLabel($post['label'])
                ->setStatus($post['status'])
                ->setImage($ImagePath);
            $saveData = $model->save();
            if ($saveData) {
                $this->messageManager->addSuccess(__('Insert Record Successfully !'));
            }
            return $this->resultRedirectFactory->create()->setPath('adminslider/index/index');
        }
    }
}
2
  • hello dear, can you share your code.? Commented Jan 15 at 7:35
  • hello @mohitvamja , please review this code and give me solution.......
    – raghav
    Commented Jan 16 at 6:43

2 Answers 2

1

hello dear I am implementing a grid module using the ui component. works for me. so please check the below code.

/var/www/html/project/app/code/Vendor/Module/view/adminhtml/ui_component/vernor_module_blog_listing.xml

<column name="status">
     <argument name="data" xsi:type="array">
        <item name="options" xsi:type="array">
           <item name="0" xsi:type="array">
              <item name="label" xsi:type="string">Active</item>
              <item name="value" xsi:type="string">1</item>
           </item>
           <item name="1" xsi:type="array">
              <item name="label" xsi:type="string">Inactive</item>
              <item name="value" xsi:type="string">0</item>
           </item>
        </item>
        <item name="config" xsi:type="array">
           <item name="editor" xsi:type="string">select</item>
           <item name="filter" xsi:type="string">select</item>
           <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
           <item name="dataType" xsi:type="string">select</item>
           <item name="label" xsi:type="string" translate="true">Status</item>
        </item>
     </argument>
  </column>

/var/www/html/project/app/code/Vendor/Module/view/adminhtml/ui_component/module_form.xml

<field name="status">
     <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
           <item name="sortOrder" xsi:type="number">10</item>
           <item name="dataType" xsi:type="string">boolean</item>
           <item name="formElement" xsi:type="string">checkbox</item>
           <item name="prefer" xsi:type="string">toggle</item>
           <item name="label" xsi:type="string" translate="true">Status</item>
           <item name="valueMap" xsi:type="array">
              <item name="true" xsi:type="number">1</item>
              <item name="false" xsi:type="number">0</item>
           </item>
           <item name="valuesForOptions" xsi:type="array">
              <item name="boolean" xsi:type="string">boolean</item>
           </item>
           <item name="default" xsi:type="number">1</item>
           <item name="dataScope" xsi:type="string">status</item>
        </item>
     </argument>
  </field>

i will share below screen shot.

enter image description hereenter image description here

enter image description here

enter image description here

please check code try work you can more information see below link

https://www.rohanhapani.com/magento-2-create-ui-component-grid-and-form/

3
  • thank you brother @mohitvamja. its working for me........
    – raghav
    Commented Jan 18 at 6:31
  • thank brother most welcome, Do you have any issues at any time? please tell me and I will help with you. Commented Jan 18 at 6:38
  • hello brother @mohitvamja I have one more query , please review this link given below............ magento.stackexchange.com/questions/371718/…
    – raghav
    Commented Jan 19 at 11:05
0

Could you please check the Model/DataProvider file - getData() function. Under this you should define the key of your staus attribute.

ex:-

    public function getData()
    {
        $generalInformationKeys = ['id', 'name', 'address', 'status', 'position', 'phone', 'store_id' ];

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.