I need to filter a grid view to show only where is_approved = true on one of my grids.
This needs to happen on page load (i.e. don't want the user to have to specify a filter).
The grids are made through UI Components, not blocks so this answer and similar applicable. I also find the docs a bit vague/not complete (what does "…" mean?!)
I'm not quite sure where to start after having tried various options but can't find any documentation on it or answered questions.
UI Component
<filters name="listing_filters">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="displayArea" xsi:type="string">dataGridFilters</item>
<item name="dataScope" xsi:type="string">filters</item>
<item name="storageConfig" xsi:type="array">
<item name="provider" xsi:type="string">sample_post_listing.sample_post_listing.listing_top.bookmarks</item>
<item name="namespace" xsi:type="string">current.filters</item>
</item>
<item name="childDefaults" xsi:type="array">
<item name="provider" xsi:type="string">sample_post_listing.sample_post_listing.listing_top.listing_filters</item>
<item name="imports" xsi:type="array">
<item name="visible" xsi:type="string">sample_post_listing.sample_post_listing.listing_top.bookmarks:current.columns.${ $.index }.visible</item>
</item>
</item>
</item>
</argument>
<filterInput name="is_approved">0</filterInput>
</filters>
…
<column name="is_approved">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="array">
<item name="disable" xsi:type="array">
<item name="value" xsi:type="string">0</item>
<item name="label" xsi:type="string" translate="true">Pending</item>
</item>
<item name="enable" xsi:type="array">
<item name="value" xsi:type="string">1</item>
<item name="label" xsi:type="string" translate="true">Approved</item>
</item>
</item>
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
<item name="editor" xsi:type="string">select</item>
<item name="dataType" xsi:type="string">select</item>
<item name="label" xsi:type="string" translate="true">Status</item>
</item>
</argument>
</column>
Data Injection
<!-- Data Providers for Admin Grids -->
<virtualType name="PostsGridDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
<arguments>
<argument name="collection" xsi:type="object" shared="false">Sample\Sample\Model\Resource\Subscription\Collection</argument>
<argument name="filterPool" xsi:type="object" shared="false">SubscriptionGirdFilterPool</argument>
</arguments>
</virtualType>
<!-- Grid Collections -->
<virtualType name="Sample\Sample\Model\ResourceModel\Posts\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">sample_sample_posts</argument>
<argument name="resourceModel" xsi:type="string">Sample\Sample\Model\ResourceModel\Posts</argument>
<argument name="default_filter" xsi:type="array">
<item name="id" xsi:type="string">0</item>
</argument>
</arguments>
</virtualType>
Best Answer
You can create your own collection which will provide the items you like.
Create a new collection. Sample\Sample\Model\ResourceModel\Posts\Grid\Collection
that collection should:
a. Implement the Magento\Framework\Api\Search\SearchResultInterface. Check the functions of that Interface and put them in your collection
b. Better extend your already existing Collection: Sample\Sample\Model\ResourceModel\Posts\Collection
So the new collection should be like that:
then go the di.xml and replace:
with:
You can now have full control of the collection. You can find a similar example here: https://github.com/magento/magento2/blob/develop/app/code/Magento/Sales/Model/ResourceModel/Grid/Collection.php