3

I'm trying to override the Export button of the Admin Orders Grid, to add another custom CSV export with fixed columns

The goal for the custom option is to always export the same columns, without changing the default behavior of CSV and Excel XML filtered columns. Something to do with the dataProvider I guess

Code:

../view/adminhtml/ui_component/sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">

    <listingToolbar name="listing_top">
        <exportButton class="Magento\Ui\Component\ExportButton" name="export_button">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">sales_order_grid.sales_order_grid.sales_order_columns.ids</item>

                    <item name="options" xsi:type="array">
                        <item name="cvs_test" xsi:type="array">
                            <item name="value" xsi:type="string">csv_custom</item>
                            <item name="label" xsi:type="string" translate="true">CSV_CUSTOM</item>
                            <item name="url" xsi:type="string">mui/export/gridToCsv</item>
                        </item>
                    </item>
                </item>

            </argument>
        </exportButton>
    </listingToolbar>
</listing>
1

After some searching, solved by following this solution and adapted to the CSV format: Magento2: Export grid data in xls format. Not sure if it is the right way but it just works.

For reference, this is code that I modified to have fixed exported columns:

Namespace\Module\Model\Export\MetadataProviderCustomCsv.php

[...]
protected function getColumns(UiComponentInterface $component)
    {
        [...]

        // Define the columns to export
        $baseColumn = reset($this->columns[$component->getName()]);

        $columns = ["entity_id"]; // put here your columns to export
        $columnsComponent = [];

        foreach ($columns as $columnKey) {
            $column = clone($baseColumn);
            $column->setData("name", $columnKey);
            $columnsComponent[$columnKey] = $column;
        }

        $this->columns[$component->getName()] = $columnsComponent;
        return $this->columns[$component->getName()];
    }

    /**
     * Retrieve Headers row array for Export
     *
     * @param UiComponentInterface $component
     * @return string[]
     */
    public function getHeaders(UiComponentInterface $component)
    {
        return ['Entity ID']; // Labels
    }
[...]

Pros: flexible export, it is also possible to easily join other entities, like the order product

Cons: it requires at least one column selected in the filter but I'm sure there is a way to create a column without cloning it

Your Answer

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

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