简体   繁体   English

如何将选定的行导出到magento 2网格中的csv?

[英]How to export selected rows to csv in magento 2 grid?

I am trying to export the custom grid data to csv file. 我正在尝试将自定义网格数据导出到csv文件。 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. 它在所有网格数据情况下都能正常工作,但是当我选择行并尝试导出时,将导出所有网格数据而不是特定记录。 在此处输入图片说明 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 对于网格生成,我在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? 我缺少什么吗?

Yes I think you are mising the layout xml file : yourmodule_yourcontroller_exportcsv.xml 是的,我认为您正在误认为布局xml文件:yourmodule_yourcontroller_exportcsv.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="yourmodule_yourcontroller_grid"/>
</page>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM