4

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

11

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.

| improve this answer | |
2

Create a column in ui component xml:

  <column name="product_id" class="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">false</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/actions</item>
            <item name="template" xsi:type="string">ui/grid/cells/actions</item>

                <item name="indexField" xsi:type="string">entity_id</item>
                <item name="label" xsi:type="string" translate="true">Product Id</item>
                <item name="sortOrder" xsi:type="number">1000</item>
            </item>
        </argument>
    </column>

Copy and Update the UI Component class Magento\Catalog\Ui\Component\Listing\Columns\ProductActions and update according to your requirement. By default it shows Edit.

The drop-down action can easily be replaced by providing custom template. Use the default template ui/grid/cells/actions located in vendor/magento/module-ui/view/base/web/templates/grid/cells/actions.html Copy and update the template in same location of your module and update path in ui component.

| improve this answer | |
  • It is rendering like "Select" dropdown. But I want this like as a text and link. – Saravanan DS Sep 11 '18 at 11:32
  • I have updated the code. Update your template according to your need. – Milind Singh Sep 11 '18 at 13:19
0
public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item['post_id'])) {
                    $item[$this->getData('name')] = [
                        'edit' => [
                            'href' => $this->urlBuilder->getUrl(
                                static::URL_PATH_EDIT,
                                [
                                    'post_id' => $item['post_id']
                                ]
                            ),
                            'label' => __('Edit')
                        ],
                        'delete' => [
                            'href' => $this->urlBuilder->getUrl(
                                static::URL_PATH_DELETE,
                                [
                                    'post_id' => $item['post_id']
                                ]
                            ),
                            'label' => __('Delete'),
                            'confirm' => [
                                'title' => __('Delete "${ $.$data.name }"'),
                                'message' => __('Are you sure you wan\'t to delete a "${ $.$data.name }" record?')
                            ]
                        ]
                    ];
                }
            }
        }

        return $dataSource;
    }

Its worked.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.