Categories: How ToMagento 2

How to Filter UI Component Grid Collection in Magento 2

Magento grids plays vital role when it comes to displaying data in efficient way. Filtration to these grids are an added charm to get accurate data based on requirements. Once we require a specific implementation to filter UI component grid collection.

Here, I’m going to share the custom code which you can implement to UI component grid collection in Magento 2.

Paste the below code in UI component grid xml file to create a column. Use the same code with required changes to create as many columns as you want.

<pre class="lang:default decode:true ">
<dataSource name="test_comments_listing_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Magento\Customer\Ui\Component\DataProvider</argument>
        <argument name="name" xsi:type="string">test_comments_listing_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">id</argument>
        <argument name="requestFieldName" xsi:type="string">id</argument>
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="update_url" xsi:type="url" path="mui/index/render"/>
                <item name="filter_url_params" xsi:type="array">
                    <item name="status" xsi:type="string">*</item>
                </item>
            </item>
        </argument>
    </argument>
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
        </item>
    </argument>
</dataSource>
</pre>

From the above code, make changes as shown below to fetch the filter options. Inside the argument tag, you can see an item tag with name filter_url_params which is responsible to filter collection. In order to make it work, set parameter name inside name attribute of item tag through which you require to filter. Below code displays the example of filter by status on a collection, here you need to pass the status value through URL.

<pre class="lang:default decode:true ">
<argument name="data" xsi:type="array">
    <item name="config" xsi:type="array">
        <item name="update_url" xsi:type="url" path="mui/index/render"/>
        <item name="filter_url_params" xsi:type="array">
            <item name="status" xsi:type="string">*</item>
        </item>
    </item>
</argument>
</pre>

Hope this blog has helped you to filter grid collection in proper way.

Let me know if you stuck with any issue, I’ll be glad to help you. Waiting for your feedback and reviews in comment section below.

Click to rate this post!
[Total: 10 Average: 4.1]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago