1

I am trying to export the custom grid data to csv file. Its working fine in all grid data case, but when I am selecting the the rows and trying to export it exporting all grid data instead of specific records. enter image description here I am using below code to export the grid data.

class ExportCsv extends \Magento\Backend\App\Action
{   
    /**
     * Default file name
     */
    const FILENAME = 'registration_code.csv';

    /**
     * @var Magento\Framework\App\Response\Http\FileFactory
     */
    protected $_fileFactory;

    public function __construct(Context $context, FileFactory $FileFactory)
    {
        parent::__construct($context);
        $this->_fileFactory = $FileFactory;
    }

    public function execute()
    {
        $this->_view->loadLayout(false);

        $fileName = self::FILENAME;

        $content = $this->_view->getLayout()
            ->createBlock('Medical\Groups\Block\Adminhtml\Code\Grid')
            ->setSaveParametersInSession(true)
            ->getCsv();

        return $this->_fileFactory->create(
            $fileName,
            $content,
            DirectoryList::VAR_DIR
        );
    }
}

For grid generation I am using below code in Grid.php

protected function _prepareColumns()
{
    $this->addColumn(
        'action',
        [
            'header' => __('Action'),
            'type' => 'action',
            'getter' => 'getId',
            'actions' => [
                [
                    'caption' => __('Edit'),
                    'url' => [
                        'base' => '*/*/edit'
                    ],
                    'field' => 'id'
                ]
            ],
            'filter' => false,
            'sortable' => false,
            'index' => 'stores',
            'header_css_class' => 'col-action',
            'column_css_class' => 'col-action',
            'is_system'   => true
        ]
    );

    $this->addColumn(
        'registration_code',
        [
            'header' => __('Registration Code'),
            'type' => 'text',
            'index' => 'registration_code',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id'
        ]
    );

    $this->addColumn(
        'url',
        [
            'header' => __('Registration Url'),
            'type' => 'text',
            'index' => 'url',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id',
            'editor' => 'text'
        ]
    );

    $this->addColumn(
        'customer_email',
        [
            'header' => __('Customer Email'),
            'type' => 'text',
            'index' => 'customer_email',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id',
            //'renderer' => 'Medical\Groups\Block\Adminhtml\Code\Grid\Column\Renderer\Customer'
        ]
    );

    $this->addColumn(
        'used_customer',
        [
            'header' => __('Code used by Customer'),
            'type' => 'text',
            'index' => 'used_customer',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id'
        ]
    );       

    $this->addColumn(
        'customer_group_id',
        [
            'header' => __('Medical Group'),
            'type' => 'text',
            'index' => 'customer_group_id',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id',
            'renderer' => 'Medical\Groups\Block\Adminhtml\Code\Grid\Column\Renderer\Customergroups'
        ]
    );       

    $this->addColumn(
        'status',
        [
            'header' => __('Code Status'),
            'type' => 'text',
            'index' => 'status',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id',
            'renderer' => 'Medical\Groups\Block\Adminhtml\Code\Grid\Column\Renderer\Status'
        ]
    );       

    $this->addColumn(
        'expiry_date',
        [
            'header' => __('Code Expiry Date'),
            'type' => 'text',
            'index' => 'expiry_date',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id'
        ]
    );       

    $this->addExportType($this->getUrl('medicalgroups/*/exportCsv', ['_current' => true]),__('CSV'));

    $block = $this->getLayout()->getBlock('grid.bottom.links');
    if ($block) {
        $this->setChild('grid.bottom.links', $block);
    }

    return parent::_prepareColumns();
}

Is I am missing something?

Your Answer

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

Browse other questions tagged or ask your own question.