element+Sortable实现表格拖拽

比眉伴天荒 2021-12-15 00:15 397阅读 0赞

1.先安装sortablejs

  1. <template>
  2. <el-table :data="Data" align="left">
  3. <el-table-column v-for="(item, index) in Table" :key="index" :prop="dropCol[index].prop" :label="item.label">
  4. <template slot-scope="scope">
  5. <el-button v-if="dropCol[index].prop=='companyname'" type="text" size="medium">{
  6. {scope.row[dropCol[index].prop]}}</el-button>
  7. <span v-else>{
  8. {scope.row[dropCol[index].prop]}}</span>
  9. </template>
  10. </el-table-column>
  11. </el-table>
  12. </template>
  13. <script>
  14. import Sortable from 'sortablejs'
  15. export default {
  16. data() {
  17. return {
  18. Table: [
  19. {
  20. label: '公司名称',
  21. prop: 'companyname'
  22. },
  23. {
  24. label: 'global_id',
  25. prop: 'global_id'
  26. },
  27. {
  28. label: '省',
  29. prop: 'address'
  30. }
  31. ],
  32. dropCol: [
  33. {
  34. label: '公司名称',
  35. prop: 'companyname'
  36. },
  37. {
  38. label: 'global_id',
  39. prop: 'global_id'
  40. },
  41. {
  42. label: '省',
  43. prop: 'address'
  44. }
  45. ],
  46. Data: [
  47. {
  48. id: '1',
  49. companyname: '公司名称1',
  50. global_id: '2606615561',
  51. address: '广东'
  52. },
  53. {
  54. id: '2',
  55. companyname: '公司名称1',
  56. global_id: '2594631498',
  57. address: '浙江'
  58. },
  59. {
  60. id: '3',
  61. companyname: '公司名称1',
  62. global_id: '2586480635',
  63. address: '浙江'
  64. },
  65. ]
  66. }
  67. },
  68. mounted() {
  69. this.rowDrop()
  70. this.columnDrop()
  71. },
  72. methods: {
  73. //行拖拽
  74. rowDrop() {
  75. const tbody = document.querySelector('.el-table__body-wrapper tbody')
  76. const _this = this
  77. Sortable.create(tbody, {
  78. onEnd({ newIndex, oldIndex }) {
  79. const currRow = _this.Data.splice(oldIndex, 1)[0]
  80. _this.Data.splice(newIndex, 0, currRow)
  81. }
  82. })
  83. },
  84. //列拖拽
  85. columnDrop() {
  86. const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
  87. this.sortable = Sortable.create(wrapperTr, {
  88. animation: 180,
  89. delay: 0,
  90. onEnd: evt => {
  91. const oldItem = this.dropCol[evt.oldIndex]
  92. this.dropCol.splice(evt.oldIndex, 1)
  93. this.dropCol.splice(evt.newIndex, 0, oldItem)
  94. }
  95. })
  96. }
  97. }
  98. }
  99. </script>

发表评论

表情:
评论列表 (有 0 条评论,397人围观)

还没有评论,来说两句吧...

相关阅读

    相关 表格列可宽度

    问题 普通表格的列是不能够通过用户操作改变宽度, 即动态改变列的宽度。 有时候, 有的列内容是多的,不够显示, 有的列内容是少的,不用太多宽度显示, 但是内容是动态的,