Share

Magento 2 Admin Custom Export Button Grid #codehacks

4 April, 2019

How to customize ExportButton Grid

Today we are going to view how to customize the export grid data in Magento2.

ExportButton Component is responsible for export grid data to specified data format (cvs, xml, and so on).

For this tutorial we are going to use a Module example called “StockAlert”

To enable ExportButton Component we need to add exportButton element with specified selectProvider item into listing configuration file (listing.xml), inside

… / app / code / [vendor] / [module] / view / adminhtml / ui_component / [vendor] _ [module] _ [referencia] _ listing.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
interactiv4_stockalert_report_listing.interactiv4_stockalert_report_listing.spinner_columns.ids



csv
CSV
interactiv4_stockalert/export/gridToCsv


xml
Excel XML

interactiv4_stockalert/export/gridToXml

Let`s do a quick code review:

Inside tag exist a tag where is setted the select Provider

1
interactiv4_stockalert_report_listing.interactiv4_stockalert_report_listing.spinner_columns.ids

In the next tag set the controllers where customize output format (CSV/XML/…)

1
2
3
4
5
6
7
8
csv
CSV
interactiv4_stockalert/export/gridToCsv


xml
Excel XML
interactiv4_stockalert/export/gridToXml

In tag

1
interactiv4_stockalert/export/gridToCsv

tag is called the controller, which implement the model “ConvertToCsv.php”

The controller … / app / code / [vendor] / [module] / Controller / Adminhtml / Export / GridToCsv.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!--?php &lt;br ?--> declare(strict_types=1);

/**
* @author Interactiv4 Team
* @copyright Copyright (c) Interactiv4 (https://www.interactiv4.com)
*/

namespace Interactiv4\StockAlert\Controller\Adminhtml\Export;

use Interactiv4\StockAlert\Model\Export\ConvertToCsv;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Response\Http\FileFactory;

/**
* Class Render
*/
class GridToCsv extends Action
{
/**
* @var ConvertToCsv
*/
protected $converter;

.
.
.

And the model … / app / code / [vendor] / [module] / Model / Export / ConvertToCsv.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!--?php &lt;br ?--> declare(strict_types=1);

/**
* @author Interactiv4 Team
* @copyright Copyright (c) Interactiv4 (https://www.interactiv4.com)
*/

namespace Interactiv4\StockAlert\Model\Export;

use Magento\Framework\Api\Search\DocumentInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Convert\ExcelFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Ui\Model\Export\MetadataProvider;
use Magento\Ui\Model\Export\SearchResultIteratorFactory;

/**
* Class ConvertToXls
*/
class ConvertToCsv
{
/**
* @var WriteInterface
*/
protected $directory;
.
.
.

Then you can implement into this model the code to customize the export format file.

Category
Author

Share

Subscribe to our newsletter

You may also like

We use third party cookies to improve our services and obtain statistical data of your browsing habits. If you continue browsing we consider that you accept its use. You can get more information at Privacy policy and cookies