Magento 2 Admin Grid

Published On: 8 December 2016.By .
  • Digital Engineering
  • Ecommerce

Create Admin Grid In Magento2 Module :

Magento 2 Grid is a kind of table which listing the items in our database table and provide some features like: sort, filter, delete, update item, etc. There are some steps to create a grid of moule in magento 2 admin.

ModuleLocation : app/code/Vendor/Module.

Step 1: Define Router Id:

Router is PHP class which is responsible for matching and processing URL request.

By default, there are some routers in Magento like; Base, DefaultRouter, Cms and UrlRewrite. Front Controller will go through all routers in routersList, so we need to add custom router in lib/internal/Magento/Framework/App/RouterList.php by adding our configuration for new router in di.xml module.

To create a Admin grid we have to define the route and frontend name of grid in route.xml.

File Location : ModuleLocation/etc/adminhtml/route.xml.

  • router id : Define ‘admin’ (in case of frontend otherwise standard).
  • frontendName : Name use after controller.

Step 2. To Create Admin Menu in Magento 2 :

I. Create menu.xml at loaction ModuleLocation/etc/adminhtml/menu.xml


II . Add Menu Item :

III Add Sub Menu :

  • id – It’s a unique string and should follow the format: {Vendor_Module}::{menu_description}.
  • title – attribute is the text which will be shown on the menu bar
  • module- attribute is defined the module which this menu is belong to..
  • sortOrder- attribute is defined the position of the menu. Lower value will display on top of menu
  • parent – attribute is an Id of other menu node. It will tell Magento that this menu is a child of another menu.
  • action – attribute will define the url of the page which this menu link to.
  • resource – attribute is used to defined the ACL rule which the admin user must have in order to see and access this menu

Setp 3. Create a Custom Table :

Schema setup scripts change database schema, they create or update database tables. If module is installing, Setup\InstallSchema::install() is executed.

To create custum table create a file installSchema.php.

FileLocation : ModuleLocation/Setup/InstallSchema.php

  • table_name : Set name of table to be create.
  • table_id : id for primary key.

To add more columns in tables use addColumn(‘column_id’, \Magento\Framework\DB\Ddl\Table::TYPE, length, ‘Name of column’).

To upgrade database use command php bin\magento setup:upgrade.

Step 4. Create a Model & Resource Model & Collection File

Create a model file in which resource model is defined. This file is use to connect with database.

FileLocation : ModuleLocation/Model/Model_File.php

A resource model is a model that is responsible for create, read, update, delete(CRUD) implementations in magento. Resource model are used to process data from model to database, model holds the data and resource model is used for some operation.

FileLocation : ModuleLocation/Model/ResourceModel/.php

  • table_name : name of custum table.
  • table_id : primary key of table.

Collection Model is used to filter and fetch collection (data from table).

FileLocation : ModuleLocation\Model\ResourceModel\/Collection.php

Step 5. Create a Abstarct Class in Controller :

FileLocation : ModuleLocation\Controller\Adminhtml\ Module_name.php

FileLocation : ModuleLocation/Controller/Adminhtml//Index.php

Step 6: Create block for grid.
In this we define the title of grid page and controller.

File Location : ModuleLocation\Block\Adminhtml\Block_Name\Grid.php

Step 7. Create a layout file for admin controller :

This file define the layout of admin grid.
File Location : ModuleLocation/view/adminhtml/layout/__.xml

Step 8. Create the file: .xml in folder:

In this step we create a grid in admin and define their function

File Location : ModuleLocation/view/adminhtml/layout/_grid.xml

Related content

That’s all for this blog

Go to Top