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.
@@ -0,0 +1,193 @@
1
+ <template>
2
+ <div ref="stkTableC" class="stk-table-compatible" @mouseleave="clearHover">
3
+ <StkTable
4
+ v-if="fixedLeftColumns.length"
5
+ ref="stkTableFixedLeft"
6
+ class="stk-table-fixed-left"
7
+ v-bind="$attrs"
8
+ show-tr-hover-class
9
+ :show-no-data="false"
10
+ :data-source="dataSourceCopy"
11
+ :columns="fixedLeftColumns"
12
+ :style="{ height: dataSourceCopy.length ? fixedTableHeight + 'px' : 'auto' }"
13
+ @sort-change="(col, order) => handleSorterChange(col, order, 'l')"
14
+ @scroll="handleFixedLeftTableScroll"
15
+ @th-drag-start="i => onThDragStart('l', i)"
16
+ @th-drop="i => onThDrop('l', i)"
17
+ ></StkTable>
18
+ <StkTable
19
+ ref="stkTableMain"
20
+ v-bind="$attrs"
21
+ show-tr-hover-class
22
+ :data-source="dataSourceCopy"
23
+ :columns="mainTableColumns"
24
+ @sort-change="(col, order) => handleSorterChange(col, order, 'm')"
25
+ @scroll="handleMainTableScroll"
26
+ @th-drag-start="i => onThDragStart('m', i)"
27
+ @th-drop="i => onThDrop('m', i)"
28
+ ></StkTable>
29
+ </div>
30
+ </template>
31
+ <script>
32
+ import { h } from 'vue';
33
+ import StkTable from '../StkTable.vue';
34
+ import store from './store';
35
+ export default {
36
+ name: 'StkTableC',
37
+ components: { StkTable },
38
+ props: {
39
+ columns: {
40
+ type: Array,
41
+ default: () => [],
42
+ },
43
+ dataSource: {
44
+ type: Array,
45
+ default: () => [],
46
+ },
47
+ },
48
+ emits: ['col-order-change', 'th-drag-start', 'th-drop', 'sort-change', 'current-change'],
49
+ data() {
50
+ return {
51
+ sharedState: store.state,
52
+ fixedTableHeight: 0,
53
+ dataSourceCopy: [],
54
+ dragStartIndex: null,
55
+ };
56
+ },
57
+ computed: {
58
+ mainTableColumns() {
59
+ return this.columns.map(col => {
60
+ return {
61
+ ...col,
62
+ ...(col.fixed
63
+ ? {
64
+ minWidth: col.width,
65
+ maxWidth: col.width,
66
+ fixed: null,
67
+ customCell: h('span', ''),
68
+ }
69
+ : {}),
70
+ };
71
+ });
72
+ },
73
+ /** 过滤得到所有固定列 */
74
+ fixedLeftColumns() {
75
+ return this.columns
76
+ .filter(it => it.fixed === 'left')
77
+ .map(col => {
78
+ return { ...col, ...{ fixed: null, minWidth: col.width, maxWidth: col.width } };
79
+ });
80
+ },
81
+ },
82
+ watch: {
83
+ dataSource(val) {
84
+ if (val) this.dataSourceCopy = val;
85
+ if (this.fixedLeftColumns?.length) {
86
+ // 重新计算虚拟滚动高度,因为为了展示暂无数据兜底,左侧固定列的高度开始为0
87
+ this.$nextTick(() => {
88
+ this.$refs.stkTableFixedLeft.initVirtualScroll();
89
+ });
90
+ }
91
+ },
92
+ },
93
+ mounted() {
94
+ this.initStkTableData();
95
+ this.fixedTableHeight = this.$refs.stkTableMain.$el.clientHeight - 1; // -1px border
96
+ this.$refs.stkTableFixedLeft?.initVirtualScroll(this.fixedTableHeight);
97
+ },
98
+ methods: {
99
+ /** 初始化表格共享data */
100
+ initStkTableData() {
101
+ if (this.fixedLeftColumns.length) {
102
+ this.$refs.stkTableFixedLeft.currentHover = store.state.currentHover;
103
+ this.$refs.stkTableFixedLeft.currentItem = store.state.currentItem;
104
+ }
105
+ this.$refs.stkTableMain.currentHover = store.state.currentHover;
106
+ this.$refs.stkTableMain.currentItem = store.state.currentItem;
107
+ },
108
+ /**
109
+ * @param {Event} e
110
+ */
111
+ handleMainTableScroll(e) {
112
+ // console.log(e.target.scrollTop);
113
+ if (this.fixedLeftColumns.length) {
114
+ this.$refs.stkTableFixedLeft.$el.scrollTop = e.target.scrollTop;
115
+ }
116
+ },
117
+ /**
118
+ * @param {Event} e
119
+ */
120
+ handleFixedLeftTableScroll(e) {
121
+ this.$refs.stkTableMain.$el.scrollTop = e.target.scrollTop;
122
+ },
123
+ handleSorterChange(col, order, type) {
124
+ if (type === 'l') {
125
+ this.$refs.stkTableMain.resetSorter();
126
+ this.dataSourceCopy = this.$refs.stkTableFixedLeft?.dataSourceCopy;
127
+ } else if (type === 'm') {
128
+ this.$refs.stkTableFixedLeft?.resetSorter();
129
+ this.dataSourceCopy = this.$refs.stkTableMain.dataSourceCopy;
130
+ }
131
+ this.$emit('sort-change', col, order);
132
+ },
133
+ setHighlightDimRow(key) {
134
+ this.$refs.stkTableFixedLeft?.setHighlightDimRow(key);
135
+ this.$refs.stkTableMain.setHighlightDimRow(key);
136
+ },
137
+ // onColOrderChange(type, sourceIndex, targetIndex) {
138
+ // console.log(type, sourceIndex, targetIndex, 'sdfsdf');
139
+ // },
140
+ onThDragStart(type, startIndex) {
141
+ this.dragStartIndex = startIndex;
142
+ this.$emit('th-drag-start', startIndex);
143
+ },
144
+ onThDrop(type, endIndex) {
145
+ if (this.dragStartIndex !== endIndex) {
146
+ this.$emit('col-order-change', this.dragStartIndex, endIndex);
147
+ }
148
+ this.$emit('th-drop', endIndex);
149
+ },
150
+ clearHover() {
151
+ this.sharedState.currentHover.value = null;
152
+ // store.state.currentHover.value = null; // 不生效
153
+ },
154
+ // ref
155
+ initVirtualScroll() {
156
+ this.$nextTick(() => {
157
+ this.$refs.stkTableFixedLeft?.initVirtualScroll();
158
+ this.$refs.stkTableMain?.initVirtualScroll();
159
+ });
160
+ },
161
+ scrollTo(top = 0) {
162
+ this.$refs.stkTableFixedLeft?.scrollTo(top);
163
+ this.$refs.stkTableMain.scrollTo(top);
164
+ },
165
+ setCurrentRow(rowKey, option = { silent: false }) {
166
+ if (!this.dataSourceCopy.length) return;
167
+ store.state.currentItem.value = this.dataSourceCopy.find(it => this.$refs.stkTableMain.rowKeyGen(it) === rowKey);
168
+ if (!option.silent) {
169
+ this.$emit('current-change', store.state.currentItem);
170
+ }
171
+ },
172
+ },
173
+ };
174
+ </script>
175
+ <style lang="less" scoped>
176
+ .stk-table-compatible {
177
+ position: relative;
178
+ .stk-table-fixed-left {
179
+ z-index: 3;
180
+ position: absolute;
181
+ left: 0;
182
+ top: 0;
183
+ &::-webkit-scrollbar {
184
+ // 隐藏表格滚动条,使其鼠标悬浮在上方时可触发scroll事件 (仅chrome支持)
185
+ width: 0;
186
+ height: 0;
187
+ }
188
+ }
189
+ .stk-table-wrapper {
190
+ height: 100%;
191
+ }
192
+ }
193
+ </style>
@@ -0,0 +1,6 @@
1
+ export default {
2
+ state: {
3
+ currentItem: { value: null },
4
+ currentHover: { value: null },
5
+ },
6
+ };