0

How can I get the store_view into the admin grid? I have a second table to store the key and the store_id. how can I retrieve the data from that table to the grid? I have below, but it is showing empty in the gird. how can I get the data from that table store_id by using the key.

    /**
     * Check is single store mode
     */
    if (!$this->_storeManager->isSingleStoreMode()) {
        $this->addColumn(
            'store_id',
            [
                'header' => __('Store View'),
                'index' => 'store_id',
                'type' => 'store',
                'store_all' => true,
                'store_view' => true,
                'sortable' => false,
                'filter_condition_callback' => [$this, '_filterStoreCondition']
            ]
        );
    }

enter image description here

enter image description here

3
  • You can use the renderer to get the value for column but if you use renderer i don't how to apply the filters on that column if you want to use the renderer let me know i will guide you and secound solution is you can use the join in getCollection function.
    – Mehran
    Dec 11, 2021 at 7:55
  • can you guide me the first option? I can give up filtering of this column. I need to create a new file for this column to render? someting like \Block\Adminhtml\Grid\Column\websiteid.php?
    – Paul Fan
    Dec 13, 2021 at 16:13
  • i have posted the answer try it and let me know if you face any issue.
    – Mehran
    Dec 14, 2021 at 6:03

1 Answer 1

1

you need to add renderer path in column like this way.

$this->addColumn(
            'store_id',
            [
                'header' => __('Store View'),
                'index' => 'store_id',
                'filter' => false,
                'sortable' => false,
                'renderer' => 'Vendor\Module\Block\Adminhtml\Grid\Renderer\WebsiteId.php',
                'column_css_class' => 'col-type',
                'header_css_class' => 'col-type',
            ]
        );

And then create WebsiteId.php

WebsiteId.php

<?php

namespace Vendor\Module\Block\Adminhtml\Grid\Renderer;

use Magento\Backend\Block\Context;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\DataObject;
use Magento\Store\Model\StoreManagerInterface;
use Vendor/Module/Model/Modelfactory;

class WebsiteId extends AbstractRenderer
{
     /**
      * @var StoreManagerInterface
     */
     private $storeManager;
    protected $modelFactory; 
    public function __construct(
        Context $context,

        array $data = []
    )
    {
        $this->storeManager = $storeManager;
        $this->modelFacotry = modelFactory;
        parent::__construct($context, $data);
    }

    public function render(DataObject $row)
    {
       $slideId = $row->getSlideId();
       $model = $this->modelFactory->create();
       $storeIds = $model->getStores($slideId);
       //Here Stores id's are returned in the form of array.
       $storeName = [];
       foreach($storeIds as $items) {
          $storeName[] = $this->getStoreCodeById($items);
       }
       return implode(",", $storeName);
    }
     /**
      * Get Store code by id
      *
      * @param int $id
      *
      * @return string|null
      */
     public function getStoreCodeById(int $id): ?string
     {
        try {
            $storeData = $this->storeManager->getStore($id);
            $storeCode = (string)$storeData->getCode();
        } catch (LocalizedException $localizedException) {
            $storeCode = null;
            $this->logger->error($localizedException->getMessage());
        }
        return $storeCode;
     }
}

In your model class write this function

public function getStores($slideId){
 return $this->getResource()->getStores($slideId); 
}

In your resource model write this function

public function getStores($slideId){
        $connection = $this->getConnection();
        $select = $connection->select()->from(
        $this->getTable('your_table_name'),
        ['store_id']
        )->where(
            'slide_id = ?',
            (int) $slideId
        );
        return $connection->fetchCol($select);
  }

It returns you the array of matching records store ids.

7
  • thanks for the input, however, I still a bit confuse the render part by retrieve data from the table base on the key. I created another post and ask about it. I wish you can help to take a look what I miss. magento.stackexchange.com/questions/350861/…
    – Paul Fan
    Dec 14, 2021 at 20:07
  • try the updated answer!
    – Mehran
    Dec 15, 2021 at 5:14
  • Thank you for the update, I am not too quite sure where the model class and the resource model location should be? can you give me an example of those 2 location please? thanks!
    – Paul Fan
    Dec 16, 2021 at 8:34
  • the model class is in Vendor/Module/Model/modelclass.php and rescource model in at Vendor/Module/Model/RescourceModel/modelclass.php
    – Mehran
    Dec 16, 2021 at 9:16
  • for creating models etc follow this codilar.com/blog/…
    – Mehran
    Dec 16, 2021 at 9:20

Your Answer

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

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