1

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?

4

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;
    }
}
  • No luck. Still not getting expected results. eventhough I have created layout also, but no results. see my code also updated. If I miss anything plz.. let me know? – Bojjaiah Dec 11 '15 at 9:20
  • 2
    Then need to investigate your module. – Sohel Rana Dec 11 '15 at 10:23
  • @SohelRana Could you help me with this stackoverflow.com/q/43344902/1616003 – Mukesh Apr 12 '17 at 6:45
  • @magentotwo How did you fix this issue could you mention in answer. – Mukesh Apr 12 '17 at 6:59
  • @Muk plz fallow the above answer. – Bojjaiah Apr 12 '17 at 9:59
1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.