OroPlatform:对网格行的自定义操作

OroPlatform: Custom action on the grid rows

上下文

我目前正在处理一个 OroPlatform 项目,我需要添加一个操作来从 OroPlatform 网格下载文件:

这是我已经完成的:

# datagrids.yml
  business-unit-grid:
    properties:
      getpdf_link:
        type: url
        route: baltimore_action_pdf
        params:
          - id
    actions:
      getpdf:
        type: getpdf
        label: "Export garanties"
        data_identifier: u.id
        entity_name: Oro\Bundle\OrganizationBundle\Entity\BusinessUnit
        icon: file
        link: getpdf_link

<?php

namespace Baltimore\Bundle\AppBundle\Extension\Action\Actions;

use Oro\Bundle\DataGridBundle\Extension\Action\Actions\AjaxAction;

class GetPdfAction extends AjaxAction
{
    /**
     * @var array
     */
    protected $requiredOptions = ['entity_name', 'data_identifier'];

    public function getOptions()
    {
        $options = parent::getOptions();
        $options['frontend_type'] = 'getpdf';
        if (empty($options['frontend_handle'])) {
            $options['frontend_handle'] = 'getpdf';
        }
        return $options;
    }
}
<?php

namespace Baltimore\Bundle\AppBundle\Controller\Actions;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

/**
 * @Route("/action")
 */
class ActionController extends Controller
{
    /**
     * @Route("/pdfexport/{id}", requirements={"id"="\d+"}, name="baltimore_action_pdf", methods={"GET", "POST"})
     */
    public function actionPdf(Request $request)
    {
        //dump($request->get('id'));

        $pdfOptions = new Options();
        $pdfOptions->set('defaultFont', 'Arial');

        // Instantiate Dompdf with our options
        $dompdf = new Dompdf($pdfOptions);

        // Retrieve the HTML generated in our twig file
        $html = $this->renderView('BaltimoreAppBundle:pdf:mypdf.html.twig', [
            'title' => "Welcome to our PDF Test"
        ]);

        // Load HTML to Dompdf
        $dompdf->loadHtml($html);

        // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
        $dompdf->setPaper('A4', 'portrait');

        // Render the HTML as PDF
        $dompdf->render();

        // Output the generated PDF to Browser (force download)
        $dompdf->stream("mypdf.pdf", [
            "Attachment" => true
        ]);

        exit;
    }
}

为了确保错误不是来自我的控制器,我在经典控制器中创建了相同的方法并且这个方法有效。

    /**
     * @Route("/download", name="app_vehicule_download")
     */
    public function downloadAction()
    {
        // Configure Dompdf according to your needs
        $pdfOptions = new Options();
        $pdfOptions->set('defaultFont', 'Arial');

        // Instantiate Dompdf with our options
        $dompdf = new Dompdf($pdfOptions);

        // Retrieve the HTML generated in our twig file
        $html = $this->renderView('BaltimoreAppBundle:pdf:mypdf.html.twig', [
            'title' => "Welcome to our PDF Test"
        ]);

        // Load HTML to Dompdf
        $dompdf->loadHtml($html);

        // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
        $dompdf->setPaper('A4', 'portrait');

        // Render the HTML as PDF
        $dompdf->render();

        // Output the generated PDF to Browser (force download)
        $dompdf->stream("mypdf.pdf", [
            "Attachment" => true
        ]);

        exit;
    }

问题

一切正常,自定义按钮在网格中可用,我可以发送 JSON 响应。但是,当我想创建一个下载 PDF 的方法时出现错误。

当我在经典控制器中使用我的代码时,它可以正常工作。这似乎与 ajax 操作有关,该操作需要 JSON 响应作为 return 类型..

我终于找到了解决这个问题的方法。我敢肯定这不是最优雅的方法,但是..它有效。

这是我所做的:

  • 我更改自定义扩展操作class
<?php

namespace Baltimore\Bundle\AppBundle\Extension\Action\Actions;

use Oro\Bundle\DataGridBundle\Extension\Action\ActionConfiguration;
use Oro\Bundle\DataGridBundle\Extension\Action\Actions\AbstractAction;

class GetPdfAction extends AbstractAction
{
    /**
     * @var array
     */
    protected $requiredOptions = ['entity_name', 'data_identifier', 'link'];

    /**
     * @param ActionConfiguration $options
     */
    public function setOptions(ActionConfiguration $options)
    {
        parent::setOptions($options);
    }
}
  • 然后,我把我的自定义动作对应的JS动作改一下
define([
    'oro/datagrid/action/model-action'
], function(ModelAction) {
    'use strict';

    /**
     * GetPDF action
     *
     * @export  oro/datagrid/action/getpdf-action
     * @class   oro.datagrid.action.GetPdfAction
     * @extends oro.datagrid.action.ModelAction
     */
    const GetPdfAction = ModelAction.extend({

        /**
         * @inheritDoc
         */
        constructor: function GetPdfAction(options) {
            GetPdfAction.__super__.constructor.call(this, options);
        },

        /**
         * Execute download file
         */
        execute: function() {
            this.downloadFile(this.messages);
        },

        /**
         * PDF download
         */
        downloadFile: function() {
            const host = window.location.protocol + '//' + window.location.host;
            const link = document.createElement('a'), filename = 'file.pdf';

            link.href = host + this.getLink();
            link.download = filename;
            link.click()
        }
    });

    return GetPdfAction;
});

如果有人知道如何改进JS脚本,那将是非常有趣的。