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 !
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIf 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