4

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
    May 18 '16 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. May 18 '16 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
    May 18 '16 at 7:21
  • @Marius yes when I asked the question, I felt like that was the only solution that could be done via uiComponents. May 18 '16 at 7:22
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;
    }
}
3
  • 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
    Jul 7 '17 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
    Jul 7 '17 at 17:50
  • @Marius what is the 'urlEntityParamName'? should that be the same as URL_PATH_EDIT? Jan 5 '18 at 15:55
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">

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.