Magento 2 – Admin Grid View Action

actiongridmagento2view

I have created admin grit using ui component. I'm trying to implement grid actions.the grid lists out my form details. how add view action to the grid
app/code/Dyode/Pricebeat/Controller/Adminhtml/Form/View.php

    <?php
 namespace Dyode\Pricebeat\Controller\Adminhtml\Form;

 class View extends \Dyode\Pricebeat\Controller\Adminhtml\Form
  {
/**
 * Backend session
 *
 * @var \Magento\Backend\Model\Session
 */
protected $backendSession;

/**
 * Page factory
 *
 * @var \Magento\Framework\View\Result\PageFactory
 */
protected $resultPageFactory;

/**
 * Result JSON factory
 *
 * @var \Magento\Framework\Controller\Result\JsonFactory
 */
protected $resultJsonFactory;

/**
 * constructor
 *
 * @param \Magento\Backend\Model\Session $backendSession
 * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
 * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
 * @param \Dyode\Pricebeat\Model\FormFactory $formFactory
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
 * @param \Magento\Backend\App\Action\Context $context
 */
public function __construct(
    \Magento\Backend\Model\Session $backendSession,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
    \Dyode\Pricebeat\Model\FormFactory $formFactory,
    \Magento\Framework\Registry $registry,
    \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
    \Magento\Backend\App\Action\Context $context
)
{
    $this->backendSession    = $backendSession;
    $this->resultPageFactory = $resultPageFactory;
    $this->resultJsonFactory = $resultJsonFactory;
    parent::__construct($formFactory, $registry, $resultRedirectFactory, $context);
}

/**
 * is action allowed
 *
 * @return bool
 */
protected function _isAllowed()
{
    return $this->_authorization->isAllowed('Dyode_Pricebeat::form');
}

/**
 * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect|\Magento\Framework\View\Result\Page
 */
public function execute()
{
    $id = $this->getRequest()->getParam('form_id');
    /** @var \Dyode\Pricebeat\Model\Form $form */
    $form = $this->initForm();
    /** @var \Magento\Backend\Model\View\Result\Page|\Magento\Framework\View\Result\Page $resultPage */
    $resultPage = $this->resultPageFactory->create();
    $resultPage->setActiveMenu('Dyode_Pricebeat::form');
    $resultPage->getConfig()->getTitle()->set(__('Pricebeat'));
    if ($id) {
        $form->load($id);
        if (!$form->getId()) {
            $this->messageManager->addError(__('This form no longer exists.'));
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath(
                'dyode_pricebeat/*/view',
                [
                    'form_id' => $form->getId(),
                    '_current' => true
                ]
            );
            return $resultRedirect;
        }
    }

    $title = $form->getId() ? $form->getTitle() : __('Pricebeat');
    $resultPage->getConfig()->getTitle()->prepend($title);
    $data = $this->backendSession->getData('dyode_dyode_form_data', true);
    if (!empty($data)) {
        $form->setData($data);
    }

    return $resultPage;
}
}

enter image description here

app/code/Dyode/Pricebeat/view/adminhtml/layout/dyode_pricebeat_form_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="Dyode\Pricebeat\Block\Adminhtml\Form\Edit" name="dyode_pricebeat_form_view"/>
    </referenceContainer>
    <referenceContainer name="left">
        <block class="Dyode\Pricebeat\Block\Adminhtml\Form\Edit\Tabs" name="dyode_pricebeat_form_tabs">
            <block class="Dyode\Pricebeat\Block\Adminhtml\Form\Edit\Tab\Form" name="dyode_pricebeat_form_edit_tab_form"/>
            <action method="addTab">
                <argument name="name" xsi:type="string">form</argument>
                <argument name="block" xsi:type="string">dyode_pricebeat_form_view_tab_form</argument>
            </action>
        </block>
    </referenceContainer>
</body>
</page>

Best Answer

Please add this in your ui component File.

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <actionsColumn name="actions" class="<Vendor>\<ModuleName>\Ui\Component\Listing\Columns\<YourActions>">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="resizeEnabled" xsi:type="boolean">false</item>
                <item name="resizeDefaultWidth" xsi:type="string">120</item>
                <item name="indexField" xsi:type="string">faq_id</item>
            </item>
        </argument>
    </actionsColumn>
    </columns>
</listing>

After that make actions file at given path \\Ui\Component\Listing\Columns\. .php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace <Vendor>\<ModuleName>\Ui\Component\Listing\Columns;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;
/**
 * Description of ProductActions
 *
 * @author dharmendra
 */
class <YourActions> extends Column
{
    /**
     * @var UrlInterface
     */
    private $urlBuilder;

    /** Url Path */
    const URL_PATH_EDIT = '<your url here like>catalog/product/edit';
    const URL_PATH_VIEW = '<your url here like>catalog/product/view';

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

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return void
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $name = $this->getData('name');
                if (isset($item['product_id'])) {
                    $item[$name]['edit'] = [
                        'href' => $this->urlBuilder->getUrl(self::URL_PATH_EDIT, ['product_id' => $item['product_id']]),
                        'label' => __('Edit')
                    ];
                    $item[$name]['delete'] = [
                        'href' => $this->urlBuilder->getUrl(self::URL_PATH_VIEW, ['product_id' => $item['product_id']]),
                        'label' => __('View')
                    ];
                }
            }
        }
        return $dataSource;
    }
}

Implement this you have to get action in your ui based grid.

Still you have any difficulty let me know.

Related Topic