2

I have create custom grid in customer information.
Create export functionality in custom admin grid without template file and ui component

Meetanshi\OrderHistory\view\adminhtml\layout\module_index_custom.xml

<?xml version="1.0"?>

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/layout_generic.xsd">
    <container name="root" label="Root">
        <block class="Vendor\Module\Block\Adminhtml\Edit\Tab\View\Demo" name="custom_tabs" />
    </container>
</layout>

Vendor\Module\Block\Adminhtml\Edit\Tab\View\Demo.php

<?php

namespace Vendor\Module\Block\Adminhtml\Edit\Tab\View;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Helper\Data;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Framework\Registry;

class Demo extends \Magento\Backend\Block\Widget\Grid\Extended
{

protected $coreRegistry;

protected $orderFactory;

public function __construct(
    Context $context,
    \Magento\Framework\App\Request\Http $request,
    Data $backendHelper,
    CollectionFactory $orderFactory,
    Registry $coreRegistry,
    array $data = []
) {
    $this->request = $request;
    $this->orderFactory = $orderFactory;
    $this->coreRegistry = $coreRegistry;
    parent::__construct($context, $backendHelper, $data);
}

protected function _construct()
{
    parent::_construct();
    $this->setId('custom_order_grid');
    $this->setUseAjax(true);
}

protected function _prepareCollection()
{
    $collection = $this->orderFactory->create()->addFieldToSelect(
        'increment_id'
    )->addFieldToSelect(
        'status'
    )->addFieldToSelect(
        'total_qty_ordered'
    )->addFieldToSelect(
        'base_discount_amount'
    )->addFieldToSelect(
        'subtotal'
    )->addFieldToSelect(
        'grand_total'
    )->addFieldToFilter(
        'customer_id',
        $this->_request->getParam('id')
    );

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $this->addColumn(
        'increment_id',
        [
            'header' => __('Order ID'),
            'sortable' => true,
            'index' => 'increment_id'
        ]
    );
    $this->addColumn(
        'status',
        [
            'header' => __('Status'),
            'index' => 'status'
        ]
    );

    return parent::_prepareColumns();
}

public function getGridUrl()
{
    return $this->getUrl('module/*/custom', ['_current' => true]);
}

}

1

1 Answer 1

2

Add the following line before return parent::_prepareColumns():

$this->addExportType('*/*/exportCsv', __('CSV'));

Create new controller for creating csv. Here is the sample:

<?php
namespace VendorName\ModuleName\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Vendor\Module\Block\Adminhtml\Edit\Tab\View\Demo;
use Magento\Framework\View\Result\PageFactory;

class ExportCsv extends Action
{
    /**
     * @var FileFactory
     */
    private $fileFactory;

    /**
     * @var PageFactory
     */
    private $resultPageFactory;

    /**
     * ExportCsv constructor.
     * @param Context $context
     * @param FileFactory $fileFactory
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        FileFactory $fileFactory,
        PageFactory $resultPageFactory
    ) {
        $this->fileFactory = $fileFactory;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $grid = $resultPage->getLayout()->createBlock(Demo::class);
        $fileName   = "export.csv";
        return $this->fileFactory->create($fileName, $grid->getCsvFile(), DirectoryList::VAR_DIR);
    }
}

You need to adjust with your module.

5
  • Console Error :- Failed to load resource: the server responded with a status of 401 () Jul 22, 2021 at 7:07
  • Where you got that error?
    – Sohel Rana
    Jul 23, 2021 at 3:22
  • When i export my grd data Jul 23, 2021 at 3:59
  • Adjust controller, you definitely made mistake there.
    – Sohel Rana
    Jul 23, 2021 at 4:30
  • I have resolve this error please upvote my question. Thanks! Jul 23, 2021 at 5:45

Your Answer

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

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