当前位置:   article > 正文

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

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

行拖拽:

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

  1. <template>
  2. <div>
  3. <el-table
  4. :data="tableData"
  5. border
  6. scrollbar-always-on
  7. ref="tableHeader"
  8. row-key="id"
  9. :row-class-name="rowClassName"
  10. >
  11. <template v-for="item in setColumns" :key="item.label">
  12. <!-- 操作列 -->
  13. <el-table-column
  14. v-if="item.prop === 'oprate'"
  15. fixed="right"
  16. :prop="item.prop"
  17. :label="item.label"
  18. >
  19. <template #header>
  20. <div class="search-title">
  21. <div :class="checked ? 'search-titleName' : ''">操作</div>
  22. <el-icon class="search-icon" @click="search">
  23. <Search color="#409EFF" />
  24. </el-icon>
  25. </div>
  26. </template>
  27. </el-table-column>
  28. <!-- 序号列 -->
  29. <el-table-column
  30. v-else-if="item.prop === 'index'"
  31. :type="item.type"
  32. :label="item.label"
  33. :width="item.width || 100"
  34. />
  35. <!-- 数据列 -->
  36. <el-table-column
  37. v-else
  38. :prop="item.prop"
  39. :label="item.label"
  40. :width="item.width || 100"
  41. />
  42. </template>
  43. </el-table>
  44. </div>
  45. </template>
  46. <script setup lang='js'>
  47. import { ref, watch, onMounted } from 'vue'
  48. import Sortable from 'sortablejs';
  49. let setColumns = ref([
  50. {
  51. prop: 'index',
  52. label: '序号',
  53. type: 'index'
  54. },
  55. {
  56. prop: 'name',
  57. label: '姓名'
  58. },
  59. {
  60. prop: 'address',
  61. label: '地址'
  62. },
  63. {
  64. prop: '11',
  65. label: '1'
  66. },
  67. {
  68. prop: '22',
  69. label: '2'
  70. },
  71. {
  72. prop: '33',
  73. label: '3'
  74. },
  75. {
  76. prop: '44',
  77. label: '4'
  78. },
  79. {
  80. prop: '55',
  81. label: '5'
  82. },
  83. {
  84. prop: '66',
  85. label: '6'
  86. },
  87. {
  88. prop: 'oprate',
  89. lable: ''
  90. }
  91. ])
  92. let tableData = ref([
  93. {
  94. name: 'Tom1',
  95. address: '上海',
  96. 11: 11,
  97. 22: 21,
  98. 33: 31,
  99. 44: 41,
  100. 55: 51,
  101. 66: 61,
  102. id: 1
  103. },
  104. {
  105. name: 'Tom2',
  106. address: '北京',
  107. 11: 12,
  108. 22: 22,
  109. 33: 32,
  110. 44: 42,
  111. 55: 52,
  112. 66: 62,
  113. id: 2
  114. },
  115. {
  116. name: 'Tom3',
  117. address: '广州',
  118. 11: 13,
  119. 22: 23,
  120. 33: 33,
  121. 44: 43,
  122. 55: 53,
  123. 66: 63,
  124. id: 3
  125. },
  126. {
  127. name: 'Tom4',
  128. address: '深圳',
  129. 11: 14,
  130. 22: 24,
  131. 33: 34,
  132. 44: 44,
  133. 55: 54,
  134. 66: 64,
  135. id: 4
  136. }
  137. ])
  138. let sortable;
  139. const tableHeader = ref(null);
  140. onMounted(() => {
  141. rowDrag() // 初始化行拖拽事件
  142. })
  143. const rowClassName = (data) => {
  144. if (data.rowIndex === 0 || data.rowIndex === tableData.value.length - 1) {
  145. return 'rowClassFilter'
  146. }
  147. }
  148. const rowDrag = () => {
  149. let el = tableHeader.value.$el.querySelector('.el-table__body-wrapper tbody')
  150. Sortable.create(el, {
  151. animation: 180,
  152. delay: 0,
  153. filter: '.rowClassFilter', // 类名为rowClassFilter的行不可进行拖拽
  154. onMove({related}) {
  155. return related.className.indexOf('rowClassFilter') === -1;
  156. },
  157. onEnd(evt) {
  158. const oldItem = tableData.value[evt.oldIndex]
  159. tableData.value.splice(evt.oldIndex, 1);
  160. tableData.value.splice(evt.newIndex, 0, oldItem);
  161. }
  162. })
  163. }
  164. </script>
  165. <style scoped>
  166. .search-title{
  167. display: flex;
  168. /* justify-content: space-around; */
  169. }
  170. .search-titleName{
  171. color: #409EFF;
  172. }
  173. .search-icon{
  174. cursor: pointer;
  175. margin-top: 5px;
  176. margin-left: 10px;
  177. }
  178. :deep(.headerClassFilter){
  179. background-color: red !important;
  180. cursor: not-allowed;
  181. }
  182. :deep(.el-table .rowClassFilter){
  183. background-color: #ffeebc !important;
  184. cursor: not-allowed;
  185. }
  186. </style>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号