naiveui表格shift多选
const handleCheck = (e: any) => {
checkedRowKeys.value = e
}
// 上一次选择的方式 1:单选,2:多选,3:连选
const clickInfo = ref({
startRowIndex: -1,
endRowIndex: -1
})
const singleRowKeys = ref([]) as any
const totalIndex = ref([]) as any
const totalKeys = (key: any) => {
data.forEach((val: any, index: any) => {
if (checkedRowKeys.value.indexOf(val.key) != -1) {
if (totalIndex.value.indexOf(index) == -1) {
totalIndex.value.push(index)
}
}
})
}
const toggleCheck = (toggleData: any, key: any) => {
if (toggleData.indexOf(key) == -1) {
toggleData.push(key)
} else {
toggleData.splice(toggleData.indexOf(key), 1)
}
}
const rowProps = (row: any, rowIndex: any) => {
return {
onClick: (e: any) => {
toggleCheck(singleRowKeys.value, row.key) // 存储单击选中的值
if (e.target.className == 'n-checkbox-box__border') {
if (e.shiftKey) { // 选框内shift全选失效,成为ctrl功能
checkedRowKeys.value = []
checkedRowKeys.value = singleRowKeys.value
}
totalIndex.value = []
totalKeys(row.key) // 获取所有选中的key的索引
clickInfo.value.startRowIndex = Math.min(...totalIndex.value) // 取出最小值作为shift的开始值
} else {
if (clickInfo.value.startRowIndex == -1) {
clickInfo.value.startRowIndex = rowIndex // 说明未点击,将开始点击的索引设为shift的初始值
} else {
if (e.shiftKey) { // 如果按住了shift,将按住shift的索引值设置为shift的最终值
clickInfo.value.endRowIndex = rowIndex
selectedTable(clickInfo.value.startRowIndex, clickInfo.value.endRowIndex, row.key)
} else { // 如果没按住,相当于是重新点击了表格,等于是表格的单选,则将这次点击的索引值设置为shift的开始值
clickInfo.value.startRowIndex = rowIndex
}
}
if (!e.shiftKey && !e.ctrlKey) {
checkedRowKeys.value = []
}
if (e.ctrlKey) {
totalIndex.value = []
totalKeys(row.key)
checkedRowKeys.value = singleRowKeys.value
if (totalIndex.value.length == 0) {
clickInfo.value.startRowIndex = -1
} else {
clickInfo.value.startRowIndex = Math.min(...totalIndex.value)
}
}
if (!e.ctrlKey) {
checkedRowKeys.value.push(row.key)
}
}
}
}
}
const selectedTable = (startRowIndex: any, endRowIndex: any, currentId: any) => {
const startIndex = Math.min(startRowIndex, endRowIndex) // 获取点击先后的最大值与最小值
const endIndex = Math.max(startRowIndex, endRowIndex)
checkedRowKeys.value = [data[startRowIndex].key]
if (startIndex == endIndex) { // 如果点击的是同一个,则只有当前被选中
checkedRowKeys.value = [currentId]
} else {
data.forEach((rowData: any, rowIndex: any) => {
if (checkedRowKeys.value.some((msItem: any) => msItem == rowData.key)) {
} else {
let temIndex = []
if (rowIndex > startIndex && rowIndex < endIndex) {
checkedRowKeys.value.push(rowData.key)
temIndex.push(rowIndex)
totalIndex.value = temIndex // 将所有选中key的索引更新
}
}
})
}
singleRowKeys.value = checkedRowKeys.value // 更新单击选中的值(ctrl功能类似)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
上次更新: 2024/07/04, 10:16:46