Search code examples
javascriptvue.jsaxioshandsontable

Vue.js crud javascript?


I need to load data to a hands-on table,

When I use:

  1. case: if used directly into data, its work good, but I need to load data when is created from Axios, using Axios. This doesn't work.
data: function() {
  return {
    info:[],
    hotSettings: {
      data: [['a','b','c'],['ra','rb','rc']],
    }
  }
}
  1. case: if use in my variable info, it doesn't work either.
data: function() {
  return {
    info:[['a','b','c'],['ra','rb','rc']],
    hotSettings: {
      data: this.info,
    }
  }
}
  1. case: using hook created. This doesn't work.
<template>     
   <div>
     <hot-table ref="hotTableComponent" :settings="hotSettings"></hot-table>
   </div>
</template>
     
<script>
import { HotTable } from '@handsontable/vue';
import Handsontable from 'handsontable';
    
export default {
  created: function (){
    this.newData()
  },
  data: function() {
    return {
      info:[],
      hotSettings: {
        data: this.info,
        colHeaders: ['ID','Name',' pain'],
        rowHeaders: true,
        minRows: 2,
        minCols: 3,
      }
    }
  },
  methods: {
    newData() {
      //dont work 1rs,
      this.info = ['a','b','c'],['ra','rb','rc']];
    
      // don't work, change 2dn 
      // let urlsecciones = 'seccion/show';
      // axios.get(urlsecciones).then(response => {
      //        this.info = response.data;
      //        console.log(response.data) // run good
      // });
     }
  },        
    components: {
      HotTable
    }
  }
</script>

Solution

  • You can´t reference data properties between them, instead you can use a computed property to handle what you want:

    new Vue({
      el: "#app",
      created: function (){
        this.newData()
      },
      data() {
        return {
          info: [],
        }
      },    
      computed:{
        hotSettings(){
          return {
            data: this.info,
            colHeaders: ['ID','Name',' pain'],
            rowHeaders: true,
            minRows: 2,
            minCols: 3,
          }
        }
      },
      methods: {
        newData() {
           this.info =  [
             ["a", "b", "c"],
             ["ra", "rb", "rc"]
           ]
          // Handle Axios logic here
       }
     },
      components: {
         'hottable': Handsontable.vue.HotTable
      }
    });
     <div id="app">
       <HotTable :settings="hotSettings"></HotTable>
     </div>

    Jsfiddle: https://jsfiddle.net/hansfelix50/069s1x35/