Magento2 – Add Default Filter to Grid

admincollection;gridmagento2

I have created one custom module with title & status field attribute in admin grid.

Grid collections in module_test_grid_block.xml

        <arguments>
             <argument name="id" xsi:type="string">testGrid</argument>
             <argument name="dataSource" xsi:type="object">Namespace\Module\Model\ResourceModel\test\Collection</argument>
             <argument name="default_sort" xsi:type="string">id</argument>
             <argument name="default_dir" xsi:type="string">desc</argument>                             
             <argument name="grid_url" xsi:type="url" path="*/*/grid"><param name="_current">1</param></argument>
         </arguments>

I want to show the data which are enabled. Have any option to add default filter in admin grid collections?

EDIT

    <block class="Namespace\Module\Block\Adminhtml\Test\Grid" name="namespace_module_test.grid" as="grid">
       <arguments>
             <argument name="id" xsi:type="string">testGrid</argument>
             <argument name="dataSource" xsi:type="object">Namespace\Module\Model\ResourceModel\test\Collection</argument>
             <argument name="default_sort" xsi:type="string">id</argument>
             <argument name="default_dir" xsi:type="string">desc</argument>                             
             <argument name="grid_url" xsi:type="url" path="*/*/grid"><param name="_current">1</param></argument>
             <argument name="default_filter" xsi:type="array">
                  <item name="status" xsi:type="string">1</item>
             </argument>
         </arguments>
        .
        .
      </block>

In Block – Grid.php

    namespace Namespace\Module\Block\Adminhtml\Test;

    use Magento\Backend\Block\Widget\Grid as WidgetGrid;

     class Grid extends WidgetGrid
        {
          public function _construct()
           {
            parent::_construct(); 
            if ($this->hasData('default_filter')) {
            // print_r($this->getData('default_filter'));die;
            $this->setDefaultFilter($this->getData('default_filter'));
           }
        }
      }

I have followed this link to create admin grid in magento 2

http://www.mage-world.com/blog/grid-and-form-in-magento-2-admin-panel-part-1.html

Best Answer

If you define grid collection thought layout than you can use updater to add default filter.

<argument name="dataSource" xsi:type="object">
    Tutorial\SimpleNews\Model\Resource\News\Collection
    <updater>Tutorial\SimpleNews\Model\Resource\News\Collection\Updater</updater>
</argument>

and

<?php
namespace Tutorial\SimpleNews\Model\Resource\News\Collection;

class CollectionUpdater implements \Magento\Framework\View\Layout\Argument\UpdaterInterface
{

    /**
     * Update grid collection according to chosen order
     *
     * @param \Tutorial\SimpleNews\Model\Resource\News\Collection $argument
     * @return \Tutorial\SimpleNews\Model\Resource\News\Collection
     */
    public function update($argument)
    {
        $argument->addFieldToFilter('you_field', 'value');

        return $argument;
    }
}

or Extend Grid block

class Grid extends \Magento\Backend\Block\Widget\Grid
{
    protected function _prepareCollection()
    {
        if ($this->getCollection()) {
            foreach ($this->getDefaultFilter() as $field => $value) {
                $this->getCollection()->addFieldToFilter($field, $value);
            }
        }
        return parent::_prepareCollection();
    }   
}