Question

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?

Was it helpful?

Solution

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;
    }
}

OTHER TIPS

In my case the Grid was not rendering because of the improper closed xml tag

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="adminhtml_concorcommon_advanced_syslog_grid/">  <!-- notice here, improper tag closing -->
    <body>
        <referenceContainer name="content">
            <block class="Concor\Common\Block\Adminhtml\Advanced\Syslog\Grid" name="common.advanced.syslog"/>
        </referenceContainer>
    </body>
</page>

After properly closing the tag Grid rendered.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="adminhtml_concorcommon_advanced_syslog_grid"/>
    <body>
        <referenceContainer name="content">
            <block class="Concor\Common\Block\Adminhtml\Advanced\Syslog\Grid" name="common.advanced.syslog"/>
        </referenceContainer>
    </body>
</page>

I was also having an issue with this, having followed all the steps I couldn't see what was happening that would have be causing it.

It turns out that I had given the layout xml the wrong name, just double check it matches the class in the body tag on the page (substituting dashes for underscores of course).

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top