4

How to add storeview code in admin grid which created using ui component.

enter image description here

7

Update your layout as below. (File: MyVendor/MyModule/view/adminhtml/layout/mymodule_myproduct_grid.xml )

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="page.main.actions">
            <block class="Magento\Backend\Block\Store\Switcher" name="adminhtml.report.grid.store_switcher" as="store_switcher">
                <arguments>
                    <argument name="use_confirm" xsi:type="string">0</argument>
                    <argument name="switch_websites" xsi:type="string">0</argument>
                    <argument name="switch_store_groups" xsi:type="string">0</argument>
                    <argument name="switch_store_views" xsi:type="string">1</argument>
                </arguments>
            </block>
        </referenceContainer>
        <referenceContainer name="content">
            <uiComponent name="my_product_listing"/>
        </referenceContainer>
    </body>
</page>

To filter products you need to change the UI component for the Bookmark.

Create a UI component class as (File: MyVendor/MyModule/Component/Bookmark.php ):

namespace MyVendor\MyModule\Component;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Api\BookmarkManagementInterface;
use Magento\Ui\Api\BookmarkRepositoryInterface;

class Bookmark extends \Magento\Ui\Component\Bookmark
{
     public function __construct(
        ContextInterface $context,
        \MyVendor\MyModule\Api\ProfileRepositoryInterface $profile,
        BookmarkRepositoryInterface $bookmarkRepository,
        BookmarkManagementInterface $bookmarkManagement,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $bookmarkRepository, $bookmarkManagement, $components, $data);
        $this->profile = $profile;
    }

    /**
     * Register component
     *
     * @return void
     */
    public function prepare()
    {
        $namespace = $this->getContext()->getRequestParam('namespace', $this->getContext()->getNamespace());
        $config = [];
        if (!empty($namespace)) {
            $storeId = $this->getContext()->getRequestParam('store');

            if (empty($storeId)) {
                $storeId = $this->getContext()->getFilterParam('store_id');
            }

            $bookmarks = $this->bookmarkManagement->loadByNamespace($namespace);
            /** @var \Magento\Ui\Api\Data\BookmarkInterface $bookmark */
            foreach ($bookmarks->getItems() as $bookmark) {
                if ($bookmark->isCurrent()) {
                    $config['activeIndex'] = $bookmark->getIdentifier();
                }

                $config = array_merge_recursive($config, $bookmark->getConfig());

                if (!empty($storeId)) {
                    $config['current']['filters']['applied']['store_id'] = $storeId;
                }
            }
        }

        $this->setData('config', array_replace_recursive($config, $this->getConfiguration($this)));

        parent::prepare();

        $jsConfig = $this->getConfiguration($this);
        $this->getContext()->addComponentDefinition($this->getComponentName(), $jsConfig);
    }
}

Add the Bookmark UI component in your UI grid xml (File: MyVendor/MyModule/view/adminhtml/ui_component/my_product_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">
     <listingToolbar name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sticky" xsi:type="boolean">true</item>
                <item name="template" xsi:type="string">ui/grid/toolbar</item>
            </item>
        </argument>
        <bookmark name="bookmarks" class="\MyVendor\MyModule\Component\Bookmark">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/controls/bookmarks/bookmarks</item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="saveUrl" xsi:type="url" path="mui/bookmark/save"/>
                        <item name="deleteUrl" xsi:type="url" path="mui/bookmark/delete"/>
                        <item name="namespace" xsi:type="string">my_product_listing</item>
                    </item>
                </item>
            </argument>
        </bookmark>
        <columnsControls name="columns_controls"/>
        <filters name="listing_filters" />
        <paging name="listing_paging"/>
    </listingToolbar>
</listing>

Replace: \MyVendor\MyModule and my_product_listing with your namespace.

Also there is another way of add the store selector in the grid i.e via filters same as catalog product grid.

  • Also, Do you want to filter products on the basis of the selected store? Because that's another thing to be done. – Milind Singh Dec 7 '18 at 6:43
  • yes i want to do – Rutvee Sojitra Dec 7 '18 at 6:44
  • 1
    @RutveeSojitra I have updated the code. hope it helps – Milind Singh Dec 7 '18 at 6:55
  • Hi @MilindSingh. Can you please update file path in your answer ? It will be helpful for me. – Denish Feb 19 at 5:54
  • Hi @DharmikPonda I have updated the paths. Hope it helps. – Milind Singh Feb 19 at 6:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.