14

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

5 Answers 5

14
+50

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();
    }   
}
4
  • Hi KAndy, It's not working for me. Could you help me to sort it out ??
    – Vigna S
    Commented Sep 6, 2016 at 7:04
  • 8
    To anyone finding this answer, please note that it only applies to legacy grids. Not UI-component based grids. Commented Jun 11, 2018 at 6:59
  • So, how can we apply this type of filters to ui-component based grid?
    – Knight017
    Commented Jan 2, 2019 at 17:42
  • 2
    For UI-component based grid, it can be done with the same approach like this magento.stackexchange.com/a/178663/55259
    – Torrey
    Commented Feb 28, 2020 at 21:03
5

you need to add this inside the arguments tag:

<argument name="default_filter" xsi:type="array">
    <item name="field_name_here" xsi:type="string">value here</item>
</argument>

if your arguments are contained in this block

<block class="Magento\Backend\Block\Widget\Grid" name="some.name.here" as="grid">

you need to create your own class that extends Magento\Backend\Block\Widget\Grid like this:

<?php 
namespace Namespace\Module\Block\Adminhtml\Whatever;

class Grid extends \Magento\Backend\Block\Widget\Grid
{
    public function _construct()
    {
        parent::_construct(); 
        if ($this->hasData('default_filter')) {
            $this->setDefaultFilter($this->getData('default_filter'));
        }
    }
}

and modify the block tag above to

 <block class="Namespace\Module\Block\Adminhtml\Whatever\Grid" name="some.name.here" as="grid">

if you already use a custom grid and not the default Magento\Backend\Block\Widget\Grid you don't need to create the class.
You just need to copy the _construct method from above in your class.

6
  • Hi @Marius , Thank you for your response. In construct method i got default filter value. But filtering not working. Not too get all records
    – DRAJI
    Commented Jan 13, 2016 at 11:11
  • 1
    maybe you should add more code you have to the question to get a better overview.
    – Marius
    Commented Jan 13, 2016 at 11:12
  • I have updated my question with filter code what i added
    – DRAJI
    Commented Jan 13, 2016 at 11:27
  • Great. I will take a new look and come back with a result.
    – Marius
    Commented Jan 13, 2016 at 11:28
  • Hi Marius, I tried this but it's not working, have you found any solution?
    – Vigna S
    Commented Sep 6, 2016 at 5:48
1

here is the reference on devdocs:

http://devdocs.magento.com/guides/v2.0/ui-components/ui-secondary-filter.html

If You need more info on that - you can create a pull request or an issue via docs page - just click "Edit this page on GitHub" link. Docs are constantly updated and improved.

2
  • 1
    the link you mentioned shows how to add filters in a grid that uses the ui components. I think the question is about changing a grid that extends Magento\Backend\Block\Widget\Grid. But I may be wrong.
    – Marius
    Commented Jan 13, 2016 at 9:16
  • Hi @EquinoxBlack Thank you for your response. But it by using UI component
    – DRAJI
    Commented Jan 13, 2016 at 11:10
1

After trying many codes which doesn't work, finally find a way doesn't need coding - make use of saving custom views function which could save filter and column settings.

Specific steps

After setting filter and column settings, click on the Default View button with the eye icon, then click Save View As… to enter a name for your view. Then you could click the custom view whenever you want to get the grid just the way you like it, and you can save custom views as many as you like.

You can also go back and use the edit icon to rename it or delete it later on, or to return to the default view.

I think this is a very good function in Magento 2 :)

enter image description here

0

The above solutions didn't work for me.

I solved the problem by extending Grid and looping through the items to filter, I even defined my own XML variable to allow me to (OR/AND) the filters, for more information see below:

  1. XML:

             <block class="MyModule\MyExtension\Block\Adminhtml\Lists\Grid" name="ordersprocess_grid.grid" as="grid">
                 <arguments>
                     <argument name="id" xsi:type="string">order_id</argument>
                     <argument name="dataSource" xsi:type="object">MyModule\MyExtension\Model\Order\ResourceModel\Processing\Collection</argument>
                     <argument name="default_filter" xsi:type="array">
                         <item name="complete" xsi:type="string">0</item>
                         <item name="cancelled" xsi:type="string">0</item>
                     </argument>
                     <argument name="my_filter_type" xsi:type="string">and</argument>
                     <argument name="default_sort" xsi:type="string">something_date</argument>
                     <argument name="default_dir" xsi:type="string">DESC</argument>
                     <argument name="save_parameters_in_session" xsi:type="boolean">1</argument>
                 </arguments>
    
  2. PHP Class:

namespace MyModule\MyExtension\Block\Adminhtml\Lists;
    
class Grid extends \Magento\Backend\Block\Widget\Grid
{
  public function _construct()
  {
      parent::_construct();
    
      $FilterType = $this->getData('my_filter_type');
    
      if ($this->hasData('default_filter'))
      {
          foreach ($this->getDefaultFilter() as $Field => $Value)
          {
              $this->getCollection()->addFilter ($Field, $Value, $FilterType);
          }
      }
  }
}

With the above code, you don't need to rely on "default_filter", if you follow my way, you could easily do the same as I did to "my_filter_type" to create your own something like "my_filter_field" which prevent the code from conflicting with God-Knows-What Magento does with the "default_filter" variable.

I hope this helps,

H

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.