1

Here is my custom grid i wan to show only those data that status is paid but now the data present with paid and unpaid both . how can i set default filter to these custom block grid...

enter image description here

here is my block grid code. Block\Adminhtml\Custom\Grid.php

<?php
namespace Vrinsoft\Giftcard\Block\Adminhtml\Custom;


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

    /**
     * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory]
     */
    protected $_setsFactory;

    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $_productFactory;

    /**
     * @var \Magento\Catalog\Model\Product\Type
     */
    protected $_type;

    /**
     * @var \Magento\Catalog\Model\Product\Attribute\Source\Status
     */
    protected $_status;
    protected $_collectionFactory;

    /**
     * @var \Magento\Catalog\Model\Product\Visibility
     */
    protected $_visibility;

    /**
     * @var \Magento\Store\Model\WebsiteFactory
     */
    protected $_websiteFactory;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setsFactory
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     * @param \Magento\Catalog\Model\Product\Type $type
     * @param \Magento\Catalog\Model\Product\Attribute\Source\Status $status
     * @param \Magento\Catalog\Model\Product\Visibility $visibility
     * @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,
        \Magento\Store\Model\WebsiteFactory $websiteFactory,
        \Vrinsoft\Giftcard\Model\ResourceModel\Custom\Collection $collectionFactory,
        \Magento\Framework\Module\Manager $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(false);
       
    }

    /**
     * @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 \Magento\Backend\Block\Widget\Grid\Column $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()
    {
        $this->addColumn(
            'id',
            [
                'header' => __('ID'),
                'type' => 'number',
                'index' => 'id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );
        $this->addColumn(
            'order_id',
            [
                'header' => __('order_id'),
                'index' => 'order_id',
                'class' => 'order_id'
            ]
        );
        $this->addColumn(
            'customer_id',
            [
                'header' => __('customer_id'),
                'index' => 'customer_id',
                'class' => 'customer_id'
            ]
        );
        $this->addColumn(
            'product_id',
            [
                'header' => __('product_id'),
                'index' => 'product_id',
                'class' => 'product_id'
            ]
        );
        $this->addColumn(
            'name',
            [
                'header' => __('name'),
                'index' => 'name',
                'class' => 'name'
            ]
        );
        $this->addColumn(
            'email',
            [
                'header' => __('email'),
                'index' => 'email',
                'class' => 'email'
            ]
        );
        $this->addColumn(
            'phone',
            [
                'header' => __('phone'),
                'index' => 'phone',
                'class' => 'phone'
            ]
        );
        $this->addColumn(
            'description',
            [
                'header' => __('description'),
                'index' => 'description',
                'class' => 'description'
            ]
        );
        $this->addColumn(
            'status',
            [
                'header' => __('status'),
                'index' => 'status',
                'class' => 'status'
            ]
        );
        $this->addColumn(
            'redemestatus',
            [
                'header' => __('redemestatus'),
                'index' => 'redemestatus',
                'class' => 'redemestatus'
            ]
        );
        $this->addColumn(
            'price',
            [
                'header' => __('price'),
                'index' => 'price',
                'class' => 'price'
            ]
        );
        $this->addColumn(
            'redemecode',
            [
                'header' => __('redemecode'),
                'index' => 'redemecode',
                'class' => 'redemecode'
            ]
        );
        $this->addColumn(
            'currentdate',
            [
                'header' => __('currentdate'),
                'index' => 'currentdate',
                'type' => 'date',
            ]
        );
        /*{{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('giftcard/*/massDelete'),
                'confirm' => __('Are you sure?')
            )
        );
        return $this;
    }

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

    /**
     * @param \Magento\Catalog\Model\Product|\Magento\Framework\Object $row
     * @return string
     */
    public function getRowUrl($row)
    {
        return $this->getUrl(
            'giftcard/*/edit',
            ['store' => $this->getRequest()->getParam('store'), 'id' => $row->getId()]
        );
    }
}

1 Answer 1

0

in your collection add these below function

\Model\ResourceModel\Custom\Collection.php

/**
     * Add filter by status code(s) to collection
     *
     * @param string|string[] $status
     * @return $this
     */
    public function loadByStatus($status)
    {
        if (!empty($status)) {
            if (is_array($status)) {
                $this->addFieldToFilter("status", ['in' => $status]);
            } else {
                $this->addFieldToFilter("status", $status);
            }
        }
        return $this;
    }

And load you collection on grid like that

Block\Adminhtml\Custom\Grid.php

 /**
     * @return $this
     */
    protected function _prepareCollection()
    {
        try{
            
            
            // $collection =$this->_collectionFactory->addAttributeToFilter('status', ['eq'=>'paid'])->load();

            $collection =$this->_collectionFactory->loadByStatus('paid');

          

            $this->setCollection($collection);

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

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.