#StackBounty: #magento2 #grid how i set the pagination in the admin in magento2 in custom module?

Bounty: 50

pagination is not working in custom module in admin in magento 2enter image description here

please tell me how it work in magento 2

<?php
namespace VendorModuleBlockAdminhtmlColor;

/**
 * Adminhtml news pages grid
 */
class Grid extends MagentoBackendBlockWidgetGridExtended
{
    /**
     * @var MagentoFrameworkModuleManager
     */
    protected $moduleManager;

    /**
     * @var MagentoEavModelResourceModelEntityAttributeSetCollectionFactory]
     */
    protected $_setsFactory;

    /**
     * @var MagentoCatalogModelProductFactory
     */
    protected $_productFactory;

    /**
     * @var MagentoCatalogModelProductType
     */
    protected $_type;

    /**
     * @var MagentoCatalogModelProductAttributeSourceStatus
     */
    protected $_status;
    protected $_collectionFactory;

    /**
     * @var MagentoCatalogModelProductVisibility
     */
    protected $_visibility;

    /**
     * @var MagentoStoreModelWebsiteFactory
     */
    protected $_websiteFactory;

    /**
     * @param MagentoBackendBlockTemplateContext $context
     * @param MagentoBackendHelperData $backendHelper
     * @param MagentoStoreModelWebsiteFactory $websiteFactory
     * @param MagentoEavModelResourceModelEntityAttributeSetCollectionFactory $setsFactory
     * @param MagentoCatalogModelProductFactory $productFactory
     * @param MagentoCatalogModelProductType $type
     * @param MagentoCatalogModelProductAttributeSourceStatus $status
     * @param MagentoCatalogModelProductVisibility $visibility
     * @param MagentoFrameworkModuleManager $moduleManager
     * @param array $data
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoBackendHelperData $backendHelper,
        MagentoStoreModelWebsiteFactory $websiteFactory,
        OcodewireDesigntoolModelResourceColorCollection $collectionFactory,
        MagentoFrameworkModuleManager $moduleManager,
        array $data = []
    ) {

        $this->_collectionFactory = $collectionFactory;
        $this->_websiteFactory = $websiteFactory;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();

        $this->setId('productGrid');
        $this->setDefaultSort('id');
         $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);

    }

    /**
     * @return Store
     */
    protected function _getStore()
    {
        $storeId = (int)$this->getRequest()->getParam('store', 0);
        return $this->_storeManager->getStore($storeId);
    }

    /**
     * @return $this
     */
    protected function _prepareCollection()
    {
        try{


            $collection =$this->_collectionFactory->load();



            $this->setCollection($collection);

            parent::_prepareCollection();

            return $this;
        }
        catch(Exception $e)
        {
            echo $e->getMessage();die;
        }
    }

    /**
     * @param MagentoBackendBlockWidgetGridColumn $column
     * @return $this
     */
    protected function _addColumnFilterToCollection($column)
    {
        if ($this->getCollection()) {
            if ($column->getId() == 'websites') {
                $this->getCollection()->joinField(
                    'websites',
                    'catalog_product_website',
                    'website_id',
                    'product_id=entity_id',
                    null,
                    'left'
                );
            }
        }
        return parent::_addColumnFilterToCollection($column);
    }

    /**
     * @return $this
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function _prepareColumns()
    {
        // echo "dffdsfsd"; die;
        $this->addColumn(
            'color_id',
            [
                'header' => __('Color Id'),
                'type' => 'number',
                'index' => 'color_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );


        $this->addColumn(
            'color_name',
            [
                'header' => __('Color Name'),
                'type' => 'text',
                'index' => 'color_name',
            ]
        );

        $this->addColumn(
            'color_code',
            [
                'header' => __('Color Code'),
                'index' => 'color_code',
                'class' => 'color code',  
            ]
        );


        $this->addColumn(
            'color_code',
            [
                'header' => __('Color Code'),
                'index' => 'color_code',
                'class' => 'color code',  
            ]
        );









         $this->addColumn(
            'Edit',
            [
                'header' => __('Edit'),
                'type' => 'action',
                'getter' => 'getId',
                'actions' => [
                    [
                        'caption' => __('Edit'),
                        'url' => [
                            'base' => '*/*/edit',
                            'params' => ['store' => $this->getRequest()->getParam('store')]
                        ],
                        'field' => 'id'
                    ]
                ],
                'filter' => false,
                'sortable' => false,
                'index' => 'stores',
                'header_css_class' => 'col-action',
                'column_css_class' => 'col-action'
            ]
        );


        /*{{CedAddGridColumn}}*/
        $block = $this->getLayout()->getBlock('grid.bottom.links');
        if ($block) {
            $this->setChild('grid.bottom.links', $block);
        }

        return parent::_prepareColumns();
    }

     /**
     * @return $this
     */
    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('id');
        $this->getMassactionBlock()->setFormFieldName('id');

        $this->getMassactionBlock()->addItem(
            'delete',
            array(
                'label' => __('Delete'),
                'url' => $this->getUrl('*/*/massDelete'),
                'confirm' => __('Are you sure?')
            )
        );
        return $this;
    }

    /**
     * @return string
     */


    /**
     * @param MagentoCatalogModelProduct|MagentoFrameworkObject $row
     * @return string
     */
    public function getRowUrl($row)
    {
        return $this->getUrl(
            '*/*/edit',
            ['store' => $this->getRequest()->getParam('store'), 'id' => $row->getId()]
        );
    }


     public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', ['_current' => true]);
    }
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.