0

I need to create grid(inside the tab) without using ui component. Please provide me a solution. this is a controller I have created to load grid but it is failed to load

public function execute()
    {

        $resultRaw = $this->resultRawFactory->create();
        return $resultRaw->setContents(
                        $this->layoutFactory->create()->createBlock(
                                'X\Y\Block\Adminhtml\FormContainer\Edit\Tab\GridContainer\Grid', 'grid.view.grid'
                        )->toHtml()
        );
    }
1

1 Answer 1

1

Create Admin Grid using Layout

Create block for this grid File: app/code/Company/Module/Block/Adminhtml/Post.php

<?php
namespace Company/Module\Block\Adminhtml;

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

protected function _construct()
    {
        $this->_controller = 'adminhtml_post';
        $this->_blockGroup = 'Company/Module';
        $this->_headerText = __('Posts');
        $this->_addButtonLabel = __('Create New Post');
        parent::_construct();
    }

}

The Grid block will extend \Magento\Backend\Block\Widget\Grid\Container and define some variable in the _construct() method.

Create layout file

File: app/code/Company/Module/view/adminhtml/layout/company_module_post_index.xml

    <?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="styles"/>
        <body>
            <referenceContainer name="content">
                <block class="Company/Module\Block\Adminhtml\Post" name="company_post_grid">
                    <block class="Magento\Backend\Block\Widget\Grid" name="company_post_grid.grid" as="grid">
                        <arguments>
                            <argument name="id" xsi:type="string">post_id</argument>
                            <argument name="dataSource" xsi:type="object">Company/Module\Model\ResourceModel\Post\Collection</argument>
                            <argument name="default_sort" xsi:type="string">id</argument>
                            <argument name="default_dir" xsi:type="string">ASC</argument>
                            <argument name="save_parameters_in_session" xsi:type="string">1</argument>
                        </arguments>
                        <block class="Magento\Backend\Block\Widget\Grid\ColumnSet" name="company_post_grid.grid.columnSet" as="grid.columnSet">
                            <arguments>
                                <argument name="rowUrl" xsi:type="array">
                                    <item name="path" xsi:type="string">*/*/edit</item>
                                </argument>
                            </arguments>
                            <block class="Magento\Backend\Block\Widget\Grid\Column" as="post_id">
                                <arguments>
                                    <argument name="header" xsi:type="string" translate="true">ID</argument>
                                    <argument name="index" xsi:type="string">post_id</argument>
                                    <argument name="type" xsi:type="string">text</argument>
                                    <argument name="column_css_class" xsi:type="string">col-id</argument>
                                    <argument name="header_css_class" xsi:type="string">col-id</argument>
                                </arguments>
                            </block>
                            <block class="Magento\Backend\Block\Widget\Grid\Column" as="name">
                                <arguments>
                                    <argument name="header" xsi:type="string" translate="true">Name</argument>
                                    <argument name="index" xsi:type="string">name</argument>
                                    <argument name="type" xsi:type="string">text</argument>
                                    <argument name="column_css_class" xsi:type="string">col-id</argument>
                                    <argument name="header_css_class" xsi:type="string">col-id</argument>
                                </arguments>
                            </block>
                            <block class="Magento\Backend\Block\Widget\Grid\Column" as="created_at">
                                <arguments>
                                    <argument name="header" xsi:type="string" translate="true">Created</argument>
                                    <argument name="index" xsi:type="string">created_at</argument>
                                    <argument name="type" xsi:type="string">date</argument>
                                    <argument name="column_css_class" xsi:type="string">col-id</argument>
                                    <argument name="header_css_class" xsi:type="string">col-id</argument>
                                </arguments>
                            </block>
                            <block class="Magento\Backend\Block\Widget\Grid\Column" as="updated_at">
                                <arguments>
                                    <argument name="header" xsi:type="string" translate="true">Modified</argument>
                                    <argument name="index" xsi:type="string">updated_at</argument>
                                    <argument name="type" xsi:type="string">date</argument>
                                    <argument name="column_css_class" xsi:type="string">col-id</argument>
                                    <argument name="header_css_class" xsi:type="string">col-id</argument>
                                </arguments>
                            </block>
                        </block>
                    </block>
                </block>
            </referenceContainer>
        </body>

</page>

In this layout file, we will define some argument for the grid. The main argument is the dataSource. This argument will link with the dataSource which we declare in the di.xml file above to connect to the database and get data for this grid.

Add Column The Column set will define the columns which will be display in the grid. If you want to use massAction, you can add this block to the grid element:

<block class="Magento\Backend\Block\Widget\Grid\Massaction" name="company.module.massaction" as="grid.massaction">
    <arguments>
        <argument name="massaction_id_field" xsi:type="string">post_id</argument>
        <argument name="form_field_name" xsi:type="string">ids</argument>
        <argument name="use_select_all" xsi:type="string">1</argument>
        <argument name="options" xsi:type="array">
            <item name="disable" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Delete</item>
                <item name="url" xsi:type="string">*/*/massDelete</item>
            </item>
        </argument>
    </arguments>
</block>

After this, please refresh the cache and go to grid page to see the result.

Ref : https://www.mageplaza.com/magento-2-module-development/create-admin-grid-magento-2.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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