The Magento 2 UI grids are used to manage the store data in an easy-to-handle format.
In default Magento 2, the backend grids allow the admin to sort, filter, and edit the data. The admin can also use pagination and mass action to update the data in bulk.
However, there is a drawback in the design of the grids that compels the admin to open each row and update the data in it. It is a tedious and time-consuming task.
The solution to this issue is to add inline edit in UI grid in Magento 2 backend as shown below:
After implementing it using the below solution, the admin need not open each entry in the edit mode but simply make the necessary changes using the inline edit as shown in the above image.
You can use this solution in a custom admin grid as well as in Magento 2 default grids.
Steps to Add Inline Edit in UI Grid in Magento 2 Backend
- Use the below code in your xml file of ui_component or in the grid where you want to add inline edit.
This file will be located under Vendor/Extension/view/adminhtml/ui_component/ .123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657<columns name="data_records_columns"><settings><editorConfig><param name="clientConfig" xsi:type="array"><item name="saveUrl" xsi:type="url" path="routes/action/inlineedit"/><item name="validateBeforeSave" xsi:type="boolean">false</item></param><param name="indexField" xsi:type="string">entity_id</param><param name="enabled" xsi:type="boolean">true</param><param name="selectProvider" xsi:type="string">data_list.data_list.data_records_columns.ids</param></editorConfig><childDefaults><param name="fieldAction" xsi:type="array"><item name="provider" xsi:type="string">data_list.data_list.data_records_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><selectionsColumn name="ids"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="indexField" xsi:type="string">entity_id</item><item name="sortOrder" xsi:type="number">0</item></item></argument></selectionsColumn>//name of a column on which you want to add inline edit<column name="name"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item><item name="editor" xsi:type="array"><item name="editorType" xsi:type="string">text</item><item name="validation" xsi:type="array"><item name="name" xsi:type="boolean">true</item></item></item><item name="label" xsi:type="string" translate="true">Name</item><item name="dataType" xsi:type="string">text</item><item name="sortOrder" xsi:type="number">50</item><item name="resizeEnabled" xsi:type="boolean">false</item><item name="resizeDefaultWidth" xsi:type="string">60</item><item name="sortable" xsi:type="boolean">false</item></item></argument></column></columns> - Create the InlineEdit.php file to get data and save it in the database at Vendor/Extension/Controller/Adminhtml/Action/.Done!123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354<?phpnamespace Vendor\Extension\Controller\Adminhtml\Action;use Magento\Backend\App\Action;use Magento\Backend\App\Action\Context;use Magento\Framework\Controller\Result\JsonFactory;use Vendore\Extension\Model\Model;class InlineEdit extends Action{protected $jsonFactory;protected $model;public function __construct(Context $context,JsonFactory $jsonFactory,Model $model){parent::__construct($context);$this->jsonFactory = $jsonFactory;$this->model = $model;}public function execute(){$resultJson = $this->jsonFactory->create();$error = false;$messages = [];if ($this->getRequest()->getParam('isAjax')) {$postItems = $this->getRequest()->getParam('items', []);if (empty($postItems)) {$messages[] = __('Please correct the data sent.');$error = true;} else {foreach (array_keys($postItems) as $entityId) {$modelData = $this->model->load($entityId);try {$modelData->setData(array_merge($modelData->getData(), $postItems[$entityId]));$modelData->save();} catch (\Exception $e) {$messages[] = "[Error:] {$e->getMessage()}";$error = true;}}}}return $resultJson->setData(['messages' => $messages,'error' => $error]);}
Do you find it challenging to edit your inline grid? Just mention your difficulty in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Get Weekly Updates
Never miss Magento tips, tricks, tutorials, and news.
Thank you for subscribing.
Something went wrong.