2

I am tring to populate a html select field with numbers 1 to 100 using Vuejs. I have tried:

<div id="selector">
    <select id='row_selector' class="form-control" v-model="intArray"></select>
</div>

<script type="text/javascript" src="path_to/vue.js"></script>
<script>
const MAX_VAL = 100; 
var numArray = Array.apply(null, {length: numArray}).map(Number.call, Number)
var app = new Vue({
    el: '#selector',
    data: {
        "intArray": numArray
    }
})

But the select element is empty. Do I need to use a directive to populate it?

1 Answer 1

8

View

<div id="app">
  <select v-model="selected">
    <option v-for="n in 100" :value="n">{{ n }}</option>
  </select>
  <p>
   Selected: {{ selected }}
  </p>
</div>

Component

new Vue({
  el: '#app',
  data: {
    selected: ''
  }
})
Sign up to request clarification or add additional context in comments.

3 Comments

This partly works - I get a drop down that appears to be the correct size, but it is empty and nothing is printed next to 'Selected:' on selection?
Yes you are right the snipper works, I think the issue may be because I am using jinja2 tempating and the double braces causing a problem. Is there a way to just supply an array to the select element?
For Jinja2 you just have to add {% raw %} {% endraw %} around your Vuejs braces as per here stackoverflow.com/questions/25359898/…

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.