0

How to create Inline Edit in grid? I need controller file or only do this in view ui_component?

I created this in ui_component but not working

<item name="childDefaults" xsi:type="array">
<item name="fieldAction" xsi:type="array">
    <item name="provider" xsi:type="string">test_mytesting_index.test_mytesting_index.test_mytesting_columns_editor</item>
    <item name="target" xsi:type="string">startEdit</item>
    <item name="params" xsi:type="array">
        <item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
        <item name="1" xsi:type="boolean">true</item>
    </item>
</item>

  • 1
    You need a controller to save the values you change in inline-grid. – aravind Jul 29 '19 at 7:34
  • But I can not change anything in my grid – Tomalo Jul 29 '19 at 7:36
0

You can take the reference from CMS module of core Magneto. This is the reference link for that https://github.com/magento/magento2/blob/2.1/app/code/Magento/Cms/view/adminhtml/ui_component/cms_block_listing.xml

Like showing in this file you need to add editorConfig to configure inline edit and you also need to create controller.

<item name="editorConfig" xsi:type="array">
    <item name="selectProvider" xsi:type="string">cms_block_listing.cms_block_listing.cms_block_columns.ids</item>
    <item name="enabled" xsi:type="boolean">true</item>
    <item name="indexField" xsi:type="string">block_id</item>
    <item name="clientConfig" xsi:type="array">
        <item name="saveUrl" xsi:type="url" path="cms/block/inlineEdit"/>
        <item name="validateBeforeSave" xsi:type="boolean">false</item>
    </item>
</item>
| improve this answer | |
0

In your Ui_component.xml place this code make inline edit work.

<settings>           
        <editorConfig>
            <param name="clientConfig" xsi:type="array">
                <item name="saveUrl" xsi:type="url" path="route/controllerfolder/controller"/>
                <item name="validateBeforeSave" xsi:type="boolean">false</item>
            </param>
            <param name="indexField" xsi:type="string">primary_field</param>
            <param name="enabled" xsi:type="boolean">true</param>
            <param name="selectProvider" xsi:type="string">test_mytesting_index.test_mytesting_index.test_mytesting_columns.ids</param>
        </editorConfig>
        <childDefaults>
            <param name="fieldAction" xsi:type="array">
                <item name="provider" xsi:type="string">test_mytesting_index.test_mytesting_index.test_mytesting_columns_editor</item>
                    <item name="target" xsi:type="string">startEdit</item>
                    <item name="params" xsi:type="array">
                    <item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
                    <item name="1" xsi:type="boolean">true</item>
                </item>
            </param>
        </childDefaults>
    </settings>

To show a column in Inline-edit, add below code

 <column name="your_field">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">text</item>
            <item name="sorting" xsi:type="string">asc</item>
            <item name="label" xsi:type="string" translate="true">Your Field Label</item>
            <item name="editor" xsi:type="array">
                <item name="editorType" xsi:type="string">text</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
            </item>
        </item>
    </argument>
</column>

Next you need to create a controller,route/controllerfolder/controller.

Vendor/Module/Controller/Area(frontend(or)Adminhtml)/controllerfolder/controller

<?php
namespace Vendor\Module\Controller\Adminhtml\controllerfolder;

class controller extends \Magento\Backend\App\Action
{

protected $jsonFactory;

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
) {
    parent::__construct($context);
    $this->jsonFactory = $jsonFactory;
}

public function execute()
{
    /** @var \Magento\Framework\Controller\Result\Json $resultJson */
    $resultJson = $this->jsonFactory->create();
    $error = false;
    $messages = [];

    if ($this->getRequest()->getParam('isAjax')) {
        $postItems = $this->getRequest()->getParam('items', []);
        if (!count($postItems)) {
            $messages[] = __('Please correct the data sent.');
            $error = true;
        } else {
            foreach (array_keys($postItems) as $entityId) {
                /** load your model to update the data */
                $model = $this->_objectManager->create('Vendor\Module\Model\YourModel')->load($entityId);
                try {
                    $model->setData(array_merge($model->getData(), $postItems[$entityId]));
                    $model->save();
                } catch (\Exception $e) {
                    $messages[] = "[Error:]  {$e->getMessage()}";
                    $error = true;
                }
            }
        }
    }

    return $resultJson->setData([
        'messages' => $messages,
        'error' => $error
    ]);
  }

}

I hope this helps!!

| improve this answer | |

Your Answer

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

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