How do I reverse the order of an array using v-for and orderBy filter in Vue JS?

后端 未结 6 1608
生来不讨喜
生来不讨喜 2021-02-02 05:58

I am using Vue JS to do viewmodel bindings. In my data object I have an array of items that are sorted in ascending order (oldest to newest) and I\'d like to keep i

相关标签:
6条回答
  • 2021-02-02 06:30

    Update for Vue2

    I want to show some ways that you can work with data and not using filters as they are deprecated in Vue2:

    inside computed property

    Use computed properties in place of filters, which is much better because you can use that data everywhere in component, not only just in template: jsFiddle

    computed: {
        reverseItems() {
            return this.items.slice().reverse();
      }     
    }
    

    inside Vuex getter property

    If you're using Vuex, and you store your data in store.state object. The best way do some transformation with data stored in state is to do that in getters object (for example filtering through a list of items and counting them, reverse order and so on...)

    getters: {
        reverseItems: state => {
            return state.items.slice().reverse();
        }
    }
    

    and retrieve state from getters in component computed property:

    computed: {
        showDialogCancelMatch() {
            return this.$store.state.reverseItems;
        }
    }
    
    0 讨论(0)
  • 2021-02-02 06:36

    Note: The below works in Vue 1, but in Vue 2 filters are deprecated and you will see: ' Property or method "reverse" is not defined on the instance but referenced during render.' See tdom_93's answer for vue2.

    You could create a custom filter to return the items in reversed order:

    Vue.filter('reverse', function(value) {
      // slice to make a copy of array, then reverse the copy
      return value.slice().reverse();
    });
    

    Then use it in the v-for expression:

    <ol>
        <li v-for="item in items | reverse" track-by="id">
    

    https://jsfiddle.net/pespantelis/sgsdm6qc/

    0 讨论(0)
  • 2021-02-02 06:37

    The v-for directive doesn't support iterating backwards, so if you want to order by newest you're going to need to add another field to indicate when the item was added, or change id to increment every time an item is added.

    Then, with field being the field indicting the order added:

    <li v-for="item in items | orderBy 'field' -1" track-by="id">
    
    0 讨论(0)
  • 2021-02-02 06:37

    For my use case (which is admittedly, apparently different than the OP...) I wanted to have the indices of the Array in reverse order in the v-for "loop."

    My solution was to create a Vue app method reverseRange(length) that returns an Array of integers from length-1 to 0. I then used that in my v-for directive and simply referred to my Array elements as myArray[index] every time I needed it.

    That way, the indices were in reverse order and I was able to then use them to access elements of the Array.

    I hope this helps someone who landed on this page with this subtle nuance in their requirements like me.

    0 讨论(0)
  • 2021-02-02 06:48

    Instead of reversing the order of the elements for creation, I only change the order of the display.

    <ol class="reverseorder">
        <li v-for="item in items" track-by="id">
    

    And my CSS

    <style>
    .reverseorder {
      display: flex;
      flex-direction: column-reverse;
    }
    </style>
    

    No need to clone the array and reverse it.

    0 讨论(0)
  • 2021-02-02 06:55

    Simple and concise solution:

    <li v-for="item in items.slice().reverse()">
    //do something with item ...
    </li>
    
    0 讨论(0)
提交回复
热议问题