0

I have created column name sku in custom grid.

Set href for sku values.

If i click the sku link,it does not go to desire page.

It go to desire page when i press right click and open the link in new tab.

In layout:

<column name="product_sku" class="Xxx\Stock\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>
            </argument>
        </column>

In Product file:

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->getIdBySku($item[$fieldName]);
                    $html = "<a  href='" . $this->context->getUrl('catalog/product/edit', ['id' => $productId]) . "' >";
                    $html .= $item[$fieldName];
                    $html .= "</a>";
                    $item[$fieldName] = $html;
                }
            }
        }

        return $dataSource;
    }
5
  • Can you please add the code for it? Mar 20 '18 at 13:43
  • Updated my post .
    – Mahi M
    Mar 20 '18 at 13:45
  • did you check that you're getting correct URL in HREF? Mar 20 '18 at 13:50
  • Yes...It go to desire page when i press right click and open the link in new tab.
    – Mahi M
    Mar 20 '18 at 13:51
  • i also add target='_blank' ..it does not work
    – Mahi M
    Mar 20 '18 at 13:52
0

Try this code

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->getIdBySku($item[$fieldName]);
                $html = "<a href='" . $this->context->getUrl('catalog/product/edit', ['id' => $productId]) . "' target='_blank' >";
                $html .= $item[$fieldName];
                $html .= "</a>";
                $item[$fieldName] = $html;
            }
        }
    }
    return $dataSource;
}
2
  • I already tried that...it does not work
    – Mahi M
    Mar 20 '18 at 13:55
  • It’s should work.. you might be adding ‘target=_blank’ in wrong way Mar 20 '18 at 14:03

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.