ElementUI Tag組件實現(xiàn)多標簽生成的方法示例
現(xiàn)在好多應用場景里會有一些需要給文章打標簽等類似的操作,之前jquery用戶是使用taginput來實現(xiàn),使用VUE以后elementui有一個組件非常簡單就是tag組件。
<el-tag :key="tag" v-for="tag in dynamicTags" closable :disable-transitions="false" @close="handleClose(tag)"> {{tag}} </el-tag> <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm" > </el-input> <el-button v-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> export default { 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() { let inputValue = 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() { let inputValue = this.inputValue; if (inputValue) { var values = inputValue.split(/[,, \n]/).filter(item=>{ return item!='' && item!=undefined }) values.forEach(element => { var index = this.dynamicTags.findIndex(i=>{ return i==element }) if(index<0){ this.dynamicTags.push(element); } }); } this.inputVisible = false; this.inputValue = ''; }
效果:
阿大發(fā)
asd三大發(fā)舒服,
阿斯頓發(fā)撒地方。
阿斯頓發(fā),阿斯頓發(fā),,阿斯頓發(fā),,阿斯頓發(fā)安撫,阿斯頓發(fā) 是淡淡的 點點滴滴方法,阿斯頓發(fā)撒地方,adfasd
我們把以上文字復制粘貼進去
所有去重,拆分都OK,那們在試一下,從excel中復制
完成。希望能夠幫到有需要的朋友。也希望大家多多支持腳本之家。
相關文章
Vue中watch監(jiān)聽屬性新舊值相同的問題解決方案
這篇文章主要給大家分享了Vue中watch監(jiān)聽屬性新舊值相同問題解決方案,如果有遇到相同問題的朋友,可以參考閱讀本文2023-08-08快速了解Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法
這篇文章主要介紹了Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下2020-07-07詳解vue中使用transition和animation的實例代碼
這篇文章主要介紹了詳解vue中使用transition和animation的實例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12