Try these all step it can be usefull for you
1.Step 1: Prepare the Grid Block
Ensure that your grid block extends the Magento\Backend\Block\Widget\Grid\Extended class. This class provides additional methods to handle filtering.
2.Step 2: Add the Filter Field
Inside your grid block's _prepareColumns() method, add the column that you want to filter. Use the filter_condition_callback option to specify the method that will handle the filtering logic.
$this->addColumn(
'column_name',
[
'header' => __('Column Name'),
'index' => 'column_name',
'type' => 'options',
'options' => $this->_getOptions(), // Provide the options for the select field
'filter_condition_callback' => [$this, '_applyFilter'],
]
);
3.Step 3: Implement the Filter Callback Method
Create a method _applyFilter($collection, $column) in your grid block. This method will receive the collection and the column object, allowing you to modify the collection query based on the selected filter options.
protected function _applyFilter($collection, $column)
{
if (!$column->getFilter()->getValue()) {
return $this;
}
$selectedOptions = explode(',', $column->getFilter()->getValue());
$this->getCollection()->addFieldToFilter('column_name', ['in' => $selectedOptions]);
return $this;
}
4.Step 4: Apply the Filter in the Controller
Ensure that your controller action, which is responsible for rendering the grid, applies the filter to the grid block. You can do this by calling the setDefaultFilter() method on the grid block instance
$gridBlock = $this->_view->getLayout()->createBlock(\Namespace\Module\Block\Adminhtml\Grid::class);
$gridBlock->setDefaultFilter(['column_name' => 'option1,option2']);
$this->_view->loadLayout();
$this->_view->renderLayout();