I have a collection of users and I want to create a backend grid to show only some user depending on a filter.
For example I want to show all the users in my table whith age greater than 18.
I created a new collection and i added my filter to _initSelect
method.
...
protected function _initSelect()
{
parent::_initSelect();
$this->addUsersFilter();
return $this;
}
...
public function addUsersFilter()
{
$this
->addFieldToFilter('age', ['gteq' => 18]);
return $this;
}
...
Bt my grid keeps showing me the whole table content regardless my filter.
Does anyone knows how to filter a grid directly from its source collection?
Best Answer
Ok, I found it by myself. Posting here if it helps:
This kind of filter cannot be done at collection level, but only at data provider level.
So, if you have:
You can just point to your custom data provider instead of inheriting from
Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
.Your XML becomes:
Now we are referring to
My\Module\Ui\Component\DataProvider\SearchResult\Items
instead ofMagento\Framework\View\Element\UiComponent\DataProvider\SearchResult
.Now we have to create our new class:
Hope it helps.