現(xiàn)在好多應(yīng)用場(chǎng)景里會(huì)有一些需要給文章打標(biāo)簽等類似的操作,之前jquery用戶是使用taginput來實(shí)現(xiàn)班挖,使用VUE以后elementui有一個(gè)組件非常簡單就是tag組件炎辨。
image.png
<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)去
image.png
image.png
所有去重磷账,拆分都OK,那們?cè)谠囈幌录炙洌瑥膃xcel中復(fù)制
image.png
image.png
完成逃糟。希望能夠幫到有需要的朋友。