How can I add the Store view selector in my custom admin grid module![enter image description here][1]
I added this in my layout now its comming in my grid now to save store wise grid information in to database
Thanks in advance
2 Answers 2
Essentially you need to add the xml for your layout file, e.g.:
<block type="adminhtml/store_switcher" name="store_switcher" as="store_switcher">
<action method="setUseConfirm">
<params>0</params>
</action>
</block>
Then the usual getChildHtml in template:
<?php echo $this->getChildHtml('store_switcher');?>
Without knowing the specifics of your module and layout xml it is hard to say how that works out for you in the backend. However, see this question where @Marius explains how to pick up the store id:
-
Thanks for reply just i did add in layout but how to save grid information store wiseMagento AJ– Magento AJ2015年03月02日 13:38:31 +00:00Commented Mar 2, 2015 at 13:38
-
Follow the link.Henry's Cat– Henry's Cat2015年03月02日 14:10:06 +00:00Commented Mar 2, 2015 at 14:10
-
what i can i see on that link ?Magento AJ– Magento AJ2015年03月02日 14:25:30 +00:00Commented Mar 2, 2015 at 14:25
-
You did visit: magento.stackexchange.com/a/17190/12662 ? See this
$storeId = $this->getRequest()->getParam('store', 0); $model->setData($data)->setStoreId($storeId)->setId($this->getRequest()->getParam('id'));Henry's Cat– Henry's Cat2015年03月02日 14:59:07 +00:00Commented Mar 2, 2015 at 14:59
For versions above 2.3.0
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.
Reference link is working to add store switcher in your custom admin page