Skip to main content

Magento 2 - Add admin grid page


In magento 2, we can add grid by mainly two types. First is to add it via the grid.php and other via xml file. Here we will go through, how to add it via grid file. You can find the sample code in the git repo.

Add the files as per the blog post. Now we need to convert the block file mentioned in the controller to grid format.

The block file will look as below.
namespace VendorName\ModuleName\Block\Adminhtml;

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

    protected function _construct() {
        $this->_controller = 'adminhtml_blockname';
        $this->_blockGroup = 'VendorName_ModuleName';
        $this->_headerText = __('Header Text');
        parent::_construct();
    }

}


Now, inside the Adminhtml folder, create a folder called Blockname and file Grid.php inside it. Create a model or use an existing model. Check the blog to create new model.
namespace VendorName\ModuleName\Block\Adminhtml\Blockname;

use \Magento\Backend\Block\Widget\Grid\Extended as ExtendGrid;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Helper\Data as BackendHelper;
use VendorName\ModuleName\Model\ResourceModel\Mymodel\CollectionFactory;

class Grid extends ExtendGrid {

    protected $_collectionFactory;

    public function __construct(Context $context, BackendHelper $backendHelper, CollectionFactory $collectionFactory, array $data = []) {
        $this->_collectionFactory = $collectionFactory;
        $this->_module = 'VendorName_ModuleName';
        parent::__construct($context, $backendHelper, $data);

        $this->setId('unique_name');
        $this->setUseAjax(false);
        $this->setDefaultSort('filename');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection() {
        $collection = $this->_collectionFactory->create();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns() {
        //same as the magento 1 code
        $this->addColumn(
            'attribute_code',
            [
                'header' => __('Attribute Code'),
                'sortable' => true,
                'index' => 'attribute_code',
            ]
        );

        return parent::_prepareColumns();
    }

    protected function _prepareMassaction() {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('update_ids');
        $this->getMassactionBlock()->setUseSelectAll(false);

        $this->getMassactionBlock()->addItem('update', [
            'label' => __('Update Selected'),
            'url' => $this->getUrl('*/*/updateMassaction'),
        ]);

        return $this;
    }

    public function getRowUrl($row) {
        return $this->getUrl($this->_module . '/*/edit', ['id' => $row->getId()]);
    }

}

Comments

Popular posts from this blog

Magento 2 - Create a simple module

In magento 2, the creation of module has changed completely. We will go through how to create a module. Like earlier magento, we do not need to put our module files in different folders for code, theme, skin, etc. All, the codes related to the module should be inside one main folder. You can find the sample code in the git repo .