2

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
  <module name="Codextblog_Customgridtab" setup_version="1.0.1"></module>
</config>

Registration.php

<?php \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Codextblog_Customgridtab',
    __DIR__ );

custom_grid_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">custom_grid_listing.custom_grid_listing_data_source</item>
            <item name="deps" xsi:type="string">custom_grid_listing.custom_grid_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">custom_grid_columns</item>
    </argument>
    <dataSource name="custom_grid_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Codextblog\Customgridtab\Ui\DataProvider\Product\CustomDataProvider</argument>
            <argument name="name" xsi:type="string">custom_grid_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="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="cacheRequests" xsi:type="boolean">false</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>
    <listingToolbar name="listing_top">
        <filters name="listing_filters">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="statefull" xsi:type="array">
                        <item name="applied" xsi:type="boolean">false</item>
                    </item>
                    <item name="params" xsi:type="array">
                        <item name="filters_modifier" xsi:type="array"/>
                    </item>
                </item>
            </argument>
        </filters>
        <paging name="listing_paging"/>
    </listingToolbar>
    <columns name="custom_grid_columns" class="Magento\Ui\Component\Listing\Columns">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="childDefaults" xsi:type="array">
                    <item name="fieldAction" xsi:type="array">
                        <item name="provider" xsi:type="string">CustomGrid</item>
                        <item name="target" xsi:type="string">startEdit</item>
                        <item name="params" xsi:type="array">
                            <item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
                            <!--  <item name="1" xsi:type="boolean">true</item> -->
                        </item>
                    </item>
                </item>
            </item>
        </argument>
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">entity_id</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="entity_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                    <item name="sortOrder" xsi:type="number">1</item>
                </item>
            </argument>
        </column>
        <column name="sku">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">SKU</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="truncate" xsi:type="number">50</item>
                    <item name="nl2br" xsi:type="boolean">true</item>
                    <item name="escape" xsi:type="boolean">true</item>
                </item>
            </argument>
        </column>
        <column name="created_at" class="Magento\Ui\Component\Listing\Columns\Date">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">dateRange</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                    <item name="dataType" xsi:type="string">date</item>
                    <item name="label" xsi:type="string" translate="true">Created At</item>
                    <item name="sortOrder" xsi:type="number">30</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Codextblog/Customgridtab/Ui/DataProvider/Product/CustomDataProvider.php

protected $request;
public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    CollectionFactory $collectionFactory,
    RequestInterface $request,
    array $meta = [],
    array $data = []
) {
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    $this->collectionFactory = $collectionFactory;
    $this->collection = $this->collectionFactory->create();
    $this->request = $request;
}

public function getData()
{
    $this->getCollection();

    $arrItems = [
        'totalRecords' => $this->getCollection()->getSize(),
        'items' => [],
    ];

    foreach ($this->getCollection() as $item) {
        $arrItems['items'][] = $item->toArray([]);
    }

    return $arrItems;
}
public function addFilter(\Magento\Framework\Api\Filter $filter)
{
    parent::addFilter($filter);
}}

Codextblog/Customgridtab/Ui/DataProvider/Product/Form/Modifier/CustomTab.php

protected $_backendUrl;
protected $_productloader;
protected $_modelCustomgridFactory;

/**
 * @var \Magento\Catalog\Model\Locator\LocatorInterface
 */
protected $locator;

/**
 * @var ArrayManager
 */
protected $arrayManager;

/**
 * @var UrlInterface
 */
protected $urlBuilder;

/**
 * @var array
 */
protected $meta = [];

/**
 * @param LocatorInterface $locator
 * @param ArrayManager $arrayManager
 * @param UrlInterface $urlBuilder
 */
public function __construct(
    LocatorInterface $locator,
    ArrayManager $arrayManager,
    UrlInterface $urlBuilder,
    \Magento\Catalog\Model\ProductFactory $_productloader,
    \Magento\Backend\Model\UrlInterface $backendUrl
) {
    $this->locator = $locator;
    $this->arrayManager = $arrayManager;
    $this->urlBuilder = $urlBuilder;
    $this->_productloader = $_productloader;
    $this->_backendUrl = $backendUrl;
}

public function modifyData(array $data)
{
    return $data;
}

public function modifyMeta(array $meta)
{
    $this->meta = $meta;
    $this->addCustomTab();

    return $this->meta;
}

protected function addCustomTab()
{
    $this->meta = array_merge_recursive(
        $this->meta,
        [
            static::SAMPLE_FIELDSET_NAME => $this->getTabConfig(),
        ]
    );
}

protected function getTabConfig()
{
return [
    'arguments' => [
        'data' => [
            'config' => [
                'label' => __('Custom Grid Tab'),
                'componentType' => Fieldset::NAME,
                'dataScope' => '',
                'provider' => static::FORM_NAME . '.product_form_data_source',
                'ns' => static::FORM_NAME,
                'collapsible' => true,
            ],
        ],
    ],
    'children' => [
        static::SAMPLE_FIELD_NAME => [
            'arguments' => [
                'data' => [
                    'config' => [
                        'autoRender' => true,
                        'componentType' => 'insertListing',
                        'dataScope' => 'custom_grid_listing',
                        'externalProvider' => 'custom_grid_listing.custom_grid_listing_data_source',
                        'selectionsProvider' => 'custom_grid_listing.custom_grid_listing.product_columns.ids',
                        'ns' => 'custom_grid_listing',
                        'render_url' => $this->urlBuilder->getUrl('mui/index/render'),
                        'realTimeLink' => false,
                        'behaviourType' => 'simple',
                        'externalFilterMode' => true,
                        'imports' => [
                            'productId' => '${ $.provider }:data.product.current_product_id'
                        ],
                        'exports' => [
                            'productId' => '${ $.externalProvider }:params.current_product_id'
                        ],

                    ],
                ],
            ],
            'children' => [],
        ],
    ],
];
}}

" with the above code i can click and select the checkbox, i am unable to save data in db. Note: I have separately created model for saving.
Reference Link : "http://www.codextblog.com/magento-2/how-to-add-custom-grid-into-a-custom-tab-in-product-edit-page-in-magento-2-admin/ "

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.