-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyTable.vue
180 lines (164 loc) · 5.35 KB
/
MyTable.vue
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
98
99
100
101
102
103
104
105
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
<template>
<div ref="table" style="display: flex;flex-direction: column;height: 100%;" :style="{width:tableWidth+'px',}">
<!--1 本组件使用示例:
<MyTable :tableData="tableData"
:columnList="columnList"/>
-->
<!--表格-->
<el-table :data="showTableData">
<el-table-column v-for="(item,index) in tableData[0]" :key="index" style="display:block;"
:prop="item" :label="firstLetterToUpperCase(item)" :width="columnWidth">
<!--自定义表头-->
<template #header>
<div style="display: flex;align-items: center;">
<!-- 可以在此添加你的图标 或者其他元素-->
<el-dropdown :trigger="actionHover?'hover':'click'">
<span @mouseenter="mouseEnter($event,index)" @mouseleave="mouseLeave($event,index)"
class="table-column-name"> {{ firstLetterToUpperCase(item) }}</span>
<template #dropdown v-if="action[index]">
<el-input @input="inputChange($event,index)" :style="{width: columnWidth-20+'px',}" size="small"
v-model="input[index]" clearable placeholder="Filter"/>
</template>
</el-dropdown>
</div>
</template>
</el-table-column>
</el-table>
<MyPagination :currentPageNum="currentPageNum"
:pageSize="pageSize"
:total="total"
:pageSizeOptions="[10,20,30,40,50,100]"
@update-currentPageNum="(e)=>currentPageNum=e"
@update-pageSize="(e)=>pageSize=e"/>
</div>
</template>
<script>
import MyPagination from "@/components/MyPagination.vue";
import MySelect from "@/components/MySelect.vue";
export default {
name: "MyTable",// vue3主张组件名应当是多个单词组合,方便后期维护
components: {MySelect, MyPagination},
props: {
tableData: {// 表格数据
type: Array,
default: () => []
},
columnList: {// 列名列表
type: Array,
default: () => ['1', '2']
},
width: {default: 1000},// 表格宽度
columnWidth: {default: 120},// 每一列宽度
columnNameStyle: {default: 0},// 列名的样式,0:默认,1:首字母大写,2:全部大写,3:全部小写
multiple: {default: false},
},
data() {
return {
// table
tableDataFilter: [],// 处理后的数据集
pageSize: 10,// 每页显示的条/行数
currentPageNum: 1,// 当前页码
// Other
input: [],
action: [],
actionHover: false,
};
},
mounted() {
const keys = this.tableData[0]
console.log("keys:", keys, this.tableData[1])
this.tableDataFilter = this.tableData[1]
},
computed: {
total() {// 总条/行数
// const w = this.tableWidth
if (this.tableDataFilter)
return this.tableDataFilter.length
else
return 0
},
tableWidth() {
const w = (this.tableData[0].length) * (this.columnWidth)
// console.log("宽度:", w)
return w
},
showTableData() {
if (this.tableDataFilter)
return this.tableDataFilter.slice(this.pageSize * (this.currentPageNum - 1), this.pageSize * this.currentPageNum)
else
return []
},
},
methods: {
// 鼠标进入
mouseEnter(e, i) {
for (let j = 0; j < this.tableData[0].length; j++) {
if (j === i) {
this.action[i] = true
continue
}
this.action[j] = false
}
this.actionHover = true
},
mouseLeave(e, i) {
for (let j = 0; j < this.tableData[0].length; j++) {
if (j === i) {
this.action[i] = true
continue
}
this.action[j] = false
}
this.actionHover = false
// this.action[i] = false
},
inputChange(e, i) {
// console.log("inputChange", e, i)
// 进行搜索过滤:
const query = e
if (query === null || query === '') {// 没有输入值时,返回原有的下拉列表(all)
this.tableDataFilter = this.tableData[1];
} else {
// console.log("filterMethod字符串存在非空:", query)
// 通过对比 输入值query \ 下拉选项的label值 来进行筛选数组值
this.tableDataFilter = this.tableData[1].filter((item) => {
return item[this.tableData[0][i]].toLowerCase().includes(query.toLowerCase());// 包含匹配,而不是完全匹配
});
}
// this.action = false
},
/*将列名的字符串首字母转换成大写
* str: 要转换的字符串
* return: 转换后的字符串*/
firstLetterToUpperCase(str) {
if (this.columnNameStyle === 1) {
return this.firstLetterToUpperCase(str);
} else if (this.columnNameStyle === 2) {
return str.toUpperCase();
} else if (this.columnNameStyle === 3) {
return str.toLowerCase();
} else {
return str;
}
},
},
watch: {
valueListCopy(newVal) {
// console.log("父组件传来新值/valueListCopy = ", newVal);
this.valueListCopy = newVal;
this.total = this.valueListCopy?.length;
this.currentPageNum = 1;
},
}
};
</script>
<style scoped>
.elPaginationSelect .el-input input {
border: none !important;
width: 100%;
}
/* 鼠标悬浮在列名上方触发的背景颜色*/
.table-column-name:hover {
background-color: pink;
}
</style>