How to add custom column in admin grid using block in magento2.2.5. I dont know how to implement this task. Could you please help me? please find the below screenshot. https://prnt.sc/q267fw
1 Answer
You may add it from your Grid.php
file itself. For example, if the custom column name is Country
,
$this->addColumn(
'country',
[
'header' => __('Country'),
'index' => 'param_id', // this is optional parameter (if you want to pass one of the available values in collection)
'filter' => false,
'renderer' => 'YourVendor\YourModule\Block\Adminhtml\Grid\Column\NewsGridCountryName'
]
);
Now you have to add the class NewsGridCountryName.php
which will return the value. For example,
namespace YourVendor\YourModule\Block\Adminhtml\Grid\Column;
use \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
class NewsGridCountryName extends AbstractRenderer
{
protected $_objectmanager;
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectmanager
) {
$this->_objectmanager = $objectmanager;
}
public function render(\Magento\Framework\DataObject $row)
{
// $row contains the parameters available in the grid collection, like 'param_id'
$collection = $this->_objectmanager->create('YourVendor\YourModule\Model\ResourceModel\Country\Collection'); // get your own country collection here
$cell = $collection->getCountryName();
return $cell;
}
}
Try this way please, let me know if it works.