cancel
Showing results for 
Search instead for 
Did you mean: 

custom grid

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

custom grid

How show image in custom grid on magento 2 ? 

I use renderer, but it does not work.

$this->addColumn(
'image',
[
'header' => __('Image'),
'index' => 'image',
'renderer' => "MyVendor\Gallery\Block\Adminhtml\Gallery\Grid\Renderer\Image",
'filter' => false

]);

Help please !! 

2 REPLIES 2

Re: custom grid

Hi @Saitama1488

 

Can you update your question with code in the Image block code?

---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: custom grid

Hello Saitama,

Check out the following code :

  

$this->addColumn(
'image',
[
'header' => __('Image'),
'index' => 'image',
'renderer' => "MyVendor\Gallery\Block\Adminhtml\Gallery\Grid\Renderer\Image",
'filter' => false
]);

Then I created a renderer block as below: 

  

namespace YourVendor\YourModule\Block\Adminhtml\Gallery\Grid\Renderer;

use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\Object;
use Magento\Store\Model\StoreManagerInterface;

class Image extends AbstractRenderer
{
    private $_storeManager;
    /**
     * @param \Magento\Backend\Block\Context $context
     * @param array $data
     */
    public function __construct(\Magento\Backend\Block\Context $context, StoreManagerInterface $storemanager, array $data = [])
    {
        $this->_storeManager = $storemanager;
        parent::__construct($context, $data);
        $this->_authorization = $context->getAuthorization();
    }
    /**
     * Renders grid column
     *
     * @param Object $row
     * @return  string
     */
    public function render(Object $row)
    {
        $mediaDirectory = $this->_storeManager->getStore()->getBaseUrl(
            \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
        );
        $imageUrl = $mediaDirectory.'/inquiry/images'.$this->_getValue($row);
        return '<img src="'.$imageUrl.'" width="50"/>';
    }
}

 Try once and reply if still have any issue. Hope this also work for you.