Magento 2 : Admin Grid

Posted 6 months ago. Visible to the public.

How to add di.xml configuration for simplet grid.

<!-- City listing definitions -->

    <virtualType name="Kemana\Directory\Model\ResourceModel\City\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">kemana_directory_city</argument>
            <argument name="resourceModel" xsi:type="string">Kemana\Directory\Model\ResourceModel\City</argument>
        </arguments>
    </virtualType>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="city_listing_data_source" xsi:type="string">Kemana\Directory\Model\ResourceModel\City\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <!-- City listing definitions -->

How to add di.xml for Grid joining table

<!-- City listing definitions -->

    <type name="Kemana\Directory\Model\ResourceModel\City\Grid\Collection" >
        <arguments>
            <argument name="mainTable" xsi:type="string">kemana_directory_city</argument>
            <argument name="resourceModel" xsi:type="string">Kemana\Directory\Model\ResourceModel\City</argument>
        </arguments>
    </type>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="city_listing_data_source" xsi:type="string">Kemana\Directory\Model\ResourceModel\City\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <!-- City listing definitions -->

The Grid\Collection implementation

<?php


namespace Kemana\Directory\Model\ResourceModel\City\Grid;

use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;

class Collection extends SearchResult
{

    protected function _initSelect()
    {
        $select = $this->getSelect()->from(['main_table' => $this->getMainTable()]);
        $select->joinLeft(
            ['dcr' => $this->getTable('directory_country_region')],
            'main_table.region_id = dcr.region_id',
            ['country_id', 'code', 'default_name']
        );
        return $this;
    }
}
vasan
Last edit
6 months ago
vasan
Keywords
Magento, 2, Grid
Posted by vasan to vasan's deck (2023-10-24 10:02)