- Vue中以指令方式
import Vue from 'vue'
import dragCellResize from 'drag-cell-resize'
Vue.use(dragCellResize);
1
2
3
2
3
- Vue中以 new DragCellResize()来调用
import {DragCellResize} from 'drag-cell-resize'
export default {
mounted(){
new DragCellResize(this.$refs['table'],()=>{},false)
// DragCellResize三个参数,第一个是要绑定的dom对象,
// 第二个为回调函数,第三个为是否自定义拖拽行为
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# Demo
# 1 默认拖拽行为
仅仅只需要在table上绑定v-drag-cell-resize
,指令会自动完成拖拽列宽操作
First Name | Last Name | Points | Age | Sec |
---|---|---|---|---|
Jill | Smith | 50 | 18 | Man |
Jill | Smith | 50 | 18 | Man |
Jill | Smith | 50 | 18 | Man |
Jill | Smith | 50 | 18 | Man |
Jill | Smith | 50 | 18 | Man |
A | B | C | |||
---|---|---|---|---|---|
A1 | A2 | B1 | B2 | B3 | C |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Copy
# 2 添加回调函数
v-drag-cell-resizez
接受一个值(Function)作为拖拽结束后的回调,再拖拽完成后会自动执行该函数
回调参数
- event:鼠标事件参数
- dragResize:拖拽的实例对象,其中包含offsetX(此次拖拽的偏移量),cell(当前拖拽的单元格)
First Name | Last Name | Points | Age | Sec |
---|---|---|---|---|
Jill | Smith | 50 | 18 | Man |
Jill | Smith | 50 | 18 | Man |
Jill | Smith | 50 | 18 | Man |
Jill | Smith | 50 | 18 | Man |
Jill | Smith | 50 | 18 | Man |
拖拽偏移量:0
Copy
# 3 自定义拖拽行为
v-drag-cell-resize.custom="dragEnd"
给指令传入.custom
修饰符将会由dragEnd
接管拖拽行为,这时候指令不会自动设置列宽,而是由传入的函数自行决定。
必要的拖拽参数在dragEnd的回调参数中可以取到.
dragCellResize
对象中的cell
为当前拖拽的单元格,通过修改该单元格属性来达到想要的结果
# eg1:每次拖拽2倍宽度,设置单元格背景色
A | B | C | |||
---|---|---|---|---|---|
A1 | A2 | B1 | B2 | B3 | C |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Jill | Smith | 50 | 18 | Man | Man |
Copy
# eg2:thead由另一个table组成。
在某些情况下,为了固定表头,我们会时使用另一个table来做表头。对于这种情况,使用.custom
修饰符接管拖拽行为,然后自行设置列宽
First Name | Last Name | Points | Age | Sec |
---|
Adam | Johnson | 67 | 18 | Man |
Adam | Johnson | 67 | 18 | Man |
Adam | Johnson | 67 | 18 | Man |
Adam | Johnson | 67 | 18 | Man |
Adam | Johnson | 67 | 18 | Man |
Copy
此例中的表格由两个table组成,包裹在div中。这时,可以将v-drag-cell-resize
指令绑定在div上面.
然后从dragEnd
回调参数中拿到当前单元格和offsetX,随即可以通过单元格获取到索引。然后再统一设置col
的width
属性来更新两个表格中的列宽