0

Magento 2 allow us to add inline edit functionality to grid created by UI_Component but I failed to figure out how to implement the same functionality for grid that created by Block.

My code for create Grid is like below:

<?php

namespace VendorName\Quote\Block\Adminhtml\Quote\Edit\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Grid\Extended;
use Magento\Backend\Helper\Data;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory;
use Magento\Quote\Model\Quote;
use VendorName\Quote\Block\Adminhtml\Grid\Renderer\RemoveFromQuoteAction;

class Items extends Extended
{
    protected $_coreRegistry = null;
    protected $collectionFactory;
    private $_collectionFactory;
    public function __construct(
        Context $context,
        Data $backendHelper,
        CollectionFactory $collectionFactory,
        Registry $coreRegistry,
        array $data = []
    ) {
        $this->_coreRegistry = $coreRegistry;
        $this->_collectionFactory = $collectionFactory;
        parent::__construct($context, $backendHelper, $data);
    }
    protected function _construct()
    {
        parent::_construct();

        /** @var Quote $quote */
        $quote = $this->_coreRegistry->registry('current_quote');
        $collection = $this->_collectionFactory
            ->getReport('vendor_name_quote_item_grid_data_source')
            ->addFieldToFilter('quote_id', $quote->getId())
            ->addFieldToSelect('item_id')
            ->addFieldToSelect('name')
            ->addFieldToSelect('sku')
            ->addFieldToSelect('discount_type')
            ->addFieldToSelect('discount_percent')
            ->addFieldToSelect('discount_amount')
            ->addFieldToSelect('qty')
            ->addFieldToSelect('is_additional')
            ->addFieldToSelect('price_incl_tax');
        $this->_collection = $collection;
        $this->setId('quote_items_grid');
        $this->setDefaultSort("created_at desc");
        $this->setUseAjax(true);
    }

    protected function _prepareColumns()
    {
        $this->addColumn(
            'item_id',
            [
                'header' => __('Item ID'),
                'index' => 'item_id'
            ]
        )
            ->addColumn(
                'name',
                [
                    'header' => __('Name'),
                    'index' => 'name',
                ]
            )
            ->addColumn(
                'sku',
                [
                    'header' => __('SKU'),
                    'index' => 'sku'
                ]
            )
            ->
            addColumn(
                'discount_type',
                [
                    'header' => __('Discount Type'),
                    'index' => 'discount_type'
                ]
            )
            ->
            addColumn(
                'discount_percent',
                [
                    'header' => __('Discount Percent'),
                    'index' => 'discount_percent'
                ]
            )
            ->addColumn(
                'discount_amount',
                [
                    'header' => __('Discount Amount'),
                    'index' => 'discount_amount'
                ]
            )
            ->addColumn(
                'qty',
                [
                    'header' => __('Quantity'),
                    'index' => 'qty'
                ]
            )
            ->addColumn(
                'price_incl_tax',
                [
                    'header' => __('Price (Incl Tax)'),
                    'index' => 'price_incl_tax'
                ]
            )
            ->addColumn(
                'action',
                [
                    'header' => ' ',
                    'filter' => false,
                    'sortable' => false,
                    'width' => '100px',
                    'renderer' => RemoveFromQuoteAction::class
                ]
            );
        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
        return "";
    }

    public function getGridUrl()
    {
        return $this->getUrl('quotation/quote/items', ['_current' => true]);
    }
}

How to allow to edit data inline ? Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.