vxe-grid 在高级导出窗口中如何自定义模板,扩展更多功能

769 阅读2分钟

官网文档:vxetable.cn 高级导出弹窗中使用自定义模板

由于 vxe-table 本身就支持导出功能,所以使用自带的导出,但是自带的导出不能满足需求时,可能需要增加一些功能,这时候就可以使用自定义插槽来实现。vxe-table 的强大之处就是基本上任何功能都能自定义扩展。

说明

先来看一下默认的导出窗口

{DEC24D11-2AEA-4B4A-9778-E3F13024FC4C}.png

接下详细说明各个插槽的用法,非常灵活: top 顶部插槽 bottom 底部插槽 footer 尾部按钮插槽 default 自定义整个弹窗内容插槽

top 顶部插槽

{19B0971F-2700-4E72-96E6-064B0FE025E8}.png

<template>
  <div>
    <vxe-button @click="openEvent">高级导出</vxe-button>
    <vxe-grid ref="gridRef" v-bind="gridOptions">
      <template #exportTop>
        <vxe-notice-bar content="自定义顶部区域" style="background: palegreen;"></vxe-notice-bar>
      </template>
    </vxe-grid>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
const gridRef = ref()
const gridOptions = reactive({
  showFooter: true,
  exportConfig: {
    slots: {
      top: 'exportTop'
    }
  },
  columns: [
    { field: 'seq', type: 'seq', width: 70 },
    { field: 'name', title: 'Name' },
    { field: 'sex', title: 'Sex' },
    { field: 'age', title: 'Age' }
  ],
  data: [
    { id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
    { id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
    { id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' }
  ],
  footerData: [
    { seq: '合计', sex: '666', age: '999' },
    { seq: '平均', sex: '888', age: '333' }
  ]
})
const openEvent = () => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.openExport()
  }
}

</script>

bottom 底部插槽

{50F2DFDB-AFC5-45C0-AE04-61FC66B89161}.png

<template>
  <div>
    <vxe-button @click="openEvent">高级导出</vxe-button>
    <vxe-grid ref="gridRef" v-bind="gridOptions">
      <template #exportBottom>
        <vxe-notice-bar content="自定义底部区域" style="background: palegreen;"></vxe-notice-bar>
      </template>
    </vxe-grid>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
const gridRef = ref()
const gridOptions = reactive({
  showFooter: true,
  exportConfig: {
    slots: {
      bottom: 'exportBottom'
    }
  },
  columns: [
    { field: 'seq', type: 'seq', width: 70 },
    { field: 'name', title: 'Name' },
    { field: 'sex', title: 'Sex' },
    { field: 'age', title: 'Age' }
  ],
  data: [
    { id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
    { id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
    { id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' }
  ],
  footerData: [
    { seq: '合计', sex: '666', age: '999' },
    { seq: '平均', sex: '888', age: '333' }
  ]
})
const openEvent = () => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.openExport()
  }
}

</script>

footer 尾部按钮插槽

{9F4782C2-1719-4819-96B0-0AAEF36C26A2}.png

<template>
  <div>
    <vxe-button @click="openEvent">高级导出</vxe-button>
    <vxe-grid ref="gridRef" v-bind="gridOptions">
      <template #exportFooter="{ options }">
        <vxe-button>自定义按钮1</vxe-button>
        <vxe-button status="error">自定义按钮2</vxe-button>
        <vxe-button @click="cancelEvent">取消</vxe-button>
        <vxe-button status="primary" @click="customExport(options)">自定义导出</vxe-button>
      </template>
    </vxe-grid>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
const gridRef = ref()
const gridOptions = reactive({
  showFooter: true,
  exportConfig: {
    slots: {
      footer: 'exportFooter'
    }
  },
  columns: [
    { field: 'seq', type: 'seq', width: 70 },
    { field: 'name', title: 'Name' },
    { field: 'sex', title: 'Sex' },
    { field: 'age', title: 'Age' }
  ],
  data: [
    { id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
    { id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
    { id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' }
  ],
  footerData: [
    { seq: '合计', sex: '666', age: '999' },
    { seq: '平均', sex: '888', age: '333' }
  ]
})
const openEvent = () => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.openExport()
  }
}
const cancelEvent = () => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.closeExport()
  }
}
const customExport = (options) => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.exportData(options)
  }
}

</script>

default 自定义整个弹窗内容插槽

{DFAE4806-3B8E-450C-B481-6C32918A2C34}.png

<template>
  <div>
    <vxe-button @click="openEvent">高级导出</vxe-button>
    <vxe-grid ref="gridRef" v-bind="gridOptions">
      <template #exportDefault>
        <vxe-notice-bar content="自定义默认区域,可以灵活的定制自定义功能" style="background: palegreen;"></vxe-notice-bar>
      </template>
      <template #exportFooter="{ options }">
        <vxe-button @click="cancelEvent">取消</vxe-button>
        <vxe-button status="primary" @click="customExport(options)">自定义导出</vxe-button>
      </template>
    </vxe-grid>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
const gridRef = ref()
const gridOptions = reactive({
  showFooter: true,
  exportConfig: {
    slots: {
      default: 'exportDefault',
      footer: 'exportFooter'
    }
  },
  columns: [
    { field: 'seq', type: 'seq', width: 70 },
    { field: 'name', title: 'Name' },
    { field: 'sex', title: 'Sex' },
    { field: 'age', title: 'Age' }
  ],
  data: [
    { id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
    { id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
    { id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' }
  ],
  footerData: [
    { seq: '合计', sex: '666', age: '999' },
    { seq: '平均', sex: '888', age: '333' }
  ]
})
const openEvent = () => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.openExport()
  }
}
const cancelEvent = () => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.closeExport()
  }
}
const customExport = (options) => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.exportData(options)
  }
}

</script>

以上就是高级导出自定义插槽模板的用法,具体更复杂的就需要去看官网 API

github
gitee