stk-table-vue 0.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -0
- package/lib/StkTable/StkTable.vue.d.ts +169 -0
- package/lib/StkTable/const.d.ts +20 -0
- package/lib/StkTable/index.d.ts +2 -0
- package/lib/StkTable/types/index.d.ts +82 -0
- package/lib/StkTable/useColResize.d.ts +18 -0
- package/lib/StkTable/useThDrag.d.ts +14 -0
- package/lib/StkTable/useVirtualScroll.d.ts +35 -0
- package/lib/StkTable/utils.d.ts +23 -0
- package/lib/StkTableC/store.d.ts +12 -0
- package/lib/stk-table-vue.js +1054 -0
- package/lib/style.css +225 -0
- package/package.json +56 -0
- package/src/StkTable/StkTable.vue +1042 -0
- package/src/StkTable/const.ts +26 -0
- package/src/StkTable/index.ts +2 -0
- package/src/StkTable/types/index.ts +109 -0
- package/src/StkTable/useColResize.ts +172 -0
- package/src/StkTable/useHighlight.ts +150 -0
- package/src/StkTable/useThDrag.ts +43 -0
- package/src/StkTable/useVirtualScroll.ts +186 -0
- package/src/StkTable/utils.ts +132 -0
- package/src/StkTable.d.ts +17 -0
- package/src/StkTable.vue +1686 -0
- package/src/StkTableC/index.vue +193 -0
- package/src/StkTableC/store.js +6 -0
- package/src/StkTable_compatible.vue +590 -0
- package/src/VirtualTree.vue +617 -0
- package/src/VirtualTreeSelect.vue +338 -0
- package/src/images/sort-btn.svg +7 -0
- package/src/vite-env.d.ts +5 -0
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="v-tree-select-wrapper" :class="{ disabled: disabled }">
|
|
3
|
+
<!-- <input type="text" @click="onInputClick($event)" /> -->
|
|
4
|
+
<div class="tree-select-main" :class="{ expand: showDropdown }" @click="onInputClick">
|
|
5
|
+
<div class="tree-select-main-label" :class="{ placeholder: !selectedLabel }" :title="selectedLabel">
|
|
6
|
+
{{ selectedLabel || placeholder }}
|
|
7
|
+
</div>
|
|
8
|
+
<div class="tree-select-main-arrow"></div>
|
|
9
|
+
</div>
|
|
10
|
+
<!-- 下拉框 -->
|
|
11
|
+
<div v-if="vIfLoadComponent" v-show="!disabled && showDropdown" class="dropdown-menu" :style="dropdownMenuStyle">
|
|
12
|
+
<VirtualTree
|
|
13
|
+
ref="virtualTree"
|
|
14
|
+
v-bind="vsTreeProps"
|
|
15
|
+
height="100%"
|
|
16
|
+
:replace-fields="assignedFields"
|
|
17
|
+
:tree-data="treeDataClone"
|
|
18
|
+
@item-click="onTreeItemClick"
|
|
19
|
+
/>
|
|
20
|
+
</div>
|
|
21
|
+
<!-- 遮罩:用于点击区域外关闭 -->
|
|
22
|
+
<div
|
|
23
|
+
v-if="!disabled && showDropdown"
|
|
24
|
+
class="dropdown-mask"
|
|
25
|
+
:style="{ zIndex: zIndex }"
|
|
26
|
+
@click="showDropdown = false"
|
|
27
|
+
></div>
|
|
28
|
+
</div>
|
|
29
|
+
</template>
|
|
30
|
+
<script>
|
|
31
|
+
/** 不支持v-model 只能通过 :value + change事件更新 */
|
|
32
|
+
import VirtualTree from './VirtualTree.vue';
|
|
33
|
+
|
|
34
|
+
const _defaultFields = {
|
|
35
|
+
key: 'key',
|
|
36
|
+
title: 'title',
|
|
37
|
+
children: 'children',
|
|
38
|
+
};
|
|
39
|
+
export default {
|
|
40
|
+
components: { VirtualTree },
|
|
41
|
+
props: {
|
|
42
|
+
value: {
|
|
43
|
+
type: String,
|
|
44
|
+
default: '',
|
|
45
|
+
},
|
|
46
|
+
/** 下拉框高度 */
|
|
47
|
+
dropdownHeight: {
|
|
48
|
+
type: Number,
|
|
49
|
+
default: 240, // 8行
|
|
50
|
+
},
|
|
51
|
+
/** 下拉框宽度 */
|
|
52
|
+
dropdownWidth: {
|
|
53
|
+
type: Number,
|
|
54
|
+
default: null,
|
|
55
|
+
},
|
|
56
|
+
/** 下拉菜单与下拉框间的距离 */
|
|
57
|
+
dropdownSpace: {
|
|
58
|
+
type: Number,
|
|
59
|
+
default: 2,
|
|
60
|
+
},
|
|
61
|
+
/** 格式化选中展示的label */
|
|
62
|
+
labelFormatter: {
|
|
63
|
+
type: Function,
|
|
64
|
+
default: null,
|
|
65
|
+
},
|
|
66
|
+
/** 下拉框的z-index */
|
|
67
|
+
zIndex: {
|
|
68
|
+
type: Number,
|
|
69
|
+
default: 10,
|
|
70
|
+
},
|
|
71
|
+
placeholder: {
|
|
72
|
+
type: String,
|
|
73
|
+
default: '请选择',
|
|
74
|
+
},
|
|
75
|
+
treeData: {
|
|
76
|
+
type: Array,
|
|
77
|
+
default: () => [],
|
|
78
|
+
},
|
|
79
|
+
/** 是否禁用 */
|
|
80
|
+
disabled: Boolean,
|
|
81
|
+
/** 替换数据title,key,children字段 */
|
|
82
|
+
replaceFields: {
|
|
83
|
+
type: Object,
|
|
84
|
+
default: () => _defaultFields,
|
|
85
|
+
},
|
|
86
|
+
/** VirtualScrollTree 的prop*/
|
|
87
|
+
vsTreeProps: {
|
|
88
|
+
type: Object,
|
|
89
|
+
default: () => ({}),
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
data() {
|
|
93
|
+
return {
|
|
94
|
+
dropdownMenuStyle: {},
|
|
95
|
+
showDropdown: false,
|
|
96
|
+
/** 展开下拉框时才加载组件 */
|
|
97
|
+
vIfLoadComponent: false,
|
|
98
|
+
/** 保存的prop value */
|
|
99
|
+
storedValue: null,
|
|
100
|
+
/** 记录下次打开下拉框时,是否需要执行这些操作1.高亮2.展开父节点3.滚动至选中的位置 */
|
|
101
|
+
resetVTree: false,
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
computed: {
|
|
105
|
+
treeDataClone() {
|
|
106
|
+
return [...this.treeData];
|
|
107
|
+
},
|
|
108
|
+
/** 合并传入的fields */
|
|
109
|
+
assignedFields() {
|
|
110
|
+
return Object.assign({}, _defaultFields, this.replaceFields);
|
|
111
|
+
},
|
|
112
|
+
selectedLabel() {
|
|
113
|
+
let label = '';
|
|
114
|
+
const item = this.getItemByKey(this.storedValue);
|
|
115
|
+
if (item) {
|
|
116
|
+
if (this.labelFormatter) {
|
|
117
|
+
label = this.labelFormatter(item);
|
|
118
|
+
} else {
|
|
119
|
+
label = item[this.assignedFields.title];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return label || this.storedValue;
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
watch: {
|
|
126
|
+
value(v) {
|
|
127
|
+
this.storedValue = v;
|
|
128
|
+
// this.$refs.virtualTree?.setCurrent(v);
|
|
129
|
+
// this.$refs.virtualTree?.expandItem([v] /* , { foldOthers: true } */);
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
mounted() {
|
|
133
|
+
this.storedValue = this.value;
|
|
134
|
+
},
|
|
135
|
+
methods: {
|
|
136
|
+
onInputClick(e) {
|
|
137
|
+
if (this.disabled) return;
|
|
138
|
+
if (!this.vIfLoadComponent || this.resetVTree) {
|
|
139
|
+
this.vIfLoadComponent = true;
|
|
140
|
+
this.resetVTree = false;
|
|
141
|
+
this.$nextTick(() => {
|
|
142
|
+
this.$refs.virtualTree?.setCurrent(this.storedValue);
|
|
143
|
+
this.$refs.virtualTree?.expandItem([this.storedValue] /* , { foldOthers: true } */);
|
|
144
|
+
this.$refs.virtualTree?.scrollTo(this.storedValue); // 滚动至
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
this.setDropdownMenuStyle(e);
|
|
149
|
+
this.showDropdown = !this.showDropdown;
|
|
150
|
+
this.$nextTick(() => {
|
|
151
|
+
this.$refs.virtualTree.resize();
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
onTreeItemClick(item) {
|
|
155
|
+
this.showDropdown = false;
|
|
156
|
+
this.$emit('change', item);
|
|
157
|
+
},
|
|
158
|
+
// -----------
|
|
159
|
+
/**
|
|
160
|
+
* 设置下拉框从上方弹出还是下方
|
|
161
|
+
*/
|
|
162
|
+
setDropdownMenuStyle() {
|
|
163
|
+
/** @type {DOMRect} */
|
|
164
|
+
const rect = this.$el.getBoundingClientRect();
|
|
165
|
+
const bottom = window.innerHeight - rect.top - rect.height;
|
|
166
|
+
const dropdownWidth = this.dropdownWidth ? this.dropdownWidth : rect.width;
|
|
167
|
+
// reset style
|
|
168
|
+
this.dropdownMenuStyle = {
|
|
169
|
+
position: 'absolute',
|
|
170
|
+
width: dropdownWidth + 'px',
|
|
171
|
+
height: this.dropdownHeight + 'px',
|
|
172
|
+
zIndex: this.zIndex + 1,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
if (window.innerWidth - rect.left >= dropdownWidth) {
|
|
176
|
+
// 右边有空间
|
|
177
|
+
this.dropdownMenuStyle.right = null;
|
|
178
|
+
} else if (rect.right >= dropdownWidth) {
|
|
179
|
+
// 左边有空间
|
|
180
|
+
this.dropdownMenuStyle.right = 0;
|
|
181
|
+
} else {
|
|
182
|
+
this.dropdownMenuStyle.width = '96vw';
|
|
183
|
+
this.dropdownMenuStyle.right = -1 * (window.innerWidth - rect.right) + 'px';
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (bottom >= this.dropdownHeight) {
|
|
187
|
+
// 下方有充足空间
|
|
188
|
+
this.dropdownMenuStyle.top = rect.height + this.dropdownSpace + 'px';
|
|
189
|
+
} else if (rect.top >= this.dropdownHeight) {
|
|
190
|
+
// 上方有充足空间
|
|
191
|
+
this.dropdownMenuStyle.top = -1 * this.dropdownHeight - this.dropdownSpace + 'px';
|
|
192
|
+
} else {
|
|
193
|
+
this.dropdownMenuStyle.top = 0;
|
|
194
|
+
this.dropdownMenuStyle.position = 'fixed';
|
|
195
|
+
this.dropdownMenuStyle.height = window.innerHeight + 'px';
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
/** 通过key值查找一项 */
|
|
199
|
+
getItemByKey(key) {
|
|
200
|
+
let result = null;
|
|
201
|
+
(function recursion(dataSource) {
|
|
202
|
+
for (let i = 0; i < dataSource.length; i++) {
|
|
203
|
+
const item = dataSource[i];
|
|
204
|
+
if (item[this.assignedFields.key] === key) {
|
|
205
|
+
result = item;
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
if (item[this.assignedFields.children]) {
|
|
209
|
+
const res = recursion.bind(this)(item[this.assignedFields.children] || []);
|
|
210
|
+
if (res === 0) return 0;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}.bind(this)(this.treeData));
|
|
214
|
+
return result;
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
// --------------- ref Func
|
|
218
|
+
/**
|
|
219
|
+
* 设置当前选中的项(高亮),会触发change事件
|
|
220
|
+
*/
|
|
221
|
+
setValue(item) {
|
|
222
|
+
let currentItem = item;
|
|
223
|
+
if (typeof item !== 'object') {
|
|
224
|
+
currentItem = this.getItemByKey(item);
|
|
225
|
+
}
|
|
226
|
+
if (currentItem) {
|
|
227
|
+
this.storedValue = currentItem[this.assignedFields.key];
|
|
228
|
+
this.resetVTree = true; // 下次打开下拉框时是否设置虚拟树
|
|
229
|
+
this.onTreeItemClick(currentItem);
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
</script>
|
|
235
|
+
<style lang="less" scoped>
|
|
236
|
+
:v-deep(.vtScroll-tree ul li .list-item:hover:not(.item-highlight)) {
|
|
237
|
+
background-color: #f7f7fc;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
.v-tree-select-wrapper.disabled {
|
|
241
|
+
.tree-select-main {
|
|
242
|
+
border: 1px solid #cbcbe1;
|
|
243
|
+
background-color: rgb(246, 246, 246);
|
|
244
|
+
cursor: not-allowed;
|
|
245
|
+
.tree-select-main-label {
|
|
246
|
+
color: #ccc;
|
|
247
|
+
}
|
|
248
|
+
.tree-select-main-arrow {
|
|
249
|
+
border-top: 5px solid #ccc;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
.v-tree-select-wrapper {
|
|
254
|
+
position: relative;
|
|
255
|
+
width: 200px;
|
|
256
|
+
height: 25px;
|
|
257
|
+
transition: border 0.3s;
|
|
258
|
+
&.disabled {
|
|
259
|
+
.tree-select-main {
|
|
260
|
+
border: 1px solid #cccccc;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
&:hover:not(.disabled) {
|
|
264
|
+
.tree-select-main {
|
|
265
|
+
border: 1px solid #8f90b5;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
.tree-select-main {
|
|
269
|
+
display: flex;
|
|
270
|
+
justify-content: space-between;
|
|
271
|
+
cursor: pointer;
|
|
272
|
+
width: 100%;
|
|
273
|
+
height: 100%;
|
|
274
|
+
border: 1px solid #ddd;
|
|
275
|
+
border-radius: 4px;
|
|
276
|
+
overflow: hidden;
|
|
277
|
+
white-space: nowrap;
|
|
278
|
+
box-sizing: border-box;
|
|
279
|
+
padding: 0 10px;
|
|
280
|
+
transition: all 0.3s;
|
|
281
|
+
&.expand {
|
|
282
|
+
border: 1px solid #8f90b5;
|
|
283
|
+
.tree-select-main-arrow {
|
|
284
|
+
border-top: 5px solid #4a4b72;
|
|
285
|
+
transform: rotate(180deg);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
.tree-select-main-label {
|
|
289
|
+
width: 100%;
|
|
290
|
+
overflow: hidden;
|
|
291
|
+
text-overflow: ellipsis;
|
|
292
|
+
&.placeholder {
|
|
293
|
+
color: #7d7d94;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
.tree-select-main-arrow {
|
|
297
|
+
margin-left: 10px;
|
|
298
|
+
align-self: center;
|
|
299
|
+
width: 0;
|
|
300
|
+
height: 0;
|
|
301
|
+
border-top: 5px solid #757699;
|
|
302
|
+
border-left: 4px solid transparent;
|
|
303
|
+
border-right: 4px solid transparent;
|
|
304
|
+
border-bottom: 0px;
|
|
305
|
+
transition: transform 0.2s ease;
|
|
306
|
+
&.expand {
|
|
307
|
+
transform: rotate(180deg);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
.dropdown-menu {
|
|
312
|
+
overflow: hidden;
|
|
313
|
+
border: 1px solid #ddd;
|
|
314
|
+
position: absolute;
|
|
315
|
+
box-sizing: border-box;
|
|
316
|
+
width: 100%;
|
|
317
|
+
// min-width: max-content;
|
|
318
|
+
// margin-top: 2px;
|
|
319
|
+
border-radius: 4px;
|
|
320
|
+
outline: none;
|
|
321
|
+
box-shadow: 0 4px 12px rgba(10, 39, 86, 0.15);
|
|
322
|
+
border: 1px solid #ececf7;
|
|
323
|
+
// ::v-deep .vtScroll-tree {
|
|
324
|
+
// min-width: max-content;
|
|
325
|
+
// }
|
|
326
|
+
}
|
|
327
|
+
/**遮罩 */
|
|
328
|
+
.dropdown-mask {
|
|
329
|
+
position: fixed;
|
|
330
|
+
top: 0;
|
|
331
|
+
left: 0;
|
|
332
|
+
right: 0;
|
|
333
|
+
bottom: 0;
|
|
334
|
+
height: 100vh;
|
|
335
|
+
width: 100vw;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
</style>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16">
|
|
2
|
+
<g id="sort-btn" fill-rule="nonzero">
|
|
3
|
+
<rect id="rect" opacity="0" x="0" y="0" width="16" height="16"></rect>
|
|
4
|
+
<polygon id="arrow-up" fill="#757699" points="7.99693049 2.00077299 4.79705419 6.00077299 11.1722317 6.00077299"></polygon>
|
|
5
|
+
<polygon id="arrow-down" fill="#757699" transform="translate(7.984643, 11.999227) scale(-1, 1) rotate(-180.000000) translate(-7.984643, -11.999227) " points="7.99693049 9.999227 4.79705419 13.999227 11.1722317 13.999227"></polygon>
|
|
6
|
+
</g>
|
|
7
|
+
</svg>
|