6

I want to show store-views in ui component grid. I am using this code in:

/app/code/Mymodule/Myimages/view/adminhtml/ui_component/communieimages_name.xml

<column name="store_ids" class="Magento\Store\Ui\Component\Listing\Column\Store">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                <item name="sortable" xsi:type="boolean">false</item>
                <item name="label" xsi:type="string" translate="true">Store View</item>
            </item>
        </argument>
    </column>

but it is showing as blank. Can anyone please help me to fix this issue??? enter image description here

4

2 Answers 2

7

I will solve this issue.

/app/code/Mymodule/Myimages/view/adminhtml/ui_component/communieimages_name.xml

In this file Add this

<column name="store_ids" class="[vendorname]\[Modulename]\Ui\Component\Listing\Columns\Store">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
        <item name="component" xsi:type="string">[vendorname]_[Modulename]/js/ui/grid/columns/html</item>
        <item name="align" xsi:type="string">left</item>
        <item name="sortable" xsi:type="boolean">false</item>
        <item name="label" xsi:type="string" translate="true">Store View</item>
        <item name="sortOrder" xsi:type="number">60</item>
        </item>
    </argument>
</column>

next, you have to create a class which is mention in the above ui_component File

[vendorname]/[Modulename]/Ui/Component/Listing/Columns/Store.php

<?php

namespace Vendor\Module\Ui\Component\Listing\Columns;

use Magento\Framework\Escaper;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Store\Model\System\Store as SystemStore;

class Store extends \Magento\Store\Ui\Component\Listing\Column\Store
{
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        SystemStore $systemStore,
        Escaper $escaper,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $systemStore, $escaper, $components, $data, 'store_ids');
    }

    protected function prepareItem(array $item)
    {
        $content = $origStores '';
        if (!is_null($item[$this->storeKey])) {
            $origStores = $item[$this->storeKey];
        }

        if (!is_array($origStores)) {
            $origStores = explode(',', $origStores);
        }
        if (in_array(0, $origStores) && count($origStores) == 1) {
            return __('All Store Views');
        }

        $data = $this->systemStore->getStoresStructure(false, $origStores);

        foreach ($data as $website) {
            $content .= $website['label'] . "<br/>";
            foreach ($website['children'] as $group) {
                $content .= str_repeat('&nbsp;', 3) . $this->escaper->escapeHtml($group['label']) . "<br/>";
                foreach ($group['children'] as $store) {
                    $content .= str_repeat('&nbsp;', 6) . $this->escaper->escapeHtml($store['label']) . "<br/>";
                }
            }
        }

        return $content;
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                $item[$this->getData('name')] = $this->prepareItem($item);
            }
        }
        return $dataSource;
    }
}

Next, you have to create a Component File that apply an Proper Formate view of your Store View in Admin Grid

this will mention in our ui_component File

<item name="component" xsi:type="string">[vendorname]_[Modulename]/js/ui/grid/columns/html</item>

create an html.js in the following path

[vendorname]/[Modulename]/view/adminhtml/web/js/ui/grid/columns/html.js

define([
    'Magento_Ui/js/grid/columns/column'
], function (Component) {
    'use strict';

return Component.extend({
     defaults: {
        bodyTmpl: 'ui/grid/cells/html'
     }
  });
});

and make you Sure Store view Data do not store in an Array Formate. it has to convert into string formate and then it will store in the table.

this will happen at the time of Save your Admin From or Edit Admin Form.

add some line of code into your (Save or Edit) file given below

$data = $this->getRequest()->getPostValue();

if(!empty($data['store_ids'])) {
   $store_view = implode(',',$data['store_ids']);
   $this->request->setPostValue('store_ids',$store_view);
}

now you see Admin Gid will show you Store View as you want.

I Hope This Helps You.

4
  • I can't understand why you have created same file again in your module? Can't I use store module core file as a renderer directly? What is the difference? Feb 25, 2020 at 5:25
  • 2
    @PatrickW it can also achive using 'Magento\Store\Ui\Component\Listing\Column\Store' but there are a problem in our custom column name because in this class line number:62 is already define field name '$storeKey = 'store_id' so when we use this class store view not show that's why we create same file again in your module. if you want to use you must define field name 'store_id'.
    – Msquare
    Feb 25, 2020 at 5:46
  • I got your point. In my case, I am using column name as 'store_id' only. Still, when I save any record with 'All Store Views' value, it shows blank in grid. For specific store selection, it shows proper store name. Any idea to resolve this? Feb 25, 2020 at 6:45
  • @patrickW store view not show so used above code. because you have to find where this field name used. and i don't have any Idea about it.
    – Msquare
    Feb 25, 2020 at 7:23
1

The answer from @Msquare is working fine, but you can apply the data cell renderer directly in the column xml. There is no need to create a custom js component for that.

<column name="store_ids" class="[vendorname]\[Modulename]\Ui\Component\Listing\Columns\Store">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
        <item name="sortable" xsi:type="boolean">false</item>
        <item name="label" xsi:type="string" translate="true">Store View</item>
        <item name="sortOrder" xsi:type="number">60</item>
        </item>
    </argument>
   <!-- Define the cell renderer here -->
   <settings>
       <bodyTmpl>ui/grid/cells/html</bodyTmpl>
   </settings>
   <!-- end renderer -->
</column>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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