Skip to content

How to add Fitter in custom grid using only block file in magneto 2

<?php

namespace MavenbirdCustomerRegisterBlockAdminhtmlGrid;
class Grid extends MagentoBackendBlockWidgetGridExtended
{

    protected $_postFactory;

    /**
     * @var MagentoFrameworkModuleManager
     */
    protected $moduleManager;

    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoBackendHelperData $backendHelper,
        MavenbirdCustomerRegisterModelPostFactory $postFactory,
        MagentoFrameworkModuleManager $moduleManager,
        array $data = []
    ) {
        $this->_postFactory = $postFactory;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('gridGrid');
        $this->setDefaultSort('id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
        $this->setVarNameFilter('grid_record');

    }

    /**
     * @return $this
     */
    protected function _prepareCollection()
    {
        $collection = $this->_postFactory->create()->getCollection();
        $this->setCollection($collection);
        parent::_prepareCollection();
        
        return $this;
    }

    /**
     * @return $this
     */
    protected function _prepareColumns()
    {

        $this->addColumn(
            'id',
            [
                'header' => __('ID'),
                'type' => 'number',
                'index' => 'id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
            ]
        );

        $this->addColumn(
            'name',
            [
                'header' => __('Name'),
                'index' => 'name',
            ]
        );

        $this->addColumn(
            'password',
            [
                'header' => __('Password'),
                'index' => 'password',
            ]
        );

        $this->addColumn(
            'email',
            [
                'header' => __('Email'),
                'index' => 'email',
            ]
        );
        $this->addColumn(
            'phone',
            [
                'header' => __('PhoneNo'),
                'index' => 'phone',
            ]
        );
        $this->addColumn(
            'comment',
            [
                'header' => __('comment'),
                'index' => 'comment',
            ]
        );
        $this->addColumn(
            'homeno',
            [
                'header' => __('homeno'),
                'index' => 'homeno',
            ]
        );
        $this->addColumn(
            'apartment',
            [
                'header' => __('apartment'),
                'index' => 'apartment',
            ]
        );
        $this->addColumn(
            'city',
            [
                'header' => __('city'),
                'index' => 'city',
            ]
        );
        $this->addColumn(
            'state',
            [
                'header' => __('state'),
                'index' => 'state',
            ]
        );
        $this->addColumn(
            'contry',
            [
                'header' => __('contry'),
                'index' => 'contry'
            ]
        );

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

        $this->addColumn(
            'delete',
            [
                'header' => __('Delete'),
                'type' => 'action',
                'getter' => 'getId',
                'actions' => [
                    [
                        'caption' => __('Delete'),
                        'url' => [
                            'base' => 'backendfrom/*/delete',
                        ],
                        'field' => 'id',
                    ],
                ],
                'filter' => false,
                'sortable' => false,
                'index' => 'stores',
                'header_css_class' => 'col-action',
                'column_css_class' => 'col-action',
            ]
        );
        
        $this->addExportType('*/*/exportCsv', __('CSV'));
        $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',
            [
                'label' => __('Delete'),
                'url' => $this->getUrl('backendfrom/*/MassDelete'),
                'confirm' => __('Are you sure?'),
            ]
        );

        return $this;
    }

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

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