export-excel.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <!-- $t is vue-i18n global function to translate lang -->
  3. <div class="app-container">
  4. <div>
  5. <FilenameOption v-model="filename" />
  6. <AutoWidthOption v-model="autoWidth" />
  7. <BookTypeOption v-model="bookType" />
  8. <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-document" @click="handleDownload">
  9. {{ $t('excel.export') }} Excel
  10. </el-button>
  11. <a href="https://panjiachen.github.io/vue-element-admin-site/feature/component/excel.html" target="_blank" style="margin-left:15px;">
  12. <el-tag type="info">Documentation</el-tag>
  13. </a>
  14. </div>
  15. <el-table v-loading="listLoading" :data="list" element-loading-text="Loading..." border fit highlight-current-row>
  16. <el-table-column align="center" label="Id" width="95">
  17. <template slot-scope="scope">
  18. {{ scope.$index }}
  19. </template>
  20. </el-table-column>
  21. <el-table-column label="Title">
  22. <template slot-scope="scope">
  23. {{ scope.row.title }}
  24. </template>
  25. </el-table-column>
  26. <el-table-column label="Author" width="110" align="center">
  27. <template slot-scope="scope">
  28. <el-tag>{{ scope.row.author }}</el-tag>
  29. </template>
  30. </el-table-column>
  31. <el-table-column label="Readings" width="115" align="center">
  32. <template slot-scope="scope">
  33. {{ scope.row.pageviews }}
  34. </template>
  35. </el-table-column>
  36. <el-table-column align="center" label="Date" width="220">
  37. <template slot-scope="scope">
  38. <i class="el-icon-time" />
  39. <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
  40. </template>
  41. </el-table-column>
  42. </el-table>
  43. </div>
  44. </template>
  45. <script>
  46. import { fetchList } from '@/api/article'
  47. import { parseTime } from '@/utils'
  48. // options components
  49. import FilenameOption from './components/FilenameOption'
  50. import AutoWidthOption from './components/AutoWidthOption'
  51. import BookTypeOption from './components/BookTypeOption'
  52. export default {
  53. name: 'ExportExcel',
  54. components: { FilenameOption, AutoWidthOption, BookTypeOption },
  55. data() {
  56. return {
  57. list: null,
  58. listLoading: true,
  59. downloadLoading: false,
  60. filename: '',
  61. autoWidth: true,
  62. bookType: 'xlsx'
  63. }
  64. },
  65. created() {
  66. this.fetchData()
  67. },
  68. methods: {
  69. fetchData() {
  70. this.listLoading = true
  71. fetchList().then(response => {
  72. this.list = response.data.items
  73. this.listLoading = false
  74. })
  75. },
  76. handleDownload() {
  77. this.downloadLoading = true
  78. import('@/vendor/Export2Excel').then(excel => {
  79. const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
  80. const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
  81. const list = this.list
  82. const data = this.formatJson(filterVal, list)
  83. excel.export_json_to_excel({
  84. header: tHeader,
  85. data,
  86. filename: this.filename,
  87. autoWidth: this.autoWidth,
  88. bookType: this.bookType
  89. })
  90. this.downloadLoading = false
  91. })
  92. },
  93. formatJson(filterVal, jsonData) {
  94. return jsonData.map(v => filterVal.map(j => {
  95. if (j === 'timestamp') {
  96. return parseTime(v[j])
  97. } else {
  98. return v[j]
  99. }
  100. }))
  101. }
  102. }
  103. }
  104. </script>
  105. <style>
  106. .radio-label {
  107. font-size: 14px;
  108. color: #606266;
  109. line-height: 40px;
  110. padding: 0 12px 0 30px;
  111. }
  112. </style>