tab.vue 9.39 KB
Newer Older
葛齐林's avatar
葛齐林 committed
1
<template>
co_dengxiongwen's avatar
co_dengxiongwen committed
2
  <div class="w-table" :class="{ 'w-table_moving': dragState.dragging }">
葛齐林's avatar
葛齐林 committed
3 4 5 6 7
    <!-- <el-table
      :data="data"
      :border="option.border"
      :height="option.height"
      :max-height="option.maxHeight"
co_dengxiongwen's avatar
co_dengxiongwen committed
8 9
      :style="{ width: parseInt(option.width)+'px' }"
      :cell-class-name="cellClassName"
葛齐林's avatar
葛齐林 committed
10 11 12 13 14 15
      :header-cell-class-name="headerCellClassName"
    > -->
    <el-table
      :data="data"
      :border="option.border"
      :stripe="true"
co_dengxiongwen's avatar
co_dengxiongwen committed
16 17
      :style="{ width: parseInt(option.width) + 'px' }"
      :cell-class-name="cellClassName"
葛齐林's avatar
葛齐林 committed
18
      :header-cell-class-name="headerCellClassName"
co_dengxiongwen's avatar
co_dengxiongwen committed
19 20 21
      :header-cell-style="{ background: '#EEF8FF', color: '#333333' }"
      @sort-change="sortChange"
      style="width:98vw"
葛齐林's avatar
葛齐林 committed
22
    >
co_dengxiongwen's avatar
co_dengxiongwen committed
23 24
      <slot name="fixed"></slot>
      <!-- 插槽 -->
葛齐林's avatar
葛齐林 committed
25

co_dengxiongwen's avatar
co_dengxiongwen committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      <template v-for="(col, index) in tableHeader">
        <el-table-column
          :key="index"
          v-if="col.slot"
          :prop="col.prop"
          :label="col.label"
          :width="col.width"
          :min-width="col.minWidth"
          :type="col.type"
          :header-align="col.headerAlign"
          :column-key="index.toString()"
          :render-header="renderHeader"
          header-align="center"
          align="center"
          sortable="custom"
        >
          <!--某一列插槽-->
          <template slot-scope="scope">
            <slot :name="col.prop" :row="scope.row"></slot>
          </template>
        </el-table-column>
        <el-table-column
          v-else-if="col.sort == false"
          :key="index"
          :prop="col.prop"
          :label="col.label"
          :width="col.width"
          :min-width="col.minWidth"
          :type="col.type"
          :header-align="col.headerAlign"
          :column-key="index.toString()"
          :render-header="renderHeader"
          header-align="center"
          align="center"
        >
        </el-table-column>
葛齐林's avatar
葛齐林 committed
62

co_dengxiongwen's avatar
co_dengxiongwen committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        <el-table-column
          v-else
          :key="index"
          :prop="col.prop"
          :label="col.label"
          :width="col.width"
          :min-width="col.minWidth"
          :type="col.type"
          :header-align="col.headerAlign"
          :column-key="index.toString()"
          :render-header="renderHeader"
          header-align="center"
          align="center"
          sortable="custom"
        >
        </el-table-column>
葛齐林's avatar
葛齐林 committed
79 80 81 82 83 84 85
      </template>

      <!-- 操作列 -->
      <template v-if="isShowOperate">
        <el-table-column
          v-if="operationNum"
          label="操作"
co_dengxiongwen's avatar
co_dengxiongwen committed
86 87
          :width="operationNum * 70"
          header-align="center"
葛齐林's avatar
葛齐林 committed
88
          align="center"
co_dengxiongwen's avatar
co_dengxiongwen committed
89
        >
葛齐林's avatar
葛齐林 committed
90 91
          <template slot-scope="scope">
            <!-- 将作用域插槽返回的对象scope继续通过作用域插槽暴露出去 -->
co_dengxiongwen's avatar
co_dengxiongwen committed
92
            <slot :row="scope.row"></slot>
葛齐林's avatar
葛齐林 committed
93 94
          </template>
        </el-table-column>
co_dengxiongwen's avatar
co_dengxiongwen committed
95
        <el-table-column v-else label="操作" width="100">
葛齐林's avatar
葛齐林 committed
96 97
          <template slot-scope="scope">
            <!-- 将作用域插槽返回的对象scope继续通过作用域插槽暴露出去 -->
