Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 2 - Admin grid add filter to collection

I created a grid in admin by XML UI components.

I need to filter collection via a URL parameter and I don't know how to achieve that. I tried to inject RequestInterface to the collection, but the filter didn't work.

di.xml

<virtualType name="SlideListingDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
    <arguments>
        <argument name="collection" xsi:type="object" shared="false">xxx\xxx\Model\ResourceModel\Grid\Slide\Collection</argument>
        <argument name="filterPool" xsi:type="object" shared="false">SlideListingFilterPool</argument> <!-- Define new object for filters -->
    </arguments>
</virtualType>
<virtualType name="SlideListingFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
    <arguments>
        <argument name="appliers" xsi:type="array">
            <item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
            <item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
        </argument>
    </arguments>
</virtualType>
<virtualType name="xxx\xxx\Model\ResourceModel\Grid\Slide\Collection" type="xxx\xxx\Ui\Component\DataProvider\SearchResult\Slide">
    <arguments>
        <argument name="mainTable" xsi:type="string">advox_sliders_slide</argument>
        <argument name="resourceModel" xsi:type="string">xxx\xxx\Model\ResourceModel\Slide</argument>
    </arguments>
</virtualType>
like image 222
szumel Avatar asked Oct 29 '22 23:10

szumel


1 Answers

you have to write after update_url in ui components

`<item name="update_url" xsi:type="url" path="mui/index/render"/>`

after this line you have to add below code

<item name="filter_url_params" xsi:type="array"> <item name="status" xsi:type="boolean">1</item> </item>

now you have to pass the status value in url.

like image 187
Khodu Vaishnav Avatar answered Jan 04 '23 13:01

Khodu Vaishnav