2

I want to bind an array to a textarea where each line in this textarea is one element in the array. I have no idea how to solve that. I already tried using a v-:change method combined with a temporary v-model to update, but that's pretty dirty.

For example:

<div v-for="item, index in list">
<b-form-textarea v-model.trim="list[index]"></b-form-textarea>
</div>

Result should look like:

list[0] = ['row1 of textarea 1', 'row 2 of textarea 1', ...]
list[1] = ['row1 of textarea 2', 'row 2 of textarea 2', ...]
2
  • You could only bind the whole textarea content to a data property using v-model, you could use computed property to update the textarea from your list and watch property to update the list from the textarea content Commented Oct 23, 2018 at 12:44
  • 1
    yes, I have to say I am pretty new to vue.js but I found an approach using computed property with getter and setter. But I didn't manage to use this together with v-for Commented Oct 23, 2018 at 12:52

1 Answer 1

3

You could save the input value of each textarea in an object, which are collected in an array.

This array could be processed via a computed property to get your desired structure - split("\n") in your case.

var app = new Vue({
  el: '#app',
  data: {
    list: [{
        id: 1,
        value: ""
      },
      {
        id: 2,
        value: ""
      },
    ]
  },
  computed: {
    listByBreaks() {
      return this.list.map(e => {
        return e.value.split("\n");
      });
    }
  }
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<!-- Required Stylesheets -->
<link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />

<!-- Required scripts -->
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>


<div id="app">
  <div v-for="item in list" :key="item.id">
    <b-form-textarea v-model="item.value"></b-form-textarea>
    <br/>
  </div>
  List by breaks: {{ listByBreaks }}
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.