Magento2 How to Create Custom Column Renderer in Admin Grid

Hello Guys,
Sometimes we need to modify the admin grid value as per our requirement and there we need the grid column renderer which helps to serve this purpose in Magento2.

Let's create a custom column renderer with following steps defined.

Step 1: Below is the sample class showing how we can use a custom admin grid column renderer through our custom grid renderer class.

<?php
namespace Magemeta\Renderer\Block\Adminhtml\Customer\Edit\Tab\Grid\Renderer;

class ColumnDisplay extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
    /**
    * Renders grid column
    * @param \Magento\Framework\DataObject $row
    * @return string
    */
    public function render(\Magento\Framework\DataObject $row)
    {
        return '<a href="'.$row->getUrl().'">' . $this->_getValue()   .'</a>';
    }
}

Step 2: Just add your custom renderer class in grid column method called "_prepareColumns()" like below code sample to make it work:


$this->addColumn(
'columnName',
[
    'header' => __('Column Name'),
    'index' => 'columnName',
    'filter' => false,
    'renderer' => 'Magemeta\Renderer\Block\Adminhtml\Customer\Edit\Tab\Grid\Renderer'
]
);

This is the way we can call our custom renderer in grid column. Hope this helps you guys!

 

Leave a Reply