9

I want to get dropdown of edit & delete in action column of admin grid in magento2 as like mention in the image. How it is possible?

$this->addColumn(
    'delete',
    [
        'header' => __('Action'),
        'type' => 'action',
        'getter' => 'getId',
        'actions' => [
            [
                'caption' => __('Delete'),
                'url' => [
                    'base' => 'abc/*/delete'
                ],
                'field' => 'abc_id'
            ]
        ],
        'filter' => false,
        'sortable' => false,
        'index' => 'stores',
        'header_css_class' => 'col-action',
        'column_css_class' => 'col-action'
    ]
);

enter image description here

1 Answer 1

17

Here i give your answer please follow bellow steps

your_grid.xml

 <actionsColumn name="actions" class="[vendor]\[module]\Ui\Component\Listing\Column\Actions">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="indexField" xsi:type="string">id</item>
        </item>
    </argument>
</actionsColumn>

app/code/[vendor]/[module]/Ui/Component/Listing/Column/Actions.php

<?php

namespace [vendor]\[module]\Ui\Component\Listing\Column;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;


class Actions extends Column
{

    const URL_PATH_EDIT = '[router]/[controller]/edit';
    const URL_PATH_DELETE = '[router]/[controller]/delete';
    const URL_PATH_DETAILS = '[router]/[controller]/details';


    protected $urlBuilder;


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


    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item['primary_id'])) {
                    $item[$this->getData('name')] = [
                        'edit' => [
                            'href' => $this->urlBuilder->getUrl(
                                static::URL_PATH_EDIT,
                                [
                                    'id' => $item['primary_id']
                                ]
                            ),
                            'label' => __('Edit')
                        ],
                        'delete' => [
                            'href' => $this->urlBuilder->getUrl(
                                static::URL_PATH_DELETE,
                                [
                                    'id' => $item['primary_id']
                                ]
                            ),
                            'label' => __('Delete'),
                            'confirm' => [
                                'title' => __('Delete "${ $.$data.title }"'),
                                'message' => __('Are you sure you wan\'t to delete a "${ $.$data.title }" record?')
                            ]
                        ]
                    ];
                }
            }
        }

        return $dataSource;
    }
}

Below code for Magento 2.3.X

<?php

namespace [vendor]\[module]\Ui\Component\Listing\Column;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class Actions extends Column {

    /** Url path */
    const URL_PATH_EDIT = '[router]/[controller]/edit';
    const URL_PATH_DELETE = '[router]/[controller]/delete';
    const URL_PATH_VIEW = '[router]/[controller]/view';

    protected $actionUrlBuilder;
    protected $urlBuilder;

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

    public function prepareDataSource(array $dataSource) {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $name = $this->getData('name');
                if (isset($item['primary_id'])) {
                    $item[$name]['edit'] = [
                        'href' => $this->urlBuilder->getUrl(
                                self::URL_PATH_EDIT, [
                                    'primary_id' => $item['primary_id']
                                ]
                        ),
                        'label' => __('Edit')
                    ];
                    $item[$name]['delete'] = [
                        'href' => $this->urlBuilder->getUrl(
                                self::URL_PATH_DELETE, [
                                    'primary_id' => $item['primary_id']
                                ]
                        ),
                        'label' => __('Delete'),
                        'confirm' => [
                            'title' => __('Delete "${ $.$data.title }"'),
                            'message' => __('Are you sure you wan\'t to delete a "${ $.$data.title }" record?')
                        ]
                    ];
                    $item[$name]['preview'] = [
                        'href' => $this->urlBuilder->getUrl(
                                self::URL_PATH_VIEW, [
                                    'primary_id' => $item['primary_id']
                                ]
                        ),
                        'label' => __('View')
                    ];
                }
            }
        }

        return $dataSource;
    }

}
8
  • 1
    Is it possible for grid.php?
    – Dhrumin
    Commented Jan 1, 2019 at 8:27
  • 1
    Not working in magento 2.3.1 Any help?
    – Avesh Naik
    Commented May 6, 2019 at 8:39
  • Yes it is not working in my magento 2.3.2 also. Is there any solution? Commented Aug 20, 2019 at 16:09
  • Please check updated answer for magento 2.3.X @AveshNaik Commented Aug 21, 2019 at 8:27
  • 2
    Please check updated answer for magento 2.3.X @KowsiganAtsayam Commented Aug 21, 2019 at 8:32

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.