Magento 2.2.5 – How to Add Link to Admin Grid Values

admin-panelgridmagento-2.2.5

In Magento 2.2.5, I have custom admin grid. I want to add the link(product edit details or customer edit details) for the table data(id).
It should have the product edit link. How to achieve this.

enter image description here

Best Answer

I have just make Link and redirect it product detail page.

Please check my answer and change as per your need.

Create a actionsColumn in ui component xml like below :

<actionsColumn name="product_id" class="<<vendor>>\<<modulename>>\Ui\Component\Listing\Columns\ProductActions">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                    <item name="filter" xsi:type="string">text</item>                    
                    <item name="label" xsi:type="string" translate="true">Product Id</item>
                    <item name="sortOrder" xsi:type="number">30</item>
                </item>
            </argument>
        </actionsColumn>

After that create your renderer ProductActions.php

<?php
namespace <<Vendor>>\<<ModuleName>>\Ui\Component\Listing\Columns;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;

class ProductActions extends Column
{
    /**
     * @var UrlInterface
     */
    private $urlBuilder;

    /** Url Path */
    const PRODUCT_URL_PATH_EDIT = 'catalog/product/edit';

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        array $components = array(),
        UrlInterface $urlBuilder,
        array $data = array()) 
    {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->urlBuilder = $urlBuilder;
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return void
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $name = $this->getData('name');                
                if (isset($item['product_id'])) {                    
                    $item[$name] = html_entity_decode('<a href="'.$this->urlBuilder->getUrl(self::PRODUCT_URL_PATH_EDIT, ['id' => $item['product_id']]).'">'.$item['product_id'].'</a>');
                }
            }
        }
        return $dataSource;
    }
}

After that check.

Still you getting any issue let me know.

Related Topic