0

I have crated Admin Grid in Magento 2 Using the Block. I have added the select field in it and it's working fine.

Now I want to add filter for the column in which select multiple options and filter the data.

$this->addColumn(
        'status',
        [
            'header' => __('Status'),
            'index' => 'status',
            'type' => 'options',
            'sortable' => true,
            'options' => $this->getStatuses(),
            'filter_index' => 'so.status',
            'header_css_class' => 'col-orders',
            'column_css_class' => 'col-orders'
        ]
    );

Is there any way to do for Grids which is created by the Block?

2 Answers 2

1

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();
3
  • How can I select multiple options in the dropdown?
    – m2dev
    Jun 2, 2023 at 12:13
  • To enable selecting multiple options in a dropdown, you need to modify the column configuration in your Magento 2 admin grid block. Specifically, you should use the multiselect type instead of options. Try this code **$this->addColumn( 'column_name', [ 'header' => __('Column Name'), 'index' => 'column_name', 'type' => 'multiselect', 'options' => $this->_getOptions(), // Provide the options for the select field 'filter_condition_callback' => [$this, '_applyFilter'], ] ); **
    – Ansari
    Jun 2, 2023 at 12:28
  • I have already used that when I use 'type' => 'multiselect', it's changed to input field in the grid header.
    – m2dev
    Jun 2, 2023 at 12:45
0

To add a filter for the "status" column with multiple select options, you need to modify the addColumn() method call as follows,

$this->addColumn(
    'status',
    [
        'header' => __('Status'),
        'index' => 'status',
        'type' => 'options',
        'sortable' => true,
        'options' => $this->getStatuses(),
        'filter_condition_callback' => [$this, 'filterStatus'],
        'header_css_class' => 'col-orders',
        'column_css_class' => 'col-orders'
    ]
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.