1

I want to filter my Grid based on condition that only active entries should be visible on the grid and not all entries from the table

I am using ui component to create grids

1 Answer 1

1

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

<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>

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 is_active value through URL.

<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="is_active" xsi:type="string">1</item>
        </item>
    </item>
</argument>

Hope this will help you Thanks

1
  • can you please share you full code where I need to add this file with location and di.xml Apr 27, 2021 at 12:34

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.