Magento 2 – Custom Admin Grid Not Rendering

admingridmagento2magento2-dev-betaphp-5.4

I'm trying to display the grid(read only for view purpose, it won't allow edit or delete). My module is calling and heading also displaying in admin panel, but grid not displaying.

Even though I'm not receiving any errors also.

my code is:

Learning/Test/Controller/Adminhtml/Test/Index.php

<?php
namespace Learning\Test\Controller\Adminhtml\Test;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Backend\App\Action
{
    const ADMIN_RESOURCE = 'Learning_Test::test';

    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Index action
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
    $resultPage = $this->resultPageFactory->create();
    $resultPage->setActiveMenu('Learning_Test::test');
    $resultPage->addBreadcrumb(__('Test'), __('Test'));
    $resultPage->addBreadcrumb(__('Manage Test'), __('Manage Test'));
    $resultPage->getConfig()->getTitle()->prepend(__('Test'));
    $resultPage->addContent(
        $resultPage->getLayout()->createBlock('Learning\Test\Block\Adminhtml\Grid')
    );
    return $resultPage;
    }
}

Learning/Test/Block/Adminhtml/Grid.php

<?php
namespace Learning\Test\Block\Adminhtml;


class Grid extends \Magento\Backend\Block\Widget\Container
{

    /**
 * @return void
 */
protected function _construct()
{
    $this->_controller = 'adminhtml_grid';
    $this->_blockGroup = 'Learning_Test';
    $this->_headerText = __('Custom Grid');
    parent::_construct();
    $this->removeButton('add');
}

}

Learning/Test/Block/Adminhtml/Grid/Grid.php

<?php
namespace Learning\Test\Block\Adminhtml\Grid;

use Magento\Store\Model\Store;

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{

    /**
 * @var \Learning\Test\Model\ResourceModel\Info\CollectionFactory
 */
protected $_collectionFactory;

/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Backend\Helper\Data $backendHelper
 * @param \Learning\Test\Model\ResourceModel\Info\CollectionFactory
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Helper\Data $backendHelper,
    \Learning\Test\Model\ResourceModel\Info\CollectionFactory $collectionFactory,
    array $data = []
) {
    $this->_collectionFactory = $collectionFactory;
    parent::__construct($context, $backendHelper, $data);
}

/**
 * @return void
 */
protected function _construct()
{
    parent::_construct();
    $this->setId('learningGrid');
    $this->setDefaultSort('id');
    $this->setDefaultDir('ASC');
}

/**
 * Prepare grid collection object
 *
 * @return $this
 */
protected function _prepareCollection()
{
    $collection = $this->_collectionFactory->create();
    $this->setCollection($collection);

    return parent::_prepareCollection();
}

/**
 * Prepare default grid column
 *
 * @return $this
 */
protected function _prepareColumns()
{
    parent::_prepareColumns();

    $this->addColumn(
        'id',
        [
            'header' => __('ID'),
            'type' => 'number',
            'index' => 'id',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id'
        ]
    );

    return $this;
}
}

Learning/Test/view/adminhtml/layout/test_test_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <block class="Learning\Test\Block\Adminhtml\Grid"
                   name="test_grid_listing" />
        </referenceContainer>
    </body>
</page>

see below screen shot also.

enter image description here

Could you please resolve my issue? where I went wrong?

Best Answer

Your controller execute look likes

/**
     * Index action
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Learning_Test::test');
        $resultPage->addBreadcrumb(__('Test'), __('Test'));
        $resultPage->addBreadcrumb(__('Manage Test'), __('Manage Test'));
        $resultPage->getConfig()->getTitle()->prepend(__('Test'));
        $resultPage->addContent(
            $resultPage->getLayout()->createBlock('Learning\Test\Block\Adminhtml\Grid')
        );
        return $resultPage;
    }
app/code/Learning/Test/Block/Adminhtml/Grid.php

looks like

namespace Learning\Test\Block\Adminhtml;

class Grid  extends \Magento\Backend\Block\Widget\Grid\Container
{
    /**
     * @return void
     */
    protected function _construct()
    {
        $this->_controller = 'adminhtml_grid';
        $this->_blockGroup = 'Learning_Test';
        $this->_headerText = __('Custom Grid');
        parent::_construct();
        $this->removeButton('add');
    }
}
app/code/Learning/Test/Block/Adminhtml/Grid/Grid.php
namespace Learning\Test\Block\Adminhtml\Grid;

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
    /**
     * @var \Learning\Test\Model\ResourceModel\Info\CollectionFactory
     */
    protected $_collectionFactory;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Learning\Test\Model\ResourceModel\Info\CollectionFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Learning\Test\Model\ResourceModel\Info\CollectionFactory $collectionFactory,
        array $data = []
    ) {
        $this->_collectionFactory = $collectionFactory;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('learningGrid');
        $this->setDefaultSort('id');
        $this->setDefaultDir('ASC');
    }

    /**
     * Prepare grid collection object
     *
     * @return $this
     */
    protected function _prepareCollection()
    {
        $collection = $this->_collectionFactory->create();
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    /**
     * Prepare default grid column
     *
     * @return $this
     */
    protected function _prepareColumns()
    {
        parent::_prepareColumns();

        $this->addColumn(
            'id',
            [
                'header' => __('ID'),
                'type' => 'number',
                'index' => 'id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );

        return $this;
    }
}