[英]on click to remove each chip after enter Vuejs
I would like to remove single chip of my choice when I click on the chip, here is the code.当我点击芯片时,我想删除我选择的单个芯片,这是代码。 Right now I can only delete chips that already separated by comma together not individually.
现在我只能删除已经用逗号分隔的筹码,而不是单独删除。
I'm not sure how to remove oneChip
after the loop我不确定如何在循环后删除
oneChip
new Vue({
el:'div',
props: {
set: {
type: Boolean,
default: true
}
},
data() {
return {
chips:[],
currentInput: ''
}
},
methods: {
saveChip() {
const {chips, currentInput, set} = this;
((set && chips.indexOf(currentInput) === -1) || !set) && chips.push(currentInput);
this.currentInput = '';
},
deleteChip(index) {
this.chips.splice(index, 1);
},
backspaceDelete({which}) {
which == 8 && this.currentInput === '' && this.chips.splice(this.chips.length - 1);
}
},
template: `
<div class="chip-container">
<div v-for="(chip, i) of chips" :key="chip.label">
<span
class="chip" v-for="oneChip in chip.split(',')" v-text="oneChip"
@click="deleteChip(oneChip)">
</span>
</div>
<input v-model="currentInput" @keypress.enter="saveChip" >
</div>
`
})
span {
border: 1px solid red;
}
.chip-container {
width: 800px;
border: 1px solid #ccc;
min-height: 34px;
display:flex;
flex-wrap: wrap;
align-content: space-between;
.chip {
margin:4px;
background: #e0e0e0;
padding:0px 4px;
border: 1px solid #ccc
border-radius: 3px;
display:flex;
align-items: center;
i {
cursor: pointer;
opacity: .56;
margin-left:8px;
}
}
input {
flex: 1 1 auto;
width:30px;
border: none;
outline: none;
padding:4px;
}
}
Here is my solution for the issue, I have split chips within saveChip function这是我对这个问题的解决方案,我在 saveChip 函数中拆分了芯片
methods: {
saveChip() {
const {chips, currentInput, set} = this;
if ((set && chips.indexOf(currentInput) === -1) || !set) {
this.chips = this.chips.concat(currentInput.trim(' ').split(','))
}
this.currentInput = '';
},
deleteChip(chip) {this.chips = this.chips.filter(c => c !== chip)}
},
template: `
<div class="chip-container">
<div v-for="(chip, i) of chips" :key="chip.label">
<span
class="chip" v-for="oneChip in chip.split(',')" v-text="oneChip"
@click="deleteChip(oneChip)">
</span>
</div>
<input v-model="currentInput" @keypress.enter="saveChip" >
</div>
`
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.