1

I looked for hours but I'm not able to pre-filter a default admin grid like the admin product grid. Does anyone know how to do this ?

Thanks !

0

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();
    }   
}

Hope this will help you.

Thanks

  • Thanks for your quick reply ! I tried to apply it to the admin product grid but I'm not able to find where the collection is set. But I would like to preset the store filter and not necessarily custom the collection. Thanks ! – magentodev Apr 18 '18 at 14:02

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.