2

This is an extension from this question Vue.js: Input formatting using computed property is not applying when typing quick

I am stuck on how to get a list of formatted values from my text input into an array list. I need to do this in a matrix, but simplified it into an array.

Please help, thank you!

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
   <div
      v-for="(input, index) in valueInputs" <-- index
      :key="index"
    >
      <input
        v-model="value"     // <-- I want to track what index I'm in
        @input="formatTime" // <-- so I can set it in an array later
        maxLength="4"       // I tried formatTime[index] or value[index]
        id="format-value" // but that doesn't work, how to pass index 
        class="input" // into formatTime or value fields?
        type="text"
      />
  </div>
</div>

data () {
  return {
  valueInputs: [],    // a list of inputs
  allFormatValues: [] // want to store all the formatted values here by the index
 }
}

Want to set an array that stores all the formatted values:

   this.allFormatValues[index] = this.value;

I'm not sure how to associate the index with the formatted string value?

1 Answer 1

1

You are retrieving the values of valueInputs array not its indexs. However, you can get the index of each value in v-for as follows:

v-for="(value, index) in valueInputs"
Sign up to request clarification or add additional context in comments.

11 Comments

Ah I simplified the code, but yes I already v-for="(value, index) in valueInputs" in the code. But the problem is I can't pass the index into the input tag v-model="value" or @input="formatTime"
You can pass it as a parameter to formatTime function. @input="formatTime(index)" or @input="formatTime(value, index)" if you need both in you function. Just make sure to change formatTime code accordingly though.
Thanks, the code in the link actually didn't work in my case. Sorry I think I wasn't clear. I have to use watches for it to work from the below link, so I do't know how to add indexes here stackoverflow.com/questions/67551738/…
Will valueInputs array be filled in by users?
Yes, a user will be able to add more days and its stored there
|

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.