I need to extract strings from a text area in vue bootstrap and save them into an array. These are number and alpha strings, and when someone puts a coma they separate. Bonus if we could do it so they appear are 'pills' or tags in a tag add input field. Read the documentation could not find anything specifically for this, thanks so much for any help.
The splitting shouldn't be too difficult, just manually bind the input and parse the output from the textarea, converting to and from the data array appropriately for each.
For the textarea's input value:
,
separating each elementlist.join(', ')
to accomplish thisFor the textarea's output value:
update
since that's what BV uses for v-model
value.split(",")
.map(item => item.trim())
.filter(item => item.length > 0)
This input/ output logic will appropriately translate the textarea's contents into an array and vice-versa.
As for the pills, I'm not sure if what I've done below is exactly what you're looking for, but I thought I'd give it a shot.
new Vue({
el: '#app',
data() {
return {
listArr: ["test", "1", "2"],
}
},
computed: {
arrToText() {
return this.listArr.join(', ');
},
},
methods: {
textToArr(value) {
this.listArr = value.split(",")
.map(item => item.trim())
.filter(item => item.length > 0);
},
},
});
<!-- Set up Vue and Bootstrap-Vue for snippet -->
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap@4/dist/css/bootstrap.min.css" /><link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.css" /><script src="//unpkg.com/vue@2/dist/vue.min.js"></script><script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.js"></script>
<div id="app">
<b-form-textarea :value="arrToText" class="mb-2" @update="textToArr"></b-form-textarea>
<h3>
<b-badge v-for="(item, index) in listArr" :key="index" pill variant="primary" class="mr-2">{{ item }}</b-badge>
</h3>
</div>