vue2server7 2.3.10 → 2.3.11
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/package.json +1 -1
- package/test/8.text +14 -0
- package/test/66.text +0 -181
package/package.json
CHANGED
package/test/8.text
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<el-input
|
|
2
|
+
v-model="value"
|
|
3
|
+
@input="value = format(value)"
|
|
4
|
+
/>
|
|
5
|
+
|
|
6
|
+
<script setup>
|
|
7
|
+
const format = (v) => {
|
|
8
|
+
v = v.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.')
|
|
9
|
+
if (v.startsWith('.')) v = '0' + v
|
|
10
|
+
const arr = v.split('.')
|
|
11
|
+
if (arr.length > 2) v = arr[0] + '.' + arr.slice(1).join('')
|
|
12
|
+
return v
|
|
13
|
+
}
|
|
14
|
+
</script>
|
package/test/66.text
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
下面给你一个Vue3 + Element Plus里“父组件是表格,点行/按钮弹出修改 Dialog”的标准封装方式:把 Dialog 做成独立组件,通过 v-model 控制显示,父组件把“当前行数据”传进去;Dialog 内部用本地表单副本编辑,点击保存再把结果 emit 回父组件。
|
|
2
|
-
|
|
3
|
-
⸻
|
|
4
|
-
|
|
5
|
-
1)子组件:EditDialog.vue(封装 Dialog)
|
|
6
|
-
|
|
7
|
-
关键点:
|
|
8
|
-
• 用 modelValue + update:modelValue 做 v-model(显示/隐藏)
|
|
9
|
-
• 接收 row(要编辑的数据)
|
|
10
|
-
• watch row:每次打开时深拷贝到本地 form
|
|
11
|
-
• 保存时 emit('submit', form) 给父组件
|
|
12
|
-
|
|
13
|
-
<template>
|
|
14
|
-
<el-dialog
|
|
15
|
-
:model-value="visible"
|
|
16
|
-
title="修改"
|
|
17
|
-
width="520px"
|
|
18
|
-
@close="handleClose"
|
|
19
|
-
>
|
|
20
|
-
<el-form :model="form" :rules="rules" ref="formRef" label-width="90px">
|
|
21
|
-
<el-form-item label="姓名" prop="name">
|
|
22
|
-
<el-input v-model="form.name" />
|
|
23
|
-
</el-form-item>
|
|
24
|
-
|
|
25
|
-
<el-form-item label="年龄" prop="age">
|
|
26
|
-
<el-input-number v-model="form.age" :min="0" />
|
|
27
|
-
</el-form-item>
|
|
28
|
-
</el-form>
|
|
29
|
-
|
|
30
|
-
<template #footer>
|
|
31
|
-
<el-button @click="emitVisible(false)">取消</el-button>
|
|
32
|
-
<el-button type="primary" :loading="saving" @click="handleSubmit">保存</el-button>
|
|
33
|
-
</template>
|
|
34
|
-
</el-dialog>
|
|
35
|
-
</template>
|
|
36
|
-
|
|
37
|
-
<script setup lang="ts">
|
|
38
|
-
import { computed, reactive, ref, watch } from "vue"
|
|
39
|
-
import type { FormInstance, FormRules } from "element-plus"
|
|
40
|
-
|
|
41
|
-
type Row = { id: number; name: string; age: number }
|
|
42
|
-
|
|
43
|
-
const props = defineProps<{
|
|
44
|
-
modelValue: boolean
|
|
45
|
-
row?: Row | null
|
|
46
|
-
}>()
|
|
47
|
-
|
|
48
|
-
const emit = defineEmits<{
|
|
49
|
-
(e: "update:modelValue", v: boolean): void
|
|
50
|
-
(e: "submit", v: Row): void
|
|
51
|
-
(e: "closed"): void
|
|
52
|
-
}>()
|
|
53
|
-
|
|
54
|
-
const visible = computed(() => props.modelValue)
|
|
55
|
-
|
|
56
|
-
const formRef = ref<FormInstance>()
|
|
57
|
-
const saving = ref(false)
|
|
58
|
-
|
|
59
|
-
// 本地表单副本(避免直接改父组件传入的 row)
|
|
60
|
-
const form = reactive<Row>({
|
|
61
|
-
id: 0,
|
|
62
|
-
name: "",
|
|
63
|
-
age: 0,
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
const rules: FormRules = {
|
|
67
|
-
name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
|
|
68
|
-
age: [{ required: true, message: "请输入年龄", trigger: "change" }],
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 每次 row 改变(尤其是点击不同表格行),刷新本地 form
|
|
72
|
-
watch(
|
|
73
|
-
() => props.row,
|
|
74
|
-
(val) => {
|
|
75
|
-
if (!val) return
|
|
76
|
-
// 简单深拷贝(行数据如果有更深层结构可换 lodash.clonedeep)
|
|
77
|
-
Object.assign(form, JSON.parse(JSON.stringify(val)))
|
|
78
|
-
// 打开时清理校验提示
|
|
79
|
-
formRef.value?.clearValidate?.()
|
|
80
|
-
},
|
|
81
|
-
{ immediate: true, deep: true }
|
|
82
|
-
)
|
|
83
|
-
|
|
84
|
-
function emitVisible(v: boolean) {
|
|
85
|
-
emit("update:modelValue", v)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function handleClose() {
|
|
89
|
-
emit("closed")
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async function handleSubmit() {
|
|
93
|
-
if (!formRef.value) return
|
|
94
|
-
await formRef.value.validate()
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
saving.value = true
|
|
98
|
-
// 交给父组件去调接口/更新表格
|
|
99
|
-
emit("submit", { ...form })
|
|
100
|
-
emitVisible(false)
|
|
101
|
-
} finally {
|
|
102
|
-
saving.value = false
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
</script>
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
⸻
|
|
109
|
-
|
|
110
|
-
2)父组件:TablePage.vue(表格 + 弹窗)
|
|
111
|
-
|
|
112
|
-
关键点:
|
|
113
|
-
• 父组件维护:dialogVisible、currentRow
|
|
114
|
-
• 点击行/按钮:设置 currentRow,再 dialogVisible = true
|
|
115
|
-
• 接收子组件 submit:调用接口更新后刷新表格
|
|
116
|
-
|
|
117
|
-
<template>
|
|
118
|
-
<el-table :data="tableData" style="width: 100%">
|
|
119
|
-
<el-table-column prop="name" label="姓名" />
|
|
120
|
-
<el-table-column prop="age" label="年龄" />
|
|
121
|
-
<el-table-column label="操作" width="120">
|
|
122
|
-
<template #default="{ row }">
|
|
123
|
-
<el-button link type="primary" @click="openEdit(row)">修改</el-button>
|
|
124
|
-
</template>
|
|
125
|
-
</el-table-column>
|
|
126
|
-
</el-table>
|
|
127
|
-
|
|
128
|
-
<EditDialog
|
|
129
|
-
v-model="dialogVisible"
|
|
130
|
-
:row="currentRow"
|
|
131
|
-
@submit="handleUpdate"
|
|
132
|
-
/>
|
|
133
|
-
</template>
|
|
134
|
-
|
|
135
|
-
<script setup lang="ts">
|
|
136
|
-
import { ref } from "vue"
|
|
137
|
-
import EditDialog from "./EditDialog.vue"
|
|
138
|
-
|
|
139
|
-
type Row = { id: number; name: string; age: number }
|
|
140
|
-
|
|
141
|
-
const tableData = ref<Row[]>([
|
|
142
|
-
{ id: 1, name: "Alice", age: 18 },
|
|
143
|
-
{ id: 2, name: "Bob", age: 22 },
|
|
144
|
-
])
|
|
145
|
-
|
|
146
|
-
const dialogVisible = ref(false)
|
|
147
|
-
const currentRow = ref<Row | null>(null)
|
|
148
|
-
|
|
149
|
-
function openEdit(row: Row) {
|
|
150
|
-
// 这里建议也做一次拷贝,避免 currentRow 与表格 data 同引用
|
|
151
|
-
currentRow.value = JSON.parse(JSON.stringify(row))
|
|
152
|
-
dialogVisible.value = true
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async function handleUpdate(payload: Row) {
|
|
156
|
-
// 1) 调接口更新
|
|
157
|
-
// await api.updateUser(payload)
|
|
158
|
-
|
|
159
|
-
// 2) 本地示例:更新 tableData
|
|
160
|
-
const idx = tableData.value.findIndex((x) => x.id === payload.id)
|
|
161
|
-
if (idx !== -1) tableData.value[idx] = payload
|
|
162
|
-
|
|
163
|
-
// 3) 或者你也可以直接 reloadList()
|
|
164
|
-
}
|
|
165
|
-
</script>
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
⸻
|
|
169
|
-
|
|
170
|
-
3)常见坑(你可能会遇到)
|
|
171
|
-
1. 不要直接在 Dialog 里改 props.row
|
|
172
|
-
• Vue 会警告,而且你会把表格行数据“实时改掉”,点取消也回不去
|
|
173
|
-
✅ 用本地 form 副本(上面就是这么做的)
|
|
174
|
-
2. 点击不同的行要能刷新弹窗内容
|
|
175
|
-
• 需要 watch(props.row),每次赋值给 form
|
|
176
|
-
3. 关闭弹窗时表单校验提示残留
|
|
177
|
-
• 打开时 clearValidate() 或关闭时 resetFields()(看你的需求)
|
|
178
|
-
|
|
179
|
-
⸻
|
|
180
|
-
|
|
181
|
-
如果你把“表单字段”和“行数据结构(row)”贴出来(比如有嵌套对象、字典下拉、日期范围之类),我可以把这个封装升级成更完整的版本:支持 open(row)、支持新增/编辑同一个 Dialog、支持异步加载详情、支持表单重置与防抖提交。
|