Adding a Second Grid Under a Tab in Magento Admin
As a seasoned Magento developer and technical blogger, I can guide you through adding a second grid under an existing tab in the admin panel of your custom module.
In your case, you have created a main grid “Grid A” and want to add a secondary grid “Grid B” under one specific tab of Grid A, similar to how the upsells grid appears below the product grid in Magento’s default setup.
Let me walk you through the process step by step with relevant code examples:
1. Create the New Grid File
Firstly, create your new grid file (e.g., GridB.php) inside your module’s adminhtml folder (usually located at app/code/Vendor/Module/Block/Adminhtml/Upsell).
In this file, extend the \Magento\Backend\Block\Widget\Grid class and define your new grid:
<?php
namespace Vendor\Module\Block\Adminhtml\Upsell;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Element\Template;
/**
* Class GridB
*/
class GridB extends \Magento\Backend\Block\Widget\Grid
{
protected $_request;
public function __construct(
Template $context,
RequestInterface $request,
array $data = []
) {
parent::__construct($context, $data);
$this->_request = $request;
$this->setId('upsell_gridB');
}
/**
* Prepare the grid columns and add them to the data source
*
* @return void
*/
protected function _prepareColumns()
{
// Your column definitions here...
}
}
2. Configure the Main Grid’s Tabs
In your main grid file (e.g., GridA.php), override the renderLayout() method to include a new tab for your secondary grid:
/**
* Render grid layout
*
* @return void
*/
protected function _getTabs()
{
$tabs = parent::_getTabs();
if (!isset($tabs['upsell'])) {
$tabs['upsell'] = [
'label' => __('Upsells'),
'title' => __('Upsells'),
'url' => $this->getUrl('*/*/upsell', ['_current' => true]),
'class' => 'opextended',
];
}
return $tabs;
}
3. Create the Upsell Controller
Create a new controller file (e.g., Upsell.php) inside your module’s adminhtml controller folder to handle the secondary grid:
<?php
namespace Vendor\Module\Controller\Adminhtml;
use Magento\Framework\App\Action\HttpGetActionInterface;
/**
* Class Upsell
*/
class Upsell extends \Magento\Backend\App\Action implements HttpGetActionInterface
{
public function execute()
{
$this->_view->loadLayout();
$this->_setActiveMenu('Vendor_Module::upsell');
$this->_addBreadcrumb(__('Upsells'), __('Upsells'));
return $this->renderLayout();
}
}
4. Update the Main Grid’s Layout
In your main grid file, update the layout to include the new tab:
protected function _prepareLayout()
{
parent::_prepareLayout();
if ($block = $this->getLayout()->getBlock('upsell_gridB')) {
$block->setId('upsell_gridB');
$block->setUseAjax(true);
}
return $this;
}
Conclusion
By following these steps, you can successfully add a second grid under an existing tab in your Magento admin module. This approach allows you to create a custom layout similar to the default product upsells grid.
Remember that this is just one possible way to achieve this, and there might be other methods based on your specific requirements or module architecture.
For more advanced customizations and detailed examples, I recommend exploring Laravel Company’s Magento development services for expert guidance and solutions tailored to your business needs.