L
L
lookingfor22020-10-04 20:29:59
Vue.js
lookingfor2, 2020-10-04 20:29:59

Why is rendering not happening?

data() {
    return {
      templatesList: this.$store.getters.templatesList,
    };
  },

sortByData() {
      const arr = this.$store.getters.templatesList;
      const n = arr.length;
      for (let i = 0; i < n - 1; i++) {
        for (let j = 0; j < n - 1 - i; j++) {
          if (arr[j + 1].created_at < arr[j].created_at) {
            const t = arr[j + 1];
            arr[j + 1] = arr[j];
            arr[j] = t;
          }
        }
      }
      console.log(arr);
      this.templatesList = arr;
      return true;
    },

The list of elements is in templatesList, I display it on the page
On click I call the sortByData method, in the console I see that the array with the data is changing, but on the page everything remains the same, although I specify this.templatesList = arr;
vuedevtools shows no events

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-10-04
@lookingfor2

Turn on console warnings. You are trying to modify the array directly in the store, which is not allowed.

const arr = this.$store.getters.templatesList.slice(0);

Well, there is a built-in sort method for sorting:
arr.sort(({created_at:a}, {created_at:b}) => a < b ? -1 : a > b ? 1 : 0);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question