index.vue
4.88 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<template>
<div class="table-content">
<!-- 采用的原生表格 -->
<table align="center">
<thead>
<tr>
<th></th>
<th>内置参数</th>
<th>参数名</th>
<th>是否必须</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in tableArray.content" :key="index">
<td style="width: 1vw">
{{ index + 1 }}
</td>
<td style="width: 12vw">
<Select
v-model:value="item.key"
placeholder="请选择"
:options="selectOptions"
@change="handleChange"
allowClear
/>
</td>
<td style="width: 12vw">
<a-input
:disabled="item.editDisabled"
placeholder="请输入"
v-model:value="item.value"
/>
</td>
<td style="width: 4vw">
<a-switch v-model:checked="item.required" />
</td>
<td style="width: 2vw">
<div>
<Button type="dashed" @click="add(item, index)">
<template #icon><PlusOutlined /></template
></Button>
<Button type="dashed" style="margin-left: 5px" @click="remove(item, index)">
<template #icon> <MinusOutlined /></template>
</Button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script lang="ts" setup name="editCellTable">
import { reactive, ref, onMounted, nextTick } from 'vue';
import { Select, Button } from 'ant-design-vue';
import { findDictItemByCode } from '/@/api/system/dict';
import { PlusOutlined, MinusOutlined } from '@ant-design/icons-vue';
defineProps({
method: {
type: String,
},
});
onMounted(() => {
getSelectOptions();
});
const getSelectOptions = async () => {
const res = await findDictItemByCode({ dictCode: 'dataview_builtin_params' });
selectOptions.value = res.map((m) => ({ label: m.itemText, value: m.itemValue }));
selectOptions.value.push({
label: '自定义',
value: 'scope',
});
};
const selectOptions = ref<{ label: string; value: string; disabled?: boolean }[]>([]);
type defaultItem = {
key: null | string;
value: string;
editDisabled: boolean;
required: boolean;
};
const tableArray = reactive<{
content: defaultItem[];
}>({
content: [
{
key: null,
value: '',
editDisabled: false,
required: false,
},
],
});
// 新增
const add = (_, index: number) => {
tableArray.content.splice(index + 1, 0, {
key: null,
value: '',
editDisabled: false,
required: false,
});
};
// 减少
const remove = (item, index: number) => {
if (tableArray.content.length !== 1) {
selectOptions.value.forEach((ele) => {
if (ele.value == item.key) {
ele.disabled = false;
}
});
tableArray.content.splice(index, 1);
}
};
//Select互斥
const handleChange = () => {
selectOptions.value.forEach((ele) => {
ele.disabled = false;
tableArray.content.forEach((element) => {
if (element.key === 'scope') {
element.editDisabled = false;
} else {
element.value = '';
element.editDisabled = true;
}
if (element.key === ele.value && element.key !== 'scope') {
ele.disabled = true;
}
});
});
};
//获取数据
const getValue = () => {
return tableArray.content;
};
//设置数据
const setValue = (data) => {
nextTick(() => (tableArray.content = data));
nextTick(() =>
setTimeout(() => {
tableArray.content.forEach(() => {
handleChange();
});
}, 20)
);
};
//重置数据
const resetValue = () => {
tableArray.content = [];
tableArray.content.push({
key: null,
value: '',
editDisabled: false,
required: false,
});
nextTick(() => {
tableArray.content.forEach(() => {
handleChange();
});
});
};
defineExpose({
getValue,
setValue,
resetValue,
});
</script>
<style scoped lang="less">
@table-color: #e5e7eb;
.table-border-color {
border: 1px solid #e5e7eb;
text-align: center;
}
.table-content {
overflow-x: auto;
table {
border-collapse: collapse;
width: 35vw;
&:extend(.table-border-color);
}
table td {
padding: 5px;
white-space: nowrap;
&:extend(.table-border-color);
}
table th {
padding: 5px;
&:extend(.table-border-color);
}
}
</style>