快捷导航
查看: 948|回复: 0

ElementUI Tag组件实现多标签生成-霸王洗发水事件

[复制链接]

管理员

发表于 2020-7-1 01:37 | 显示全部楼层 |阅读模式
现在好多应用场景里会有一些需要给文章打标签等类似的操作,之前jquery用户是使用taginput来实现,使用VUE以后elementui有一个组件非常简单就是tag组件。<el-tag:key="tag"v-for="tag in dynamicTags"closable:disable-transitions="false"@close="handleClose(tag)">{{tag}}</el-tag><el-inputclass="input-new-tag"v-if="inputVisible"v-model="inputValue"ref="saveTagInput"size="small"@keyup.enter.native="handleInputConfirm"@blur="handleInputConfirm"></el-input><el-buttonv-else class="button-new-tag"size="small"@click="showInput">+ New Tag</el-button><style>.el-tag + .el-tag {margin-left: 10px;}.button-new-tag {margin-left: 10px;height: 32px;line-height: 30px;padding-top: 0;padding-bottom: 0;}.input-new-tag {width: 90px;margin-left: 10px;vertical-align: bottom;}</style><script>exportdefault{data() {return{dynamicTags: ['标签一', '标签二', '标签三'],inputVisible: false,inputValue: ''};},methods: {handleClose(tag) {this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);},showInput() {this.inputVisible = true;this.$nextTick(_ => {this.$refs.saveTagInput.$refs.input.focus();});},handleInputConfirm() {letinputValue = this.inputValue;if(inputValue) {this.dynamicTags.push(inputValue);}this.inputVisible = false;this.inputValue = '';}}}</script>这个是官方文档给的实例,这样可以解决单一标签输入。但是实际场景中,好多用户是通过ctrl+c,ctrl+v的方式输入的,有可能还会一起粘贴好多行的标签,更有可能从excel中复制出来。那我一一解决一下这样一个场景首先,先改一下样式,让文本框变长:.el-tag{margin-right: 10px;}.el-tag+ .el-tag{margin-right: 10px;}.button-new-tag{height: 32px;line-height: 30px;padding-top: 0;padding-bottom: 0;}.input-new-tag{vertical-align: bottom;}接着,修改一下enter和blur事件:handleInputConfirm() {letinputValue = this.inputValue;if(inputValue) {varvalues = inputValue.split(/[,, \n]/).filter(item=>{returnitem!=''&& item!=undefined})values.forEach(element=>{varindex = this.dynamicTags.findIndex(i=>{returni==element})if(index<0){this.dynamicTags.push(element);}}); }this.inputVisible = false;this.inputValue = '';}效果:阿大发asd 三大发舒服,阿斯顿发撒地方。阿斯顿发,阿斯顿发,,阿斯顿发,,阿斯顿发安抚,阿斯顿发 是淡淡的 点点滴滴方法,阿斯顿发撒地方,adfasd我们把以上文字复制粘贴进去所有去重,拆分都OK,那们在试一下,从excel中复制完成。希望能够帮到有需要的朋友。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

966SEO学习网

GMT+8, 2024-4-26 14:32

Powered by SEO学习网  本站采用创作共用版权 CC BY-NC-SA 3.0 CN 许可协议,转载或复制请注明出处

© 2014-2019 投诉建议及友链申请联系邮箱:[email protected]

快速回复 返回顶部 返回列表