当前位置:   article > 正文

基于elementUI的el-table组件实现按住某一行数据上下滑动选中/选择或取消选中/选择鼠标经过的行。拖拽选择上下行数据。_el-table 选中某行 进行上移或者下移

el-table 选中某行 进行上移或者下移

 

实现代码 

  1. <template>
  2. <div :class="$options.name">
  3. <el-table
  4. style="user-select: none"
  5. ref="table"
  6. :data="tableData"
  7. :row-class-name="row_class_name"
  8. @mousedown.native="mousedownTable"
  9. @row-click="row_click"
  10. @cell-mouse-enter="cell_mouse_enter"
  11. @cell-mouse-leave="cell_mouse_leave"
  12. @mouseup.native="mouseupTable"
  13. @mouseleave.native="mouseupTable"
  14. @selection-change="selection_change"
  15. >
  16. <el-table-column type="selection" width="50" :selectable="selectable" />
  17. <el-table-column type="index" label="序号" width="60" />
  18. <el-table-column prop="ID" label="ID" />
  19. <el-table-column prop="username" label="用户名" />
  20. </el-table>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. name: "sgBody",
  26. components: {},
  27. data() {
  28. return {
  29. isMousedownTable: false, //是否按下表格
  30. currentEnterRow: null, //当前移入的行数据
  31. tableData: [
  32. { ID: "330110198704103091", username: "username1" },
  33. { ID: "330110198704103092", username: "username2" },
  34. { ID: "330110198704103093", username: "username3" },
  35. { ID: "330110198704103094", username: "username4", disabled: true },
  36. { ID: "330110198704103095", username: "username5" },
  37. { ID: "330110198704103096", username: "username6" },
  38. { ID: "330110198704103097", username: "username7" },
  39. { ID: "330110198704103098", username: "username8" },
  40. ],
  41. selection: [], //表格选中项数组
  42. };
  43. },
  44. methods: {
  45. // 屏蔽复选框
  46. selectable(row) {
  47. return !row.disabled;
  48. },
  49. // 表格按下
  50. mousedownTable(d) {
  51. this.currentEnterRow &&
  52. !this.currentEnterRow.disabled &&
  53. this.$refs.table.toggleRowSelection(this.currentEnterRow);
  54. this.isMousedownTable = true;
  55. },
  56. // 单击表格行
  57. row_click(row, column, event) {
  58. this.currentEnterRow || (!row.disabled && this.$refs.table.toggleRowSelection(row));
  59. },
  60. // 进入单元格
  61. cell_mouse_enter(row, column, cell, event) {
  62. if (column.type === "selection") {
  63. this.currentEnterRow = null;
  64. } else {
  65. this.isMousedownTable &&
  66. !row.disabled &&
  67. this.$refs.table.toggleRowSelection(row);
  68. this.currentEnterRow = row;
  69. }
  70. },
  71. // 离开单元格
  72. cell_mouse_leave(row, column, cell, event) {
  73. this.currentEnterRow = null;
  74. },
  75. // 鼠标弹起或者离开表格
  76. mouseupTable(d) {
  77. this.isMousedownTable = false;
  78. },
  79. // 表格选中项发生改变
  80. selection_change(selection) {
  81. this.selection = selection;
  82. },
  83. // 表格每行样式
  84. row_class_name({ row, rowIndex }) {
  85. if (row.disabled) return "disabled";
  86. return this.selection.find((v) => v.ID == row.ID) ? "selected" : "";
  87. },
  88. },
  89. };
  90. </script>
  91. <style lang="scss" scoped>
  92. .sgBody {
  93. >>> .el-table {
  94. $bgColor: #409eff11;
  95. tr {
  96. // 高亮选中行
  97. &:hover,
  98. &.selected {
  99. td {
  100. background-color: $bgColor !important;
  101. }
  102. }
  103. // 禁用行
  104. &.disabled {
  105. $bgColor: #eee;
  106. td {
  107. background-color: $bgColor !important;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/797944
推荐阅读