Magento grid is a table which is responsible for listing database items to display in a managed way. This grid avails the feature to sorting, filtering and updating data items as per the requirements of admin users. There are many default grids available in Magento 2 like product grid, order grid, review grid etc. But sometimes you require to create a custom grid to manage and show database items of your custom module. This requires to custom code to create an admin grid in Magento 2.
There are 2 ways to create admin grid in Magento 2: using layout and using component. Here I will show you both the methods to create grid and use it in your module.
Create Admin Grid using Component
- First of all, you have to create xml file according to your action given in Admin Menu. For example, here I have set action like extension/demo/grid.Create file here: app/code/Vendor/Extension/view/adminhtml/layout/extension_demo_grid.xml
1<!--?xml version="1.0"?--> - Now you need to create component file declared in above step.
Put the code from extension_demo_grid_listing.xml file in app/code/Vendor/Extension/view/adminhtml/ui_component/extension_demo_grid_listing.xmlNow we have to create di.xml for dataprovider to ui component: app/code/Vendor/Extension/etc/di.xml
12345678910111213<!--?xml version="1.0"?-->mc_grid_dataVendor\Extension \Model\ResourceModel\ExtensionVendor\Extension\Model\ResourceModel\Extension\Grid\Collection
Create Admin Grid using Layout
- First step is to create Controller Action File: app/code/Vendor/Extension/Controller/Adminhtml/Demo/Grid.php
12345678910111213<!--?php namespace Vendor\ Extension\Block\Adminhtml; class Demo extends \Magento\Backend\Block\Widget\Grid\Container { protected function _construct() { $this->_controller = 'adminhtml_demo'; //_controller is the path to the Grid block inside the Block folder. In this Extension, I have put Demo.php file inside of the Adminhtml/Blog folder$this->_blockGroup = 'Vendor_Extension'; //_blockGroup is the name of our module with format VendorName_ExtensionName$this->_headerText = __('Records'); //_headerText is the title of Grid page$this->_addButtonLabel = __('Create New Records'); //_addButtonLabel is the label of Add New button.parent::_construct();}}</pre><p>The Grid block will extend \Magento\Backend\Block\Widget\Grid\Container and define some variable in the _construct() method.</p></li><li>Create Layout File:<br ?-->You will need a layout file to connect with Grid Block and render the grid.Create <strong><a href="https://magecomp.com/blog/wp-content/uploads/2017/06/extension_demo_grid.xml" target="_blank" rel="noopener noreferrer"><span style="color: blue;">extension_demo_grid.xml</span></a></strong> file at app/code/Vendor/Extension/view/adminhtml/layout/extension_demo_grid.xml
Note: You need to create model and ResourceModel for your extension prior to implementing this. Also
you need to add code in xml to add other fields.
Creating admin grid in Magento 2 is not rocket science but it needs proper and streamlined coding. After implementing above code, your admin grid will get ready. Let me know if you have any query, I’ll be glad to help you. Your feedback and suggestions are awaited through commenting. Till then, Happy Coding.
Hi,
How to join two table in grid ,I was search and tried to implement but getting no data in grid and also I think main_table alise not getting can you explain it step by step, also try with mass deletion
Kindly confirm you replace the proper value with the table name and model class.
Hi,
This involves creating a table. If I don’t need to use a table, how can I declare source to use to populate the table? I mean: part of my module consists of adding a custom attribute to a category. and I want to build an admin grid that lists only the categories which don’t have the custom attribute filled in. In this case, the module doesn’t need a custom table, it simply fetches the category collection and filter the collection to get only the categories with the custom attribute empty. I have already created a Controller and a Block to fetch the data but how can I pass those data to the grid?
Try using Admin grid using layout method.
Hi,
This involves creating a table. If I don’t need to use a table, how can I declare source to use to populate the table? I mean: part of my module consists of adding a custom attribute to a category. and I want to build an admin grid that lists only the categories which don’t have the custom attribute filled in. In this case, the module doesn’t need a custom table, it simply fetches the category collection and filter the collection to get only the categories with the custom attribute empty. I have already created a Controller and a Block to fetch the data but how can I pass those data to the grid?
Try using Admin grid using layout method.
Where VendorExtensionGridDataProvider goes from? I didn’t find the declaration.
“item name=”vendor_extension_grid_listing_data_source” xsi:type=”string”
Vendor\Extension\Model\ResourceModel\Extension\Grid\Collection ”
this line in di.xml means declaration of datasource
Where VendorExtensionGridDataProvider goes from? I didn’t find the declaration.
“item name=”vendor_extension_grid_listing_data_source” xsi:type=”string”
VendorExtensionModelResourceModelExtensionGridCollection ”
this line in di.xml means declaration of datasource
Hi,
what does the Line:
return $this->_authorization->isAllowed(‘Vendor_Extension::grid’);
exactly. What does the Argument Vendor_Extension::grid??
Please check, the code here is updated. This code is used to assign role of the grid.
Hi,
what does the Line:
return $this->_authorization->isAllowed(‘Vendor_Extension::grid’);
exactly. What does the Argument Vendor_Extension::grid??
Please check, the code here is updated. This code is used to assign role of the grid.