1

I wish to add a column as link in sales order grid. While clicking the link, I wish to perform some logic,

I have reffered the below link for magento 1 which is based on layout grid add button inside custom column in sales order grid page

I need the same solution for magento 2

Many blogs are available to add custom column in sales order grid, but no luck

Thanks in advance

1

I have created one column as "sku" in a custom grid. It displays SKU as a link and also its redirect into view page.

In XML:

<column name="sku" class="XXX\Movements\Ui\Component\Listing\Columns\Product">
            <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">Sku</item>
                    <item name="sortable" xsi:type="boolean">false</item>
                </item>
            </argument>
        </column>

In Product.php:

<?php

namespace XXX\Movements\Ui\Component\Listing\Columns;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Catalog\Model\ProductFactory as ProductCollection;
use Magento\Ui\Component\Listing\Columns\Column;

class Product extends Column
{

    /**
     * @var ProductFactory
     */
    protected $_product;

    public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, ProductCollection $product, array $components = [], array $data = [])
    {
        $this->_product = $product;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item[$fieldName])) {
                    $productId = $this->_product->create()->getIdBySku($item[$fieldName]);
                    $html = "<a  href='" . $this->context->getUrl('catalog/product/edit', ['id' => $productId]) . "'>";
                    $html .= $item[$fieldName];
                    $html .= "</a>";
                    $item[$fieldName] = $html;
                }
            }
        }

        return $dataSource;
    }

}

It may be helpful for your scenario.

  • if you can add some info (explanation) in your answer it would be better. Please clarify as well in which xml file should the code above be added. – fmsthird Apr 15 at 6:45

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.