• vue3+ele-plus+sortableJs对el-table使用sortableJs插件对表格拖拽时限定某列或某行不允许拖拽


    如需有对el-table表格进行拖拽的需求,请点击:
    eleplus对el-table表格进行拖拽(使用sortablejs进行列拖拽和行拖拽):-CSDN博客
     如果你已实现拖拽需求,但拖拽后发现表头并未改变的话,请点击:

    解决el-table表格拖拽后,只改变了数据,表头没变的问题-CSDN博客

    先看看是不是你想要的。

    限制拖拽

    Sortable.js中文网

    sortableJs有个配置项是filter,过滤掉不可进行拖拽的列或行。

    本文采用vue3+ele-plus的table组件结合sortableJs插件对表格拖拽需求进行开发:

    列拖拽:

    使用:header-cell-class-name="headerCellClassName"对el-table的列增加headerClassFilter类名,有此类名的行不可进行拖拽(在Sortable.create实例中对headerClassFilter类名进行过滤,即filter绑定的类名)。

    1. <script setup lang='js'>
    2. import { ref, onMounted } from 'vue'
    3. import Sortable from 'sortablejs';
    4. let setColumns = ref([
    5. {
    6. prop: 'index',
    7. label: '序号',
    8. type: 'index'
    9. },
    10. {
    11. prop: 'name',
    12. label: '姓名'
    13. },
    14. {
    15. prop: 'address',
    16. label: '地址'
    17. },
    18. {
    19. prop: '11',
    20. label: '1'
    21. },
    22. {
    23. prop: '22',
    24. label: '2'
    25. },
    26. {
    27. prop: '33',
    28. label: '3'
    29. },
    30. {
    31. prop: '44',
    32. label: '4'
    33. },
    34. {
    35. prop: '55',
    36. label: '5'
    37. },
    38. {
    39. prop: '66',
    40. label: '6'
    41. },
    42. {
    43. prop: 'oprate',
    44. lable: ''
    45. }
    46. ])
    47. let tableData = ref([
    48. {
    49. name: 'Tom1',
    50. address: '上海',
    51. 11: 11,
    52. 22: 21,
    53. 33: 31,
    54. 44: 41,
    55. 55: 51,
    56. 66: 61,
    57. id: 1
    58. },
    59. {
    60. name: 'Tom2',
    61. address: '北京',
    62. 11: 12,
    63. 22: 22,
    64. 33: 32,
    65. 44: 42,
    66. 55: 52,
    67. 66: 62,
    68. id: 2
    69. },
    70. {
    71. name: 'Tom3',
    72. address: '广州',
    73. 11: 13,
    74. 22: 23,
    75. 33: 33,
    76. 44: 43,
    77. 55: 53,
    78. 66: 63,
    79. id: 3
    80. },
    81. {
    82. name: 'Tom4',
    83. address: '深圳',
    84. 11: 14,
    85. 22: 24,
    86. 33: 34,
    87. 44: 44,
    88. 55: 54,
    89. 66: 64,
    90. id: 4
    91. }
    92. ])
    93. let sortable;
    94. const tableHeader = ref(null);
    95. onMounted(() => {
    96. columnDrag(); // 初始化列拖拽事件
    97. })
    98. const headerCellClassName = ({column}) => { // 给序号和操作列增加类名,意在这两列不可进行拖拽
    99. if (column.label === '序号' || column.property === 'oprate') {
    100. return 'headerClassFilter'
    101. }
    102. }
    103. const columnDrag = () => {
    104. let el = tableHeader.value.$el.querySelector('.el-table__header-wrapper tr')
    105. Sortable.create(el, {
    106. animation: 180,
    107. delay: 0,
    108. filter: '.headerClassFilter', // 类名为headerClassFilter的列不可进行拖拽
    109. onMove({ related }) { // 移动单元格时,如果related.className包含headerClassFilter,即不可拖拽
    110. return related.className.indexOf('headerClassFilter') === -1;
    111. },
    112. onEnd(evt) {
    113. const oldItem = setColumns.value[evt.oldIndex]
    114. setColumns.value.splice(evt.oldIndex, 1);
    115. setColumns.value.splice(evt.newIndex, 0, oldItem);
    116. }
    117. })
    118. }
    119. script>
    120. <style scoped>
    121. .search-title{
    122. display: flex;
    123. /* justify-content: space-around; */
    124. }
    125. .search-titleName{
    126. color: #409EFF;
    127. }
    128. .search-icon{
    129. cursor: pointer;
    130. margin-top: 5px;
    131. margin-left: 10px;
    132. }
    133. :deep(.headerClassFilter){
    134. background-color: red !important;
    135. cursor: not-allowed;
    136. }
    137. :deep(.el-table .rowClassFilter){
    138. background-color: #ffeebc !important;
    139. cursor: not-allowed;
    140. }
    141. style>

    行拖拽:

    使用:row-class-name="rowClassName"对el-table的行增加rowClassFilter类名,有此类名的行不可进行拖拽(在Sortable.create实例中对rowClassFilter类名进行过滤,即filter绑定的类名)。

    1. <script setup lang='js'>
    2. import { ref, watch, onMounted } from 'vue'
    3. import Sortable from 'sortablejs';
    4. let setColumns = ref([
    5. {
    6. prop: 'index',
    7. label: '序号',
    8. type: 'index'
    9. },
    10. {
    11. prop: 'name',
    12. label: '姓名'
    13. },
    14. {
    15. prop: 'address',
    16. label: '地址'
    17. },
    18. {
    19. prop: '11',
    20. label: '1'
    21. },
    22. {
    23. prop: '22',
    24. label: '2'
    25. },
    26. {
    27. prop: '33',
    28. label: '3'
    29. },
    30. {
    31. prop: '44',
    32. label: '4'
    33. },
    34. {
    35. prop: '55',
    36. label: '5'
    37. },
    38. {
    39. prop: '66',
    40. label: '6'
    41. },
    42. {
    43. prop: 'oprate',
    44. lable: ''
    45. }
    46. ])
    47. let tableData = ref([
    48. {
    49. name: 'Tom1',
    50. address: '上海',
    51. 11: 11,
    52. 22: 21,
    53. 33: 31,
    54. 44: 41,
    55. 55: 51,
    56. 66: 61,
    57. id: 1
    58. },
    59. {
    60. name: 'Tom2',
    61. address: '北京',
    62. 11: 12,
    63. 22: 22,
    64. 33: 32,
    65. 44: 42,
    66. 55: 52,
    67. 66: 62,
    68. id: 2
    69. },
    70. {
    71. name: 'Tom3',
    72. address: '广州',
    73. 11: 13,
    74. 22: 23,
    75. 33: 33,
    76. 44: 43,
    77. 55: 53,
    78. 66: 63,
    79. id: 3
    80. },
    81. {
    82. name: 'Tom4',
    83. address: '深圳',
    84. 11: 14,
    85. 22: 24,
    86. 33: 34,
    87. 44: 44,
    88. 55: 54,
    89. 66: 64,
    90. id: 4
    91. }
    92. ])
    93. let sortable;
    94. const tableHeader = ref(null);
    95. onMounted(() => {
    96. rowDrag() // 初始化行拖拽事件
    97. })
    98. const rowClassName = (data) => {
    99. if (data.rowIndex === 0 || data.rowIndex === tableData.value.length - 1) {
    100. return 'rowClassFilter'
    101. }
    102. }
    103. const rowDrag = () => {
    104. let el = tableHeader.value.$el.querySelector('.el-table__body-wrapper tbody')
    105. Sortable.create(el, {
    106. animation: 180,
    107. delay: 0,
    108. filter: '.rowClassFilter', // 类名为rowClassFilter的行不可进行拖拽
    109. onMove({related}) {
    110. return related.className.indexOf('rowClassFilter') === -1;
    111. },
    112. onEnd(evt) {
    113. const oldItem = tableData.value[evt.oldIndex]
    114. tableData.value.splice(evt.oldIndex, 1);
    115. tableData.value.splice(evt.newIndex, 0, oldItem);
    116. }
    117. })
    118. }
    119. script>
    120. <style scoped>
    121. .search-title{
    122. display: flex;
    123. /* justify-content: space-around; */
    124. }
    125. .search-titleName{
    126. color: #409EFF;
    127. }
    128. .search-icon{
    129. cursor: pointer;
    130. margin-top: 5px;
    131. margin-left: 10px;
    132. }
    133. :deep(.headerClassFilter){
    134. background-color: red !important;
    135. cursor: not-allowed;
    136. }
    137. :deep(.el-table .rowClassFilter){
    138. background-color: #ffeebc !important;
    139. cursor: not-allowed;
    140. }
    141. style>

  • 相关阅读:
    C专家编程 第1章 C:穿越时空的迷雾 1.3 标准I/O库和C预处理器
    JavaScript工具函数大全
    go实现自定义rpc框架 (核心:服务端&客户端、自定义io流、编解码、服务发现、负载均衡、支持多语言网关等)
    论文字体,Word字体大小对照换算表(字号、磅、英寸、像素)
    重学Elasticsearch第1章 : Elasticsearch, Kibana概念、Elasticsearch相关术语
    蓄电池电压检测单元 电池监控模块 24路电池电压采样模块电源检测
    【数据结构】外部排序、多路平衡归并与败者树、置换-选择排序(生成初始归并段)、最佳归并树算法
    CSAPP-第二章学习笔记
    未解决的notebook问题
    如何高效的开展app的性能测试?
  • 原文地址:https://blog.csdn.net/weixin_42234899/article/details/139627900