ElementUI Tag組件實(shí)現(xiàn)多標(biāo)簽生成的方法示例
現(xiàn)在好多應(yīng)用場(chǎng)景里會(huì)有一些需要給文章打標(biāo)簽等類似的操作,之前jquery用戶是使用taginput來實(shí)現(xiàn),使用VUE以后elementui有一個(gè)組件非常簡單就是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: ['標(biāo)簽一', '標(biāo)簽二', '標(biāo)簽三'], 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>
這個(gè)是官方文檔給的實(shí)例,這樣可以解決單一標(biāo)簽輸入。但是實(shí)際場(chǎng)景中,好多用戶是通過ctrl+c,ctrl+v的方式輸入的,有可能還會(huì)一起粘貼好多行的標(biāo)簽,更有可能從excel中復(fù)制出來。
那我一一解決一下這樣一個(gè)場(chǎng)景
首先,先改一下樣式,讓文本框變長:
.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ā) 是淡淡的 點(diǎn)點(diǎn)滴滴方法,阿斯頓發(fā)撒地方,adfasd
我們把以上文字復(fù)制粘貼進(jìn)去
所有去重,拆分都OK,那們?cè)谠囈幌?,從excel中復(fù)制
完成。希望能夠幫到有需要的朋友。也希望大家多多支持腳本之家。
相關(guān)文章
Vue中watch監(jiān)聽屬性新舊值相同的問題解決方案
這篇文章主要給大家分享了Vue中watch監(jiān)聽屬性新舊值相同問題解決方案,如果有遇到相同問題的朋友,可以參考閱讀本文2023-08-08快速了解Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法
這篇文章主要介紹了Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09vue-router路由懶加載及實(shí)現(xiàn)的3種方式
這篇文章主要給大家介紹了關(guān)于vue-router路由懶加載及實(shí)現(xiàn)的3種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02詳解vue中使用transition和animation的實(shí)例代碼
這篇文章主要介紹了詳解vue中使用transition和animation的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12