AddTag.vue
4.03 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
<script>
import * as api from '@/api/customer/tag'
export default {
name: 'AddTag',
components: {},
props: {
// 添加标签显隐
visible: {
type: Boolean,
default: false,
},
// 表单参数
form: {
type: Object,
default: () => ({
groupName: '',
weTags: [],
}),
},
},
emits: ['update:visible'],
data() {
return {
// 添加标签输入框
newInput: '',
// 表单验证规则
rules: Object.freeze({
groupName: [{ required: true, message: '必填项', trigger: 'blur' }],
weTags: [{ required: true, message: '必填项', trigger: 'blur' }],
}),
// 添加便签按钮显隐
visibleAdd: false,
}
},
watch: {},
computed: {
Pvisible: {
get() {
if (this.visible) {
this.$nextTick(() => {
this.$refs['form'].clearValidate()
})
}
return this.visible
},
set(val) {
this.$emit('update:visible', val)
},
},
},
created() {},
mounted() {},
methods: {
closeTag(tag, index) {
// if (tag.id) {
// tag.status = 1
// // this.$forceUpdate()
// } else {
this.form.weTags.splice(index, 1)
// }
},
showInput() {
this.visibleAdd = true
this.$nextTick((_) => {
this.$refs.saveTagInput.$refs.input.focus()
})
},
newInputConfirm() {
let name = this.newInput
if (name) {
Array.isArray(this.form.weTags) || (this.form.weTags = [])
let isExist = this.form.weTags.some((e) => {
return e.name === name
})
if (isExist) {
this.msgError('标签名已存在,不可重复添加')
return
}
this.form.weTags.push({ name })
}
this.visibleAdd = false
this.newInput = ''
},
submit() {
if (!this.visibleAdd) {
this.$refs['form'].validate((valid) => {
this.newInput = ''
let form = JSON.parse(JSON.stringify(this.form))
if (!form.weTags.length) {
return
}
api[form.groupId ? 'update' : 'add'](form).then(() => {
this.msgSuccess('操作成功')
this.Pvisible = false
this.$emit('success')
})
})
}
},
},
}
</script>
<template>
<el-dialog
:title="(form.groupId ? '修改' : '添加') + '标签/组'"
:model-value="Pvisible"
@update:modelValue="(val) => (Pvisible = val)"
width="500px"
append-to-body
:close-on-click-modal="false">
<el-form ref="form" :model="form" :rules="rules" label-width="100px">
<el-form-item label="标签组名称" prop="groupName">
<el-input
v-model.trim="form.groupName"
maxlength="15"
show-word-limit
placeholder="请输入标签组名称,该名称不支持再次修改" />
</el-form-item>
<el-form-item label="标签" prop="weTags">
<template v-for="(item, index) in form.weTags" :key="index">
<el-tag v-if="item.status !== 1" type="primary" closable @close="closeTag(item, index)">
{{ item.name }}
</el-tag>
</template>
<div>
<el-input
class="input-new-tag"
v-if="visibleAdd"
v-model.trim="newInput"
ref="saveTagInput"
maxlength="10"
show-word-limit
@keyup.enter="newInputConfirm"
@blur="newInputConfirm"></el-input>
<el-button v-else type="primary" plain class="button-new-tag" @click="showInput">添加标签</el-button>
</div>
</el-form-item>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button type="primary" @click="submit" v-preventReClick="1000">确 定</el-button>
<el-button @click="Pvisible = false">取 消</el-button>
</div>
</template>
</el-dialog>
</template>
<style lang="scss" scoped>
.input-new-tag {
width: auto;
margin-right: 10px;
vertical-align: bottom;
}
</style>