标签管理修改无数据也可以回车创建

This commit is contained in:
王卓煜
2025-09-28 11:07:08 +08:00
parent 18f3804536
commit 5536fc06e1

View File

@@ -15,6 +15,8 @@
@remove-tag="handleTagRemove" @remove-tag="handleTagRemove"
@change="handleSelectionChange" @change="handleSelectionChange"
@keyup.enter.native="handleEnterKey" @keyup.enter.native="handleEnterKey"
@keyup.delete.native="handleDeleteKey"
ref="tagSelect"
> >
<el-option <el-option
v-for="item in searchResults" v-for="item in searchResults"
@@ -113,36 +115,29 @@ export default {
this.loading = false; this.loading = false;
this.params = {}; this.params = {};
}, },
// 修改doSearch方法添加搜索结果为空时的提示
async doSearch(query) {
if (!query.trim()) {
this.searchResults = []
return
}
this.loading = true
try {
const {result:tags} = await apiCourseTag.searchTags({tagName:query})
tags.forEach(item => {
this.tagMap.set(item.id, item)
})
this.searchResults = tags
// 当搜索结果为空时,提示用户可以按回车键创建标签
if (tags.length === 0) {
this.$message.info('无此标签,按回车键创建')
}
} finally {
this.loading = false
}
},
handleTagRemove(tagId) { handleTagRemove(tagId) {
this.selectedTags = this.selectedTags.filter(id => id !== tagId) this.selectedTags = this.selectedTags.filter(id => id !== tagId)
this.$emit('change', this.displayTags) this.$emit('change', this.displayTags)
// 清空输入框内容
if (this.$refs.tagSelect) {
const input = this.$refs.tagSelect.$refs.input
if (input) {
input.value = ''
}
}
}, },
removeTag(tagId) { removeTag(tagId) {
this.handleTagRemove(tagId) this.handleTagRemove(tagId)
}, },
// 新增:处理删除键事件
handleDeleteKey(event) {
// 如果输入框内容为空,不执行任何搜索
if (!event.target.value.trim()) {
this.searchResults = []
}
},
//按回车键,创建新标签 //按回车键,创建新标签
handleEnterKey(event) { handleEnterKey(event) {
const inputVal = event.target.value?.trim() const inputVal = event.target.value?.trim()
@@ -155,6 +150,13 @@ export default {
// 新增:处理选择变化事件 // 新增:处理选择变化事件
handleSelectionChange() { handleSelectionChange() {
this.$emit('change', this.displayTags) this.$emit('change', this.displayTags)
// 选择标签后清空输入框
if (this.$refs.tagSelect) {
const input = this.$refs.tagSelect.$refs.input
if (input) {
input.value = ''
}
}
}, },
//创建新标签 //创建新标签
async createNewTag(tagName) { async createNewTag(tagName) {
@@ -187,6 +189,28 @@ export default {
this.loading = false this.loading = false
} }
}, },
// 修改doSearch方法添加搜索结果为空时的提示
async doSearch(query) {
if (!query.trim()) {
this.searchResults = []
return
}
this.loading = true
try {
const {result:tags} = await apiCourseTag.searchTags({tagName:query})
tags.forEach(item => {
this.tagMap.set(item.id, item)
})
this.searchResults = tags
// 当搜索结果为空时,提示用户可以按回车键创建标签
if (tags.length === 0) {
this.$message.info('无此标签,按回车键创建')
}
} finally {
this.loading = false
}
}
} }
} }
</script> </script>