Magento 2 – How to Filter Custom Grid Collection

collection;gridmagento2

I am trying to filter my collection from my custom table using _initSelect method in class Collection in namespace Anshu\Custom\Model\ResourceModel\MyData

protected function _initSelect()
    {
        // code for filtering collection
    }

But it is not going through this method.

Am I following the correct approach?
What can I filter my collection to show data in Grid in admin?

Best Answer

The issue was because of the type in virtualType tag in di.xml
Changed

<virtualType name="Anshu\Custom\Model\ResourceModel\MyData\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">

to

<virtualType name="Anshu\Custom\Model\ResourceModel\MyData\Grid\Collection">

Also Changed

class Collection extends \Anshu\Custom\Model\ResourceModel\MyData\Collection implements \Magento\Framework\Api\Search\SearchResultInterface

to

class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
Related Topic