2

I have created a custom report,

Used below code in my ui_component listing file.

<column name="custom_tax_code" class="Vendor\Module\Ui\Component\Listing\Column\Quantity">
 <argument name="data" xsi:type="array">
  <item name="config" xsi:type="array">
  <item name="filter" xsi:type="string">false</item>
  <item name="label" xsi:type="string" translate="true">Tax Code</item>
 </item>
</argument>
</column>

<column name="shipping_amount_custom" class="Vendor\Module\Ui\Component\Listing\Column\Quantity" >
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">false</item>
                <item name="label" xsi:type="string" translate="true">Shipping Amount</item>
            </item>
        </argument>
    </column>

Then in Quantity.php

 class Quantity extends \Magento\Ui\Component\Listing\Columns\Column {

  public function prepareDataSource(array $dataSource) {
    if (isset($dataSource['data']['items'])) {       
     foreach ($dataSource['data']['items'] as & $item) {

     $item['custom_tax_code'] = $taxcode; // used some logic to get this value
     $item['shipping_amount_custom'] = $lastItemShippingAmt;// used some logic to get this value
     }
  }
   return $dataSource;
  }
}

This code is working fine and it is showing properly in admin Grid, But when exported to CSV, values for these 2 columns are not showing. likewise i have many other custom columns. posted only 2 here.

How to resolve this issue? Can anyone help me out please. Thanks

1

Magento doesn't render the custom fields on export.

https://github.com/magento/magento2/issues/14014

You should add some specific customization for export. I suppose in case dynamic values you can add plugins for these methods:

https://github.com/magento/magento2/blob/06dbbc86572e110ecb60c52e8f54a8a144262d56/app/code/Magento/Ui/Model/Export/ConvertToCsv.php#L64-L103

https://github.com/magento/magento2/blob/06dbbc86572e110ecb60c52e8f54a8a144262d56/app/code/Magento/Ui/Model/Export/MetadataProvider.php#L154-L170

P.S. Otherwise you can try to add your needed data to collection directly, using custom attributes or join to needed table(s), so that the code $document->getCustomAttribute($column)->getValue() retrieve the correct value.

Here we can see how was solved the similar issue with empty fields in the exported CSV for customer grid -

\Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute()

| improve this answer | |
  • hi @Sergi.sss, I'm not cleared, where is the working code here? – jafar pinjar Oct 17 '19 at 10:24
  • @jafar pinjar If you want a working solution, you need to provide your extension code entirely. – sergei.sss Oct 17 '19 at 10:36
  • hi @sergei, can you pls share your email id? – jafar pinjar Oct 17 '19 at 11:23
  • You can temporary upload your code to github, it will be more convenient for me to change it. And more people will be able to help you. – sergei.sss Oct 17 '19 at 11:36
  • you can check the module here, github.com/pinjar/CustomReport – jafar pinjar Oct 18 '19 at 5:59
0

I crate a Admin Grid using Ui-Component.

You want all Data you have create a Custom Export Button that export your all data.

edit in your Ui-Component file where Admin Grid Show

[vendor_name] \ [module_name] \view\adminhtml\ui_component

Add this code to add Button into Admin Grid

Ui-ComponentFIleName.xml

<item name="buttons" xsi:type="array">

    <item name="import" xsi:type="array">
        <item name="name" xsi:type="string">import</item>
        <item name="label" xsi:type="string" translate="true">Import</item>
        <item name="class" xsi:type="string">secondary</item>
        <item name="url" xsi:type="string">*/*/importdata</item>
        <item name="sortOrder" xsi:type="number">20</item>
    </item>

    <item name="export" xsi:type="array">
        <item name="name" xsi:type="string">export</item>
        <item name="label" xsi:type="string" translate="true">Export</item>
        <item name="class" xsi:type="string">secondary</item>
        <item name="url" xsi:type="string">*/*/exportdata</item>
        <item name="sortOrder" xsi:type="number">30</item>
    </item>

</item>

Now create a File that Export or Import Data. File Name must be same as you define in Ui-Component file

  <item name="url" xsi:type="string">*/*/exportdata</item>

  <item name="url" xsi:type="string">*/*/importdata</item>

File path must be like this

[vendor_name] \ [module_name] \ Controller \ Adminhtml \ [Controller_name] \ Exportdata.php

Exportdata.php

<?php

namespace [vendor_name]\[module_name]\Controller\Adminhtml\[Controller_name];

use Magento\Framework\App\Filesystem\DirectoryList;


class Exportdata extends \Magento\Backend\App\Action
{
    protected $uploaderFactory;

    protected $_locationFactory; 

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\Filesystem $filesystem,
        \[vendor_name]\[module_name]\Model\locatorFactory $locationFactory // This is returns Collaction of Data

    ) {
       parent::__construct($context);
       $this->_fileFactory = $fileFactory;
       $this->_locationFactory = $locationFactory;
       $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); // VAR Directory Path
       parent::__construct($context);
    }

    public function execute()
    {   
        $name = date('m-d-Y-H-i-s');
        $filepath = 'export/export-data-' .$name. '.csv'; // at Directory path Create a Folder Export and FIle
        $this->directory->create('export');

        $stream = $this->directory->openFile($filepath, 'w+');
        $stream->lock();

        //column name dispay in your CSV 

        $columns = ['Col-1-name','Col-2-name','Col-3-name','Col-4-name','Col-5-name','Col-6-name','Col-7-name','Col-8-name','Col-9-name',];

            foreach ($columns as $column) 
            {
                $header[] = $column; //storecolumn in Header array
            }

        $stream->writeCsv($header);

        $location = $this->_locationFactory->create();
        $location_collection = $location->getCollection(); // get Collection of Table data 

        foreach($location_collection as $item){

            $itemData = [];

            // column name must same as in your Database Table 

            $itemData[] = $item->getData('col-1-name');
            $itemData[] = $item->getData('col-2-name');
            $itemData[] = $item->getData('col-3-name');
            $itemData[] = $item->getData('col-4-name');
            $itemData[] = $item->getData('col-5-name');
            $itemData[] = $item->getData('col-6-name');
            $itemData[] = $item->getData('col-7-name');
            $itemData[] = $item->getData('col-8-name');
            $itemData[] = $item->getData('col-9-name');

            $stream->writeCsv($itemData);

        }

        $content = [];
        $content['type'] = 'filename'; // must keep filename
        $content['value'] = $filepath;
        $content['rm'] = '1'; //remove csv from var folder

        $csvfilename = 'locator-import-'.$name.'.csv';
        return $this->_fileFactory->create($csvfilename, $content, DirectoryList::VAR_DIR);

    }


}

Now you can Click on the Export Button and See your .csv file Downloaded below.

I Hope This Helps You.

| improve this answer | |
  • If it's works for you VOTE UP – Msquare Oct 20 '19 at 7:54
  • hi @Msquare, I haven't tried this code, the code i used is almost similar, can you pls help me with that, github.com/pinjar/CustomReport – jafar pinjar Oct 21 '19 at 7:32
  • @jafarpinjar yes i will helps you. – Msquare Nov 10 '19 at 10:02
  • @jafarpinjar using Objectmanager is bad way. Please Follow above code. this way to get all Data.I will provide you a my Module to Export and Import Data.Give me a Email ID and say Hi – Msquare Nov 10 '19 at 10:23

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.