index.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <el-popover
  3. placement="top"
  4. width="300"
  5. v-model="visible">
  6. <p>导出日期选择</p>
  7. <m-form ref="exportRefs" label-width="60px" :items="exportItems" v-model="exportValues"></m-form>
  8. <div style="text-align: right; margin: 0">
  9. <el-button size="mini" @click="visible = false">取消</el-button>
  10. <el-button type="orange" size="mini" @click="onClick">导出</el-button>
  11. </div>
  12. <m-button slot="reference" size="mini" type="orange" icon="el-icon-download" :loading="exportLoading">导出</m-button>
  13. </el-popover>
  14. </template>
  15. <script>
  16. import { downloadFile } from '@/utils'
  17. export default {
  18. name: 'exportByDate',
  19. props: {
  20. searchValue: {
  21. type: String,
  22. default: 'month'
  23. },
  24. searchType: {
  25. type: String,
  26. default: 'month'
  27. },
  28. searchFormat: {
  29. type: String,
  30. default: 'yyyy-MM'
  31. },
  32. onExport: {
  33. type: Function,
  34. default: () => {}
  35. }
  36. },
  37. data () {
  38. return {
  39. visible: false,
  40. exportValues: {
  41. [this.searchValue]: null
  42. },
  43. exportLoading: false
  44. }
  45. },
  46. computed: {
  47. exportItems () {
  48. return [
  49. {
  50. label: '时间',
  51. type: 'datePicker',
  52. prop: this.searchValue,
  53. options: {
  54. size: 'mini',
  55. placeholder: '请选择时间',
  56. type: this.searchType,
  57. valueFormat: this.searchFormat
  58. },
  59. rules: [
  60. { required: true, message: '请选择时间', trigger: 'change' }
  61. ]
  62. }
  63. ]
  64. }
  65. },
  66. methods: {
  67. onClick () {
  68. this.$refs.exportRefs.validate(async valid => {
  69. if (!valid) {
  70. return
  71. }
  72. this.visible = false
  73. this.exportLoading = true
  74. try {
  75. const { data, name } = await this.onExport(this.exportValues)
  76. downloadFile(data, name)
  77. } catch (error) {
  78. this.$message.error(error)
  79. } finally {
  80. this.exportLoading = false
  81. }
  82. })
  83. }
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. </style>