Magento 2 : Listing component Dataprovider

Posted . Visible to the public.

Adding dataprovider for listing component with filtering and sorting

Listing component default data provider get all the data from collection.
Sometime we need to do filer and sorting.
Following example shows how to do the filer and sorting of a listing component



class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{

    /**
     * @inheritdoc
     */
    public function getData()
    {
        $this->addFilter(
            $this->filterBuilder->setField('proposal_id')->setValue($this->request->getParam('proposal_id'))->setConditionType('eq')->create()
        );
        $this->addOrder('due_date', 'ASC');
        return parent::getData();
    }

}

In listing xml file


<column name="amount" class="Vasan\Bidding\Ui\Component\Listing\Stone\Column\Amount" sortOrder="40">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="label" xsi:type="string" translate="true">Amount</item>
                </item>
            </argument>
        </column>
<dataProvider class="Vasan\Bidding\Ui\Component\Listing\Stone\DataProvider" name="stone_listing_data_source">
            <settings>
                <requestFieldName>entity_id</requestFieldName>
                <primaryFieldName>entity_id</primaryFieldName>
            </settings>
        </dataProvider>

di.xml

<!-- Stone listing definitions -->

    <type name="Vasan\Bidding\Model\ResourceModel\Stones\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">bidding_stones</argument>
            <argument name="resourceModel" xsi:type="string">Vasan\Bidding\Model\ResourceModel\Stones</argument>
        </arguments>
    </type>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="stone_listing_data_source" xsi:type="string">Vasan\Bidding\Model\ResourceModel\Stones\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <!-- Stone listing definitions -->
vasan
Last edit
vasan
Posted by vasan to vasan's deck (2022-03-24 16:42)