5

In my current Magento 2 module developement, I currently have the following code in my Grid.php file:

public function getRowUrl($row)
{
    if ($id = $row->getMageProd()) {
        return $this->getUrl(
            'catalog/product/edit',
            ['id' => $id]
        );
    } else {
        return parent::getRowUrl($row);
    }
}

Is there any way I can implement such thing via my ui_component file ?

4
  • why do you need a row url in the ui component? In the grid approach, the result of getGridUrl would determine the url where you are redirected when clicking on the row. The new ui component approach, when clicking a row, you either enter in the inline edit mode or nothing happens.
    – Marius
    Commented May 18, 2016 at 7:15
  • @Marius the idea is that entities are linked to products. Based on the mage_prod attribute, people should be able to access the product edit page by clicking the row. Commented May 18, 2016 at 7:17
  • you can add an actions column with an edit link at the end of the grid. Would that fit your needs?
    – Marius
    Commented May 18, 2016 at 7:21
  • @Marius yes when I asked the question, I felt like that was the only solution that could be done via uiComponents. Commented May 18, 2016 at 7:22

3 Answers 3

5

You can add a column at the end of the grid of type Action.
Something like this:
Inside the tag <columns> add this at the end

<actionsColumn name="actions" class="[Namespace]\[Module]\Ui\Component\Listing\Column\[Endity]Actions">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="indexField" xsi:type="string">[pk_id]</item>
            <item name="urlEntityParamName" xsi:type="string">[param_used_in_get]</item>
        </item>
    </argument>
</actionsColumn>

then create the class [Namespace]\[Module]\Ui\Component\Listing\Column\[Entity]Actions.

namespace [Namespace]\[Module]\Ui\Component\Listing\Column;
class [Entity]Actions extends \Magento\Ui\Component\Listing\Columns\Column
{
    const URL_PATH_EDIT = '[module]/[entity]/edit';

    /**
     * URL builder
     * 
     * @var \Magento\Framework\UrlInterface
     */
    protected $_urlBuilder;

    /**
     * constructor
     * 
     * @param \Magento\Framework\UrlInterface $urlBuilder
     * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
     * @param \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory
     * @param array $components
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\UrlInterface $urlBuilder,
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    )
    {
        $this->_urlBuilder = $urlBuilder;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }


    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item['mage_prod'])) {
                    $item[$this->getData('name')] = [
                        'edit' => [
                            'href' => $this->_urlBuilder->getUrl(
                                static::URL_PATH_EDIT,
                                [
                                    '[get_var]' => $item['[pk]_id']
                                ]
                            ),
                            'label' => __('Edit')
                        ],
                    ];
                }
            }
        }
        return $dataSource;
    }
}
4
  • I add same code in magento 2.1.7 but it's display edit link in grid i want grid row url without edit column.
    – himansu
    Commented Jul 7, 2017 at 17:13
  • @himansu the ui-components grids don't work as the classical grids (with blocks). Clicking on the row does not send you to the edit page of that entity.
    – Marius
    Commented Jul 7, 2017 at 17:50
  • @Marius what is the 'urlEntityParamName'? should that be the same as URL_PATH_EDIT? Commented Jan 5, 2018 at 15:55
  • @Marius it is possible - please see Product Grid (my answer below also) Commented Jun 23, 2022 at 5:52
2

Analog getRowAction actions column must exist and must have 'edit' action

<columns name="xxx_listing_columns">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="childDefaults" xsi:type="array">
                <item name="fieldAction" xsi:type="array">
                    <item name="provider" xsi:type="string">xxx_listing.xxx_listing.xxx_listing_columns.actions</item>
                    <item name="target" xsi:type="string">applyAction</item>
                    <item name="params" xsi:type="array">
                        <item name="0" xsi:type="string">edit</item>
                        <item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
                    </item>
                </item>
            </item>
        </item>
    </argument>
    <selectionsColumn name="ids">
0

You can see the product grid as example. So you need add columns.settings and actionsColumn for your component xml file

...
    <columns name="entity_columns">
        <settings>
            <childDefaults>
                <param name="fieldAction" xsi:type="array">
                    <item name="provider" xsi:type="string">component_name.component_name.entity_columns.actions</item>
                    <item name="target" xsi:type="string">applyAction</item>
                    <item name="params" xsi:type="array">
                        <item name="0" xsi:type="string">edit</item>
                        <item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
                    </item>
                </param>
            </childDefaults>
        </settings>
.....
       <actionsColumn name="actions" class="Vendor\News\Ui\Component\Listing\Column\NewsActions">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">news_id</item>
                    <item name="urlEntityParamName" xsi:type="string">news_id</item>
                </item>
            </argument>
        </actionsColumn>
    </columns>
...

Also you need add styles for cursor (in your _module.less)

    .vendor_name-index-index {
    .admin__data-grid-wrap {
        .data-row {
            cursor: pointer;
        }
      }
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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