How to Add Multi Select Filter in Magento 2 Admin Grid
Have you ever needed a Magento 2 admin grid that is filterable by more column’s values, i.e. something from multiselect drop-down?
For example, you want to export customers with 3 different groups and you have to select a group thrice and export the data for all the 3 one by one. However, if you want to filter all 3 of them together, you cannot perform a single action as the default Magento 2 allows applying only one filter at a time.
If the business requirement demands to apply a mass action on products satisfying more than one condition, the solution below can be helpful. Likewise implementing allows to filter orders based on multiple order status.
Implement the below code to add multi select filter in Magento 2 admin grid as shown in the figure below:
Steps to Add Multi-Select Filter in Magento 2 Admin Grid:
- Add this code into ui_component’s grid xml file:
for example : [Vendor]\[Module]\view\adminhtml\ui_component\my_first_grid.xml
12345678910111213141516171819202122232425262728293031323334353637383940414243444546<filters name="listing_filters"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="columnsProvider" xsi:type="string">imageclean_use_images_grid.imageclean_use_images_grid.images_columns</item><item name="storageConfig" xsi:type="array"><item name="provider" xsi:type="string">imageclean_use_images_grid.imageclean_use_images_grid.listing_top.bookmarks</item><item name="namespace" xsi:type="string">current.filters</item></item><item name="templates" xsi:type="array"><item name="filters" xsi:type="array"><item name="select" xsi:type="array"><item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item><item name="template" xsi:type="string">ui/grid/filters/elements/ui-select</item></item></item></item><item name="childDefaults" xsi:type="array"><item name="provider" xsi:type="string">imageclean_use_images_grid.imageclean_use_images_grid.listing_top.listing_filters</item><item name="imports" xsi:type="array"><item name="visible" xsi:type="string">imageclean_use_images_grid.imageclean_use_images_grid.images_columns.${ $.index }:visible</item></item></item></item></argument></filters><column name="used"><argument name="data" xsi:type="array"><item name="options" xsi:type="object">\[Vendor]\[Module]\Model\ProductImage\Status</item><item name="config" xsi:type="array"><item name="sortOrder" xsi:type="number">10</item><item name="label" translate="true" xsi:type="string">Image Status</item><item name="filter" xsi:type="string">select</item><item name="dataType" xsi:type="string">select</item><item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item></item></argument></column> - Create Status.php file at path [Vendor]\[Module]\Model\ProductImage\Status.php
123456789101112<?phpnamespace [Vendor]\[Module]\Model\ProductImage;class Status implements \Magento\Framework\Option\ArrayInterface{public function toOptionArray(){$options = [];$options[] = ['label' => 'Unused image', 'value' => '0'];$options[] = ['label' => 'Used image', 'value' => '1'];return $options;}}
That’s it.
Any doubts on the topic will be entertained in the Comments section below. I’d be happy to solve it.
Do share the solution with fellow developers via social media.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Disable Cache for Block Using XML in Magento 2
Magento 2.3.3: Know Everything about [October 2019] Release
Next