11

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

11
+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();
    }   
}
  • Hi KAndy, It's not working for me. Could you help me to sort it out ?? – Vigna S Sep 6 '16 at 7:04
  • 3
    To anyone finding this answer, please note that it only applies to legacy grids. Not UI-component based grids. – Andre Klang Jun 11 '18 at 6:59
  • So, how can we apply this type of filters to ui-component based grid? – Knight017 Jan 2 at 17:42
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.

  • 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 Jan 13 '16 at 11:11
  • 1
    maybe you should add more code you have to the question to get a better overview. – Marius Jan 13 '16 at 11:12
  • I have updated my question with filter code what i added – DRAJI Jan 13 '16 at 11:27
  • Great. I will take a new look and come back with a result. – Marius Jan 13 '16 at 11:28
  • Hi Marius, I tried this but it's not working, have you found any solution? – Vigna S Sep 6 '16 at 5:48
0

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.

  • 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 Jan 13 '16 at 9:16
  • Hi @EquinoxBlack Thank you for your response. But it by using UI component – DRAJI Jan 13 '16 at 11:10
0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.