drag-table.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div class="app-container">
  3. <!-- Note that row-key is necessary to get a correct row order. -->
  4. <el-table ref="dragTable" v-loading="listLoading" :data="list" row-key="id" border fit highlight-current-row style="width: 100%">
  5. <el-table-column align="center" label="ID" width="65">
  6. <template slot-scope="{row}">
  7. <span>{{ row.id }}</span>
  8. </template>
  9. </el-table-column>
  10. <el-table-column width="180px" align="center" label="Date">
  11. <template slot-scope="{row}">
  12. <span>{{ row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
  13. </template>
  14. </el-table-column>
  15. <el-table-column min-width="300px" label="Title">
  16. <template slot-scope="{row}">
  17. <span>{{ row.title }}</span>
  18. </template>
  19. </el-table-column>
  20. <el-table-column width="110px" align="center" label="Author">
  21. <template slot-scope="{row}">
  22. <span>{{ row.author }}</span>
  23. </template>
  24. </el-table-column>
  25. <el-table-column width="100px" label="Importance">
  26. <template slot-scope="{row}">
  27. <svg-icon v-for="n in + row.importance" :key="n" icon-class="star" class="icon-star" />
  28. </template>
  29. </el-table-column>
  30. <el-table-column align="center" label="Readings" width="95">
  31. <template slot-scope="{row}">
  32. <span>{{ row.pageviews }}</span>
  33. </template>
  34. </el-table-column>
  35. <el-table-column class-name="status-col" label="Status" width="110">
  36. <template slot-scope="{row}">
  37. <el-tag :type="row.status | statusFilter">
  38. {{ row.status }}
  39. </el-tag>
  40. </template>
  41. </el-table-column>
  42. <el-table-column align="center" label="Drag" width="80">
  43. <template slot-scope="{}">
  44. <svg-icon class="drag-handler" icon-class="drag" />
  45. </template>
  46. </el-table-column>
  47. </el-table>
  48. <!-- $t is vue-i18n global function to translate lang (lang in @/lang) -->
  49. <div class="show-d">
  50. <el-tag style="margin-right:12px;">{{ $t('table.dragTips1') }} :</el-tag> {{ oldList }}
  51. </div>
  52. <div class="show-d">
  53. <el-tag>{{ $t('table.dragTips2') }} :</el-tag> {{ newList }}
  54. </div>
  55. </div>
  56. </template>
  57. <script>
  58. import { fetchList } from '@/api/article'
  59. import Sortable from 'sortablejs'
  60. export default {
  61. name: 'DragTable',
  62. filters: {
  63. statusFilter(status) {
  64. const statusMap = {
  65. published: 'success',
  66. draft: 'info',
  67. deleted: 'danger'
  68. }
  69. return statusMap[status]
  70. }
  71. },
  72. data() {
  73. return {
  74. list: null,
  75. total: null,
  76. listLoading: true,
  77. listQuery: {
  78. page: 1,
  79. limit: 10
  80. },
  81. sortable: null,
  82. oldList: [],
  83. newList: []
  84. }
  85. },
  86. created() {
  87. this.getList()
  88. },
  89. methods: {
  90. async getList() {
  91. this.listLoading = true
  92. const { data } = await fetchList(this.listQuery)
  93. this.list = data.items
  94. this.total = data.total
  95. this.listLoading = false
  96. this.oldList = this.list.map(v => v.id)
  97. this.newList = this.oldList.slice()
  98. this.$nextTick(() => {
  99. this.setSort()
  100. })
  101. },
  102. setSort() {
  103. const el = this.$refs.dragTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
  104. this.sortable = Sortable.create(el, {
  105. ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
  106. setData: function(dataTransfer) {
  107. // to avoid Firefox bug
  108. // Detail see : https://github.com/RubaXa/Sortable/issues/1012
  109. dataTransfer.setData('Text', '')
  110. },
  111. onEnd: evt => {
  112. const targetRow = this.list.splice(evt.oldIndex, 1)[0]
  113. this.list.splice(evt.newIndex, 0, targetRow)
  114. // for show the changes, you can delete in you code
  115. const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
  116. this.newList.splice(evt.newIndex, 0, tempIndex)
  117. }
  118. })
  119. }
  120. }
  121. }
  122. </script>
  123. <style>
  124. .sortable-ghost{
  125. opacity: .8;
  126. color: #fff!important;
  127. background: #42b983!important;
  128. }
  129. </style>
  130. <style scoped>
  131. .icon-star{
  132. margin-right:2px;
  133. }
  134. .drag-handler{
  135. width: 20px;
  136. height: 20px;
  137. cursor: pointer;
  138. }
  139. .show-d{
  140. margin-top: 15px;
  141. }
  142. </style>