Vuejs list reorder doesn't trigger transitions

1.4k views Asked by At

I've been doing some vuejs for almost a year now and I never really got animations and transitions to work, it seems very unclear to me how all this is supposed to work and today I decided to try and finally understand them but once again I am stuck, the transition simply won't work and I don't understand what I'm doing wrong. Here is my code:

The HTML

<div id="app">
 <div class="row">
      <div class="container-fluid">
        <transition-group name="flip-list" tag="div" class="row horizontal-scroll">
          <div class="col-xs-12 col-md-2 col-lg-2 col-card" 
            v-for="(someCard, index) in someCards" v-bind:key="index">
            <div class="some-card" @click="changeOrder(someCard)">
              {{ someCard.someTitle }}
            </div>
          </div>
        </transition-group>
      </div>
    </div>
</div>

The JS

new Vue({
  el: "#app",
  data: {
    someCards: [
          {
            order: 2,
            someTitle: 'Title 1'
          },
          {
            order: 2,
            someTitle: 'Title 2'
          },
          {
            order: 2,
            someTitle: 'Title 3'
          },
          {
            order: 3,
            someTitle: 'Title 4'
          },
          {
            order: 3,
            someTitle: 'Title 5'
          },
          {
            order: 1,
            someTitle: 'Title 6'
          },
          {
            order: 1,
            someTitle: 'Title 7'
          },
          {
            order: 1,
            someTitle: 'Title 8'
          },
          {
            order: 3,
            someTitle: 'Title 1'
          }
        ]
  },
  methods: {
    changeOrder(someCardData) {
        let newRandom = Math.floor(Math.random() * 3) + 1;
        while (newRandom === someCardData.order) {
          newRandom = Math.floor(Math.random() * 3) + 1;
        }
        someCardData.order = newRandom;
        this.reorderList();
      },
      reorderList() {
        this.someCards.sort(function(objA, objB) {
          return objA.order - objB.order;
        })
      },
  }
})

The CSS

/* default */
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}
#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}
/* bootstrap */
.row {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}
.col-1, .col-2, .col-3, .col-4, .col-5, .col-6, .col-7, .col-8, .col-9, .col-10, .col-11, .col-12, .col, .col-auto, .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12, .col-sm, .col-sm-auto, .col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12, .col-md, .col-md-auto, .col-lg-1, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-10, .col-lg-11, .col-lg-12, .col-lg, .col-lg-auto, .col-xl-1, .col-xl-2, .col-xl-3, .col-xl-4, .col-xl-5, .col-xl-6, .col-xl-7, .col-xl-8, .col-xl-9, .col-xl-10, .col-xl-11, .col-xl-12, .col-xl, .col-xl-auto {
    position: relative;
    width: 100%;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
}

/* my css */
.horizontal-scroll {
  overflow-x: auto;
  -ms-flex-wrap: nowrap;
  flex-wrap: nowrap;
}
.col-card {
  max-width: 14%;
}
.some-card {
  height: 220px;
  font-size: 16px;
  text-align: center;
  padding-right: 5px;
  margin-top: 5px;
  -webkit-box-shadow: 1px 1px 5px rgba(0,0,0,.1);
  box-shadow: 1px 1px 5px rgba(0,0,0,.1);
  border-radius: 2px;
  background: #fff;
  margin-bottom: 20px;
  position: relative;
  border-top-style: solid;
}
.flip-list-move {
  transition: transform 1s;
}

https://jsfiddle.net/eywraw8t/459088/

I am trying to have an animation triggered when I reorder a card in a list of cards aligned in a row. To change the position of a card, simply click on it. The reorder works but no transition is done.

I used this documentation https://v2.vuejs.org/v2/guide/transitions.html#List-Move-Transitions and have seen a couple of fairly easy tutorials but I must be missing something. Can someone please check the code out and tell me what it is ?

Thanks

1

There are 1 answers

1
Husam Elbashir On BEST ANSWER

It's because you're using the index as a key. The index will always be the same for the same position even if the card changes. I'd give each card a unique id and then use that as a key instead ..

