介绍
平台默认的导出会打开弹窗,然后在弹窗内的视图选择是用模板方式导出还是选字段导出,但是有时候有部分场景希望点击导出动作后直接进入导出流程,导出指定的某个模板,我们可以通过覆写打开弹窗的动作来实现该功能。
本文档参考了 表格页自定义按钮如何获取搜索区域的查询条件
代码示例
以下代码大部分场景只需要修改其中excelTplName更换模板即可,另外如何想增加复用性,还可以将该属性改为从元数据的配置中获取。
import {
ActionType,
BaseActionWidget,
BaseElementListViewWidget,
BooleanHelper,
ClickResult,
Condition,
ExcelExportTask,
FILE_MODULE_NAME,
getSessionPath,
GraphqlHelper,
http,
IQueryPageResult,
ISort,
queryDslWidget,
ReturnPromise,
RuntimeServerAction,
ServerActionWidget,
SPI,
SubmitValue,
SYSTEM_MODULE_NAME,
translateValueByKey,
UrlHelper,
ViewActionTarget,
Widget
} from '@kunlun/dependencies';
import { OioNotification } from '@kunlun/vue-ui-antd';
@SPI.ClassFactory(
BaseActionWidget.Token({
actionType: [ActionType.View],
target: [ViewActionTarget.Dialog],
model: 'ys0328.k2.Model0000000453',
name: 'internalGotoListExportDialog'
})
)
export class DemoExportActionWidget extends ServerActionWidget {
/**
* excel导出模板名称
* @protected
*/
protected excelTplName = '演示抽屉跳转链接导出';
/**
* 导出任务的模型编码
* @protected
*/
protected exportTaskModel = 'excelExportTask';
/**
* 导出任务的方法
* @protected
*/
protected exportTaskFun = 'createExportTask';
/**
*
* 是否是同步导出
*/
@Widget.Reactive()
protected get syncExport() {
return BooleanHelper.isTrue(this.getDsl().sync) || !true;
}
protected async executeAction(action: RuntimeServerAction, parameters: SubmitValue): Promise<ClickResult> {
const workbookId = await this.getWorkbookId();
if (!workbookId) {
return false;
}
let task = {
workbookDefinition: {
id: workbookId
}
} as ExcelExportTask;
// 从平台内置的方法获取搜索区域的条件
const { condition } = this.getSearchRsqlAndQueryParams();
// 排序规则
let sortList = [] as ISort[];
const baseViewWidget = Widget.select(this.rootHandle);
const listViewWidget = queryDslWidget(baseViewWidget?.getChildrenInstance(), BaseElementListViewWidget);
if (listViewWidget) {
sortList = listViewWidget.sortList;
}
return this.export(task, condition, sortList);
}
protected getUploadBodyGql(id: string, condition: string | Condition, sortList: ISort[]) {
let queryData = '{}';
let rsql = condition;
if (condition instanceof Condition) {
const conditionBodyData = condition.getConditionBodyData();
if (conditionBodyData && Object.keys(conditionBodyData).length) {
queryData = GraphqlHelper.serializableObject(conditionBodyData);
}
rsql = condition.toString();
}
const sortListStr = (sortList || []).map((sort) => `{field: "${sort.sortField}", direction: ${sort.direction}}`).join(',');
const sorts = sortListStr ? `sort: {orders: [${sortListStr}]}` : '';
const conditionWrapper = condition
? `,conditionWrapper:{
rsql: "${rsql}",
queryData: "${queryData}"
${sorts}
}`
: '';
const mutation = `mutation {
${this.exportTaskModel}Mutation {
${this.exportTaskFun}(data: {sync: ${this.syncExport}, workbookDefinition: {id: ${id}}${conditionWrapper}}) {
id
name
state
}
}
}`;
return mutation;
}
protected async export(task: ExcelExportTask, condition: Condition | string, sortList: ISort[]) {
let gql = await this.getUploadBodyGql(task.workbookDefinition!.id, condition, sortList);
const variables = {
path: getSessionPath()
};
let responseResult = false;
if (this.syncExport) {
window.open(
UrlHelper.appendBasePath(
`/pamirs/${FILE_MODULE_NAME}?query=${encodeURIComponent(gql)}&variables=${encodeURIComponent(
JSON.stringify(variables)
)}`
),
'_blank'
);
responseResult = true;
} else {
const res = await http.mutate(SYSTEM_MODULE_NAME.FILE, gql, variables);
const data = res.data[`${this.exportTaskModel}Mutation`][this.exportTaskFun] as any;
responseResult = !!data.id;
}
if (responseResult) {
OioNotification.success(this.translate('kunlun.common.success'));
return true;
}
OioNotification.error(this.translate('kunlun.common.error'));
return false;
}
protected async getWorkbookId(): ReturnPromise<string | undefined> {
const model = this.model.model!;
const gql = `{
excelWorkbookDefinitionQuery {
queryPage(
page: {currentPage: 1, size: 1}
queryWrapper: {rsql: "displayName=='${this.excelTplName}' and model == '${model}' and dataStatus == 'ENABLED' and type =in= ('IMPORT_EXPORT','EXPORT')"}
) {
content {
displayName
id
}
totalPages
totalElements
}
}
}
`;
const variables = {
path: getSessionPath()
};
const res = await http.query(SYSTEM_MODULE_NAME.FILE, gql, variables);
const pageRes = res.data.excelWorkbookDefinitionQuery.queryPage as unknown as IQueryPageResult<any>;
const id = pageRes.content?.[0]?.id;
if (!id) {
OioNotification.warning(translateValueByKey('没有找到匹配的导出模板'));
}
return id;
}
}
Oinone社区 作者:nation原创文章,如若转载,请注明出处:https://doc.oinone.top/frontend/18221.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验