How to update data() in vue.draggable

3.7k Views Asked by At

I know this may be basic stuff... but I cannot get my data updated when using Vue.Draggable 2.23. In the code below, the 4 lists are returned from the axios call exactly as they are now return by data(), but then, of course, with content. Somehow I keep getting "Property or method "lane0" is not defined on the instance but referenced during render." In App.vue:

<script>
import draggable from "vuedraggable";
import axios from "axios";
export default {
  name: "two-lists",
  display: "Two Lists",
  order: 1,
  components: {
    draggable
  },

  data() {
    return {
        "lane0": [],
        "lane1": [],
        "lane2": [],
        "lane3": []
  }
    },
  mounted () {
    axios
      .get('http://localhost:8000/pylims/get_sequencable_lanes/10/S4')
      .then(response => (this.data= response.data))
  },
  methods: {

    log: function(evt) {
      window.console.log(evt);
    }
  }
}
</script>

On request, the HTML part (template in App.vue):

<template>
  <div class="row">
    <div class="col-3">
      <h3>Draggable 1</h3>
      <draggable class="list-group" :list="lane0" group="people" @change="log">
        <div
          class="list-group-item"
          v-for="(element, index) in lane0"
          :key="element.sample_name"
        >
          {{ element.sample_name }} {{ index }}
        </div>
      </draggable>
    </div>

    <div class="col-3">
      <h3>Draggable 2</h3>
      <draggable class="list-group" :list="lane1" group="people" @change="log">
        <div
          class="list-group-item"
          v-for="(element, index) in lane1"
          :key="element.sample_name"
        >
          {{ element.sample_name }} {{ index }}
        </div>
      </draggable>
    </div>

    <div class="col-3">
      <h3>Draggable 3</h3>
      <draggable class="list-group" :list="lane2" group="people" @change="log">
        <div
          class="list-group-item"
          v-for="(element, index) in lane2"
          :key="element.sample_name"
        >
          {{ element.sample_name }} {{ index }}
        </div>
      </draggable>
    </div>
<br><br>
{{ lane0 }}
    <br><br>
{{ lane1 }}
    <br><br>
{{ lane2 }}
    <br><br>
{{ lane3 }}

  </div>
</template>
1

There are 1 best solutions below

0
On BEST ANSWER

As Tobias G. states in the comment and is clearly explained here: https://v2.vuejs.org/v2/cookbook/using-axios-to-consume-apis.html the problem is solved by wrapping the lanes in an object, and then updating that from within the mounted() function, like this:

 data() {
    return {
        lanes: {
          "lane0": [],
          "lane1": [],
          "lane2": [],
          "lane3": []
        }
  }
    },
  mounted () {
    axios
      .get('http://localhost:8000/pylims/get_sequencable_lanes/10/S4')
      .then(response => (this.lanes = response.data))
  },

then using it in the HTML part like this:

<draggable class="list-group" :list="lanes.lane0" group="people" @change="log">