Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] 请问 MaProTable 工具栏插件有没有内置导出表格数据功能? #528

Open
nighon opened this issue Jan 13, 2025 · 2 comments

Comments

@nighon
Copy link

nighon commented Jan 13, 2025

请问 MaProTable 工具栏插件(ToolbarPlugin)有没有内置导出表格数据功能?比如导出为 .csv, .xlsx 等格式。🙏

@kanyxmo
Copy link
Member

kanyxmo commented Jan 14, 2025

目前没有,可以自己添加,或者等后面官方开发插件

@Darknesser
Copy link

用到了导出xlsx,简单实现了一下,使用xlswriter

  • 在删除按钮旁边增加一个导出按钮
<template #toolbarLeft>
  <el-button
    v-auth="['trademark:order:delete']"
    type="danger"
    plain
    :disabled="selections.length < 1"
    @click="handleDelete"
  >
    {{ t('crud.delete') }}
  </el-button>
  <el-button
    v-auth="['trademark:order:export']"
    type="primary"
    plain
    @click="handleExport"
  >
    {{ t('biz.field.export') }}
  </el-button>
</template>
  • 搜索参数
// 搜索参数
const searchFormValues = ref({})

// 给MaProTable加一个search-submit
<MaProTable ref="proTableRef" :options="options" :schema="schema" @search-submit="handleSearchSubmit">

// 搜索参数赋值
function handleSearchSubmit(form: Record<string, any>): void {
  searchFormValues.value = form
}
  • 点击事件处理
function handleExport() {
  const response = exportFile(searchFormValues.value)
  response.then((res) => {
    if (!res || !(res instanceof Blob)) {
      msg.alertError('Export response invalid')
      return
    }
    const url = window.URL.createObjectURL(new Blob([res]))
    const link = document.createElement('a')
    link.href = url
    link.setAttribute('download', 'order.xlsx')
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }, err => msg.alertError(err),
  ).catch((err) => {
    msg.alertError(err)
  })
}
  • 调用接口
export function exportFile(data: TrademarkOrderSearchVo) {
  return useHttp().post('/admin/trademark-order/export', data, {
    responseType: 'blob',
    timeout: 3600,
  })
}
  • 接口实现,xlswriter使用就不展示了
#[Post(
    path: '/admin/trademark-order/export',
    operationId: 'trademarkOrderExport',
    summary: 'Export Trademark Order',
    security: [['Bearer' => [], 'ApiKey' => []]],
    tags: ['Trademark Manage']
)]
#[Permission(code: 'trademark:order:export')]
public function export(Request $request, Response $response): ResponseInterface
{
    [$filePath, $fileName] = $this->trademarkOrderService->export($request->all());
    return $response->download($filePath, $fileName);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants