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 !!
Hi @Saitama1488
Can you update your question with code in the Image block code?
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.