orocrm - OroPlatform:网格行上的自定义操作
问题描述
语境
我目前正在处理 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 的方法时,我遇到了错误。
当我在经典控制器中使用我的代码时,它可以工作。似乎这与需要 JSON 响应作为返回类型的 ajax 操作有关。
解决方案
我终于找到了解决此问题的解决方案。我确信这不是最优雅的方式,但是......它有效。
这是我所做的:
- 我更改了自定义扩展操作类
<?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 脚本,那将非常有趣。
推荐阅读
- python - 为不同地块之间的线条保持相同的颜色顺序?我丢了一行,颜色现在重新排列
- javascript - 为什么状态为假时此按钮不显示?
- node.js - 为什么说 show 的属性未定义?
- javascript - 如何防止具有绑定数据属性的HTML对象元素在“三星互联网”浏览器全屏后刷新
- javascript - StencilJS 渲染嵌套组件
- c# - 获取一个 SSIS C# 脚本组件以通过 OleDb 从 Excel 中读取:
- r - 计算文件数的平均值会导致“FUN(X[[i]], ...) 中的错误:仅在具有所有数字变量的数据帧上定义”
- node.js - 如何从猫鼬的两个集合中查询和获取文档
- php - 在 WordPress 中插入自定义排序帖子代码的位置
- firebase - 当我将默认的 Firebase 托管域替换为新域时,当前的 Firebase 分析数据会发生什么情况?