How To Export Filtered Collection From Ui Grid Magento 2 => Almost all the grids are made using ui component in magento 2.Ui component has a feature to export collection data in csv and xml format.
By default it exports complete collection.
so if you want to export few records from the collection using a filter.
then i am going to explain how you can do this.
First of all you have set the class for export_button element, where you can filter the collection.
<exportButton name="export_button" class="Webkul\FilterCollectionTest\Ui\Component\ExportButton">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="selectProvider" xsi:type="string">filtercollectiontest_chat_listing.filtercollectiontest_chat_listing.filtercollectiontest_chat_columns.ids</item>
</item>
</argument>
</exportButton>
Then create the file on above mentioned location (Webkul\FilterCollectionTest\Ui\Component\ExportButton).
<?php
/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_FilterCollectionTest
* @author Webkul
* @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
namespace Webkul\FilterCollectionTest\Ui\Component;
/**
* Class ExportButton
*/
class ExportButton extends \Magento\Ui\Component\AbstractComponent
{
/**
* Component name
*/
const NAME = 'exportButton';
/**
* @var \Magento\Framework\UrlInterface
*/
protected $_urlBuilder;
/**
* @var \Magento\Framework\App\Request\Http
*/
protected $_request;
/**
* @param ContextInterface $context
* @param UrlInterface $urlBuilder
* @param \Magento\Framework\App\Request\Http $request
* @param array $components
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\UiComponent\ContextInterface $context,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Framework\App\Request\Http $request,
array $components = [],
array $data = []
) {
parent::__construct($context, $components, $data);
$this->_urlBuilder = $urlBuilder;
$this->_request = $request;
}
/**
* @return void
*/
public function prepare()
{
$customerId = $this->_request->getParam('customer_id');
if (isset($customerId)) {
$configData = $this->getData('config');
if (isset($configData['options'])) {
$configOptions = [];
foreach ($configData['options'] as $configOption) {
$configOption['url'] = $this->_urlBuilder->getUrl(
$configOption['url'],
["customer_id"=>$customerId]
);
$configOptions[] = $configOption;
}
$configData['options'] = $configOptions;
$this->setData('config', $configData);
}
}
parent::prepare();
}
}
So now when you do the export filter collection will be exported. here i have made filter of customer id.
Hope so it will help someone.
Thank you
2 comments
If you are facing any issue with the module please email us at [email protected]
Regards