Magento 2 – Add Filter or Join Query in Grid Collection

adminfiltergridmagento2uicomponent

I created an admin grid using reference https://ranasohel.me/2014/04/20/creating-magento2-adminhtml-grid/.

Now I want to apply addFieldToFilter() of my collection in the grid.

How can I do this? Because Magento 2 use ui_component for the grid in XML.

Best Answer

If you need to create join in any grid then you need to existing grid collection. For this example, the grid has own collection. So you can add following code to grid collection.

File: SR/Weblog/Model/ResourceModel/BlogPosts/Grid/Collection.php

protected function _initSelect()
{
    parent::_initSelect();

    $this->join(
        [$this->getTable('second_table_name')],
        'main_table.blogpost_id = '.$this->getTable('second_table_name').'.blogpost_id',
        array()
    );

    return $this;
}

Also, you can use:

public function addFieldToFilter($field, $condition = null)
{
}
Related Topic