select-excel.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div class="app-container">
  3. <!-- $t is vue-i18n global function to translate lang -->
  4. <el-input v-model="filename" :placeholder="$t('excel.placeholder')" style="width:350px;" prefix-icon="el-icon-document" />
  5. <el-button :loading="downloadLoading" style="margin-bottom:20px" type="primary" icon="el-icon-document" @click="handleDownload">
  6. {{ $t('excel.selectedExport') }}
  7. </el-button>
  8. <a href="https://panjiachen.github.io/vue-element-admin-site/feature/component/excel.html" target="_blank" style="margin-left:15px;">
  9. <el-tag type="info">Documentation</el-tag>
  10. </a>
  11. <el-table
  12. ref="multipleTable"
  13. v-loading="listLoading"
  14. :data="list"
  15. element-loading-text="拼命加载中"
  16. border
  17. fit
  18. highlight-current-row
  19. @selection-change="handleSelectionChange"
  20. >
  21. <el-table-column type="selection" align="center" />
  22. <el-table-column align="center" label="Id" width="95">
  23. <template slot-scope="scope">
  24. {{ scope.$index }}
  25. </template>
  26. </el-table-column>
  27. <el-table-column label="Title">
  28. <template slot-scope="scope">
  29. {{ scope.row.title }}
  30. </template>
  31. </el-table-column>
  32. <el-table-column label="Author" width="110" align="center">
  33. <template slot-scope="scope">
  34. <el-tag>{{ scope.row.author }}</el-tag>
  35. </template>
  36. </el-table-column>
  37. <el-table-column label="Readings" width="115" align="center">
  38. <template slot-scope="scope">
  39. {{ scope.row.pageviews }}
  40. </template>
  41. </el-table-column>
  42. <el-table-column align="center" label="PDate" width="220">
  43. <template slot-scope="scope">
  44. <i class="el-icon-time" />
  45. <span>{{ scope.row.display_time }}</span>
  46. </template>
  47. </el-table-column>
  48. </el-table>
  49. </div>
  50. </template>
  51. <script>
  52. import { fetchList } from '@/api/article'
  53. export default {
  54. name: 'SelectExcel',
  55. data() {
  56. return {
  57. list: null,
  58. listLoading: true,
  59. multipleSelection: [],
  60. downloadLoading: false,
  61. filename: ''
  62. }
  63. },
  64. created() {
  65. this.fetchData()
  66. },
  67. methods: {
  68. fetchData() {
  69. this.listLoading = true
  70. fetchList(this.listQuery).then(response => {
  71. this.list = response.data.items
  72. this.listLoading = false
  73. })
  74. },
  75. handleSelectionChange(val) {
  76. this.multipleSelection = val
  77. },
  78. handleDownload() {
  79. if (this.multipleSelection.length) {
  80. this.downloadLoading = true
  81. import('@/vendor/Export2Excel').then(excel => {
  82. const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
  83. const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
  84. const list = this.multipleSelection
  85. const data = this.formatJson(filterVal, list)
  86. excel.export_json_to_excel({
  87. header: tHeader,
  88. data,
  89. filename: this.filename
  90. })
  91. this.$refs.multipleTable.clearSelection()
  92. this.downloadLoading = false
  93. })
  94. } else {
  95. this.$message({
  96. message: 'Please select at least one item',
  97. type: 'warning'
  98. })
  99. }
  100. },
  101. formatJson(filterVal, jsonData) {
  102. return jsonData.map(v => filterVal.map(j => v[j]))
  103. }
  104. }
  105. }
  106. </script>