0

I want to add Filter to Grid like Sku or name fillter in Manage Product here my code

Grid.php

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Vendor\Module\Model\testFactory
     */
    protected $_testFactory;

    /**
     * @var \Vendor\Module\Model\Status
     */
    protected $_status;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Vendor\Module\Model\testFactory $testFactory
     * @param \Vendor\Module\Model\Status $status
     * @param \Magento\Framework\Module\Manager $moduleManager
     * @param array $data
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Vendor\Module\Model\TestFactory $TestFactory,
        \Vendor\Module\Model\Status $status,
        \Magento\Framework\Module\Manager $moduleManager,
        array $data = []
    ) {
        $this->_testFactory = $TestFactory;
        $this->_status = $status;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $backendHelper, $data);
    }

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

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

        parent::_prepareCollection();

        return $this;
    }

    /**
     * @return $this
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    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' => __('text'),
                        'index' => 'name',
                    ]
                );

           $this->addExportType($this->getUrl('module/*/exportCsv', ['_current' => true]),__('CSV'));
           $this->addExportType($this->getUrl('module/*/exportExcel', ['_current' => true]),__('Excel XML'));

        $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()->setTemplate('Vendor_Module::test/grid/massaction_extended.phtml');
        $this->getMassactionBlock()->setFormFieldName('test');

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

        $statuses = $this->_status->getOptionArray();

        $this->getMassactionBlock()->addItem(
            'status',
            [
                'label' => __('Change status'),
                'url' => $this->getUrl('module/*/massStatus', ['_current' => true]),
                'additional' => [
                    'visibility' => [
                        'name' => 'status',
                        'type' => 'select',
                        'class' => 'required-entry',
                        'label' => __('Status'),
                        'values' => $statuses
                    ]
                ]
            ]
        );


        return $this;
    }


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

    /**
     * @param \Vendor\Module\Model\test|\Magento\Framework\Object $row
     * @return string
     */
    public function getRowUrl($row)
    {

        return $this->getUrl(
            'module/*/edit',
            ['id' => $row->getId()]
        );

    }



}

module_test_grid.xml

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/layout_generic.xsd">
    <update handle="formkey"/>
    <container name="root" label="Root">
        <block class="Vendor\Module\Block\Adminhtml\Test\Grid" name="admin.module.test.grid"/>
    </container>
</layout>

module_test_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Adminhtml\Test" name="module_test_listing"/>
        </referenceContainer>
        <referenceContainer name="admin.module.test.grid"></referenceContainer>
    </body>
</page>

can anyone guide me how can i add filter form like Manage Product Sku,Name etc...

  • 1
    Want to perform filter on Magento products Or Yours custom table collection class? – Amit Bera Feb 17 at 13:51
  • Custom Table Collection – Navin Bhudiya Feb 17 at 14:04
  • want this grid for admin area? – Amit Bera Feb 17 at 14:16
  • yes i had already added grid in admin but i want add filter into same – Navin Bhudiya Feb 17 at 14:45
  • @AmitBera any idea? – Navin Bhudiya Feb 18 at 5:16

Your Answer

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

Browse other questions tagged or ask your own question.