new Vue({
  el: "#app",
  data: {
    someCards: [{
        id: 0,
        order: 2,
        someTitle: 'Title 1'
      },
      {
        id: 1,
        order: 2,
        someTitle: 'Title 2'
      },
      {
        id: 2,
        order: 2,
        someTitle: 'Title 3'
      },
      {
        id: 3,
        order: 3,
        someTitle: 'Title 4'
      },
      {
        id: 4,
        order: 3,
        someTitle: 'Title 5'
      },
      {
        id: 5,
        order: 1,
        someTitle: 'Title 6'
      },
      {
        id: 6,
        order: 1,
        someTitle: 'Title 7'
      },
      {
        id: 7,
        order: 1,
        someTitle: 'Title 8'
      },
      {
        id: 8,
        order: 3,
        someTitle: 'Title 1'
      }
    ]
  },
  methods: {
    changeOrder(someCardData) {
      let newRandom = Math.floor(Math.random() * 3) + 1;
      while (newRandom === someCardData.order) {
        newRandom = Math.floor(Math.random() * 3) + 1;
      }
      someCardData.order = newRandom;
      this.reorderList();
    },
    reorderList() {
      this.someCards.sort(function(objA, objB) {
        return objA.order - objB.order;
      })
    },
  }
})
/* default */

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}


/* bootstrap */

.row {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}

.col-1,
.col-2,
.col-3,
.col-4,
.col-5,
.col-6,
.col-7,
.col-8,
.col-9,
.col-10,
.col-11,
.col-12,
.col,
.col-auto,
.col-sm-1,
.col-sm-2,
.col-sm-3,
.col-sm-4,
.col-sm-5,
.col-sm-6,
.col-sm-7,
.col-sm-8,
.col-sm-9,
.col-sm-10,
.col-sm-11,
.col-sm-12,
.col-sm,
.col-sm-auto,
.col-md-1,
.col-md-2,
.col-md-3,
.col-md-4,
.col-md-5,
.col-md-6,
.col-md-7,
.col-md-8,
.col-md-9,
.col-md-10,
.col-md-11,
.col-md-12,
.col-md,
.col-md-auto,
.col-lg-1,
.col-lg-2,
.col-lg-3,
.col-lg-4,
.col-lg-5,
.col-lg-6,
.col-lg-7,
.col-lg-8,
.col-lg-9,
.col-lg-10,
.col-lg-11,
.col-lg-12,
.col-lg,
.col-lg-auto,
.col-xl-1,
.col-xl-2,
.col-xl-3,
.col-xl-4,
.col-xl-5,
.col-xl-6,
.col-xl-7,
.col-xl-8,
.col-xl-9,
.col-xl-10,
.col-xl-11,
.col-xl-12,
.col-xl,
.col-xl-auto {
  position: relative;
  width: 100%;
  min-height: 1px;
  padding-right: 15px;
  padding-left: 15px;
}


/* my css */

.horizontal-scroll {
  overflow-x: auto;
  -ms-flex-wrap: nowrap;
  flex-wrap: nowrap;
}

.col-card {
  max-width: 14%;
}

.some-card {
  height: 220px;
  font-size: 16px;
  text-align: center;
  padding-right: 5px;
  margin-top: 5px;
  -webkit-box-shadow: 1px 1px 5px rgba(0, 0, 0, .1);
  box-shadow: 1px 1px 5px rgba(0, 0, 0, .1);
  border-radius: 2px;
  background: #fff;
  margin-bottom: 20px;
  position: relative;
  border-top-style: solid;
}

.flip-list-move {
  transition: transform 1s;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<div id="app">
  <div class="row">
    <div class="container-fluid">
      <transition-group name="flip-list" tag="div" class="row horizontal-scroll">
        <div class="col-xs-12 col-md-2 col-lg-2 col-card" v-for="(someCard, index) in someCards" v-bind:key="someCard.id">
          <div class="some-card" @click="changeOrder(someCard)">
            {{ someCard.someTitle }}
          </div>
        </div>
      </transition-group>
    </div>
  </div>
</div>

JSFiddle