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

时间:2018-03-29 12:40:53

标签: php magento magento2 magento2.1

我正在尝试将自定义网格数据导出到csv文件。它在所有网格数据情况下工作正常,但是当我选择行并尝试导出它时导出所有网格数据而不是特定记录。 Working with Arrays, Lists and Sets 我使用下面的代码导出网格数据。

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
        );
    }
}

对于网格生成,我在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();
}

我遗失了什么吗?

1 个答案:

答案 0 :(得分:0)

是的,我认为您是在错误布局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>