co_dengxiongwen's avatar
co_dengxiongwen committed
98
            <slot :row="scope.row"></slot>
葛齐林's avatar
葛齐林 committed
99 100 101 102 103 104 105
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>
<script>
co_dengxiongwen's avatar
co_dengxiongwen committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
export default {
  props: {
    data: {
      default: function() {
        return []
      },
      type: Array,
    },
    header: {
      default: function() {
        return []
      },
      type: Array,
    },
    option: {
      default: function() {
        return {}
      },
      type: Object,
    },
    //是否显示操作
    isShowOperate: {
      type: Boolean,
      default: false,
    },
    //几种操作用于设置操作列宽度
    operationNum: {
      type: Number,
      default: 2,
    },
    // 排序是否转驼峰
    isToLine: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      tableHeader: this.header,
      dragState: {
        start: -9, // 起始元素的 index
        end: -9, // 移动鼠标时所覆盖的元素 index
        dragging: false, // 是否正在拖动
        direction: undefined, // 拖动方向
      },
    }
  },
  watch: {
    header(val, oldVal) {
      this.tableHeader = val
    },
  },
  methods: {
    //排序
    sortChange(column) {
      if (column.order === 'descending') {
        this.order = 'desc'
      } else {
        this.order = 'asc'
      }
      if (column.column.columnKey) {
        if (this.isToLine) {
          this.sort = this.toLine(column.column.property)
        } else {
          this.sort = column.column.property
        }
      } else {
        this.sort = column.prop
      }
      // this.$parent.getDataList()
      this.$emit('getDataList', this.sort, this.order)
    },
    toLine(name) {
      return name.replace(/([A-Z])/g, '_$1').toLowerCase()
    },
    renderHeader(createElement, { column }) {
      return createElement(
        'div',
        {
          class: ['thead-cell'],
          on: {
            mousedown: ($event) => {
              this.handleMouseDown($event, column)
葛齐林's avatar
葛齐林 committed
189
            },
co_dengxiongwen's avatar
co_dengxiongwen committed
190 191
            mousemove: ($event) => {
              this.handleMouseMove($event, column)
葛齐林's avatar
葛齐林 committed
192
            },
co_dengxiongwen's avatar
co_dengxiongwen committed
193
          },
葛齐林's avatar
葛齐林 committed
194
        },
co_dengxiongwen's avatar
co_dengxiongwen committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        [
          // 添加 <a> 用于显示表头 label
          createElement('a', column.label),
          // 添加一个空标签用于显示拖动动画
          createElement('span', {
            class: ['virtual'],
          }),
        ]
      )
    },
    // 按下鼠标开始拖动
    handleMouseDown(e, column) {
      this.dragState.dragging = true
      this.dragState.start = parseInt(column.columnKey)
      // 给拖动时的虚拟容器添加宽高
      let table = document.getElementsByClassName('w-table')[0]
      let virtual = document.getElementsByClassName('virtual')
      for (let item of virtual) {
        item.style.height = table.clientHeight - 1 + 'px'
        item.style.width = item.parentElement.parentElement.clientWidth + 'px'
      }
      document.addEventListener('mouseup', this.handleMouseUp)
    },
葛齐林's avatar
葛齐林 committed
218

co_dengxiongwen's avatar
co_dengxiongwen committed
219 220 221 222 223 224 225 226 227 228 229 230
    // 鼠标放开结束拖动
    handleMouseUp() {
      this.dragColumn(this.dragState)
      // 初始化拖动状态
      this.dragState = {
        start: -9,
        end: -9,
        dragging: false,
        direction: undefined,
      }
      document.removeEventListener('mouseup', this.handleMouseUp)
    },
葛齐林's avatar
葛齐林 committed
231

co_dengxiongwen's avatar
co_dengxiongwen committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    // 拖动中
    handleMouseMove(e, column) {
      if (this.dragState.dragging) {
        let index = parseInt(column.columnKey) // 记录起始列
        if (index - this.dragState.start !== 0) {
          this.dragState.direction =
            index - this.dragState.start < 0 ? 'left' : 'right' // 判断拖动方向
          this.dragState.end = parseInt(column.columnKey)
        } else {
          this.dragState.direction = undefined
        }
      } else {
        return false
      }
    },
葛齐林's avatar
葛齐林 committed
247

co_dengxiongwen's avatar
co_dengxiongwen committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    // 拖动易位
    dragColumn({ start, end, direction }) {
      let tempData = []
      let left = direction === 'left'
      let min = left ? end : start - 1
      let max = left ? start + 1 : end
      if (direction !== undefined) {
        for (let i = 0; i < this.tableHeader.length; i++) {
          if (i === end) {
            tempData.push(this.tableHeader[start])
          } else if (i > min && i < max) {
            tempData.push(this.tableHeader[left ? i - 1 : i + 1])
          } else {
            tempData.push(this.tableHeader[i])
          }
        }
        this.tableHeader = tempData
        this.$emit('getDataList', this.sort, this.order)
      }
    },
    headerCellClassName({ column, columnIndex }) {
      let active =
        columnIndex - 1 === this.dragState.end
          ? `darg_active_${this.dragState.direction}`
          : ''
      let start = columnIndex - 1 === this.dragState.start ? `darg_start` : ''
      return `${active} ${start}`
    },
葛齐林's avatar
葛齐林 committed
276

co_dengxiongwen's avatar
co_dengxiongwen committed
277 278 279 280 281
    cellClassName({ column, columnIndex }) {
      return columnIndex - 1 === this.dragState.start ? `darg_start` : ''
    },
  },
}
葛齐林's avatar
葛齐林 committed
282 283 284
</script>

<style lang="scss">
co_dengxiongwen's avatar
co_dengxiongwen committed
285 286 287 288
.w-table {
  .el-table .darg_start {
    background: #f3f3f3;
  }
葛齐林's avatar
葛齐林 committed
289 290
  .el-table th {
    padding: 0;
co_dengxiongwen's avatar
co_dengxiongwen committed
291 292 293
    height: 50px;
    font-weight: 700;
    position: relative;
葛齐林's avatar
葛齐林 committed
294
    // border:1px solid red;
co_dengxiongwen's avatar
co_dengxiongwen committed
295
    .virtual {
葛齐林's avatar
葛齐林 committed
296
      position: absolute;
co_dengxiongwen's avatar
co_dengxiongwen committed
297 298
      z-index: 999;
      display: inline-block;
葛齐林's avatar
葛齐林 committed
299 300
      width: 0;
      height: 0;
co_dengxiongwen's avatar
co_dengxiongwen committed
301
      transform: translate(-45%, -5%);
葛齐林's avatar
葛齐林 committed
302 303 304 305 306 307
      background: none;
      border: none;
      // border: 1px solid red;
    }
    &.darg_active_left {
      .virtual {
co_dengxiongwen's avatar
co_dengxiongwen committed
308
        // border-left: 2px dotted #666;
葛齐林's avatar
葛齐林 committed
309
        z-index: 99;
co_dengxiongwen's avatar
co_dengxiongwen committed
310 311
        background: skyblue;
        opacity: 0.3;
葛齐林's avatar
葛齐林 committed
312 313 314 315
      }
    }
    &.darg_active_right {
      .virtual {
co_dengxiongwen's avatar
co_dengxiongwen committed
316
        // border-right: 2px dotted #666;
葛齐林's avatar
葛齐林 committed
317
        z-index: 99;
co_dengxiongwen's avatar
co_dengxiongwen committed
318 319
        background: skyblue;
        opacity: 0.3;
葛齐林's avatar
葛齐林 committed
320 321 322 323 324 325 326 327 328 329 330
      }
    }
  }
  .thead-cell {
    padding: 0;
    display: inline-flex;
    flex-direction: column;
    align-items: left;
    cursor: pointer;
    overflow: initial;
    &:before {
co_dengxiongwen's avatar
co_dengxiongwen committed
331
      content: '';
葛齐林's avatar
葛齐林 committed
332 333 334 335 336 337 338 339
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
    }
  }
  &.w-table_moving {
co_dengxiongwen's avatar
co_dengxiongwen committed
340
    .el-table th .thead-cell {
葛齐林's avatar
葛齐林 committed
341 342 343 344 345 346 347
      cursor: move !important;
    }
    .el-table__fixed {
      cursor: not-allowed;
    }
  }
}
co_dengxiongwen's avatar
co_dengxiongwen committed
348
</style>