Magento – How to add a link in a grid column if the records are not found

magento-2.0magento-2.0.7magento2

I created a custom module.In grid if data is available then in action column edit option is there. If there is no data, then no edit option is displayed. So, I cannot enter into edit page if no data is available.
I cant use add new entry button. My requirement is to get the link in a grid column. I tried using a renderer but no luck.

namespace Vendor\Module\Block\Adminhtml\Data\Grid\Renderer;
class Link extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
public function render(\Magento\Framework\DataObject $row)
{
    $category = $row->getDataAllowed();
    if (empty($category))
    {
        $value =  $row->getData($this->getColumn()->getIndex());
        return '<a href="http://www.google.com">New'.$value.'</a>';
    }
}
}

and in customgrid.php I mention renderer also.

  $this->addColumn('action', array(
        'header' => __('Action'),
        'width' => '100',
        'type' => 'action',
        'getter' => 'getId',
        'renderer' => 'Vendor\Module\Block\Adminhtml\Data\Grid\Renderer\Link',
        'actions' => array(
            array(
                'caption' => __('Edit'),
                'url' => array('base' => 'vendor_module/Data/edit'),
                'field' => 'id'
            )
        ), 
        'filter' => false,
        'sortable' => false,
        'index' => 'stores',
        'is_system' => true,
    ));

The output should come like this
enter image description here

Best Answer

In your Grid class add this method:

public function getEmptyText()
{
    $url = $this->getUrl('module/controller/new')
    return '<a href="'.$url.'">'.__('New record').'</a>';
}

replace module and controller with the real values from your module.

Related Topic