How can I print with a list from 2018 to 1920?

1

I try to print a list with v-for that does the same thing as a for cycle like this

for(n=2018; n>=1920; n--)

This prints a list from 2018 to 1920

The way I have it now is

v-for="n in 2018)" v-if='n >= 120'

But obviously the list is printed from 1920 to 2018

    
asked by Jorge Alberto Ortega Ceja 10.10.2018 в 19:28
source

1 answer

2

You can create a computed property that returns the arrangement of years in the order you want.

Example

window.onload = function() {
  new Vue({
    el: '#editor',
    data: {
      selected: ''
    },
    computed: {
      getYears: function() {
        let years = [];
        for (let n = 2018; n >= 1920; n--) {
          years.push(n);
        }
        return years;
      }
    }
  })
}
<script src="https://unpkg.com/vue@latest/dist/vue.js"></script>

<div id="editor">
  <select v-model="selected">
    <option v-for="option in getYears" v-bind:value="option">
      {{ option }}
    </option>
  </select>
  <span>Seleccionado: {{ selected }}</span>
</div>

References:

answered by 10.10.2018 в 21:17