0

guys, I m new to Vue so don't know how to solve this let me first show code then the problem, I m using Vue-Good-table here is link https://xaksis.github.io/vue-good-demos/#/simple-table

<vue-good-table
  title="Product List"
  :columns="columns"
  :rows="rows"
  :paginate="true"
  :lineNumbers="true" />

export default {
data(){
return {
  columns: [
    {
      label: 'productname',
      field: 'product_name',
      filterable: true,
    },
    {
      label: 'productdesc',
      field: 'product_desc',
     // type: 'number',
      html: false,
      filterable: true,
    },

  ],
  rows:[],//get axios return value
 };
},
 methods:{
  next() {
    var _this=this
     this.$http.get('http://localhost:3000/api/companyproducts')
            .then(function (response) {

           _this.rows=response.data
            })
            .catch(function (error) {

                });
    }
  }
}

now my problem is that how i can append this axios responded value with rows of good table

Rocky
  • 319
  • 3
  • 8
  • 23
  • I will mention that you are replacing the content of the table with this call, NOT appending. To append, use the `rows.push( response.data )` method. – parker_codes Mar 31 '18 at 19:21

2 Answers2

2

I'm guessing with the code you have, the view does not respond after the http.get completes.

You may be able to do it by configuring rows as a computed property, since this watches it's dependents (i.e row_data) for changes

computed: {
  rows: function () { 
    return this.row_data;
  }
}
...
data(){
  return {
    columns: [
    ...
    ],
  row_data:[]
 };
}
...
methods: {
  next() {
    var _this=this
    this.$http.get('http://localhost:3000/api/companyproducts')
      .then(function (response) {
         _this.row_data = response.data
      })  
  }
}
...
created: function() {
  this.next()
}
Richard Matsen
  • 20,671
  • 3
  • 43
  • 77
2

To "append axios data" you have to install axios first in your project.

So first install axios to your project:

npm install --save axios

Then transform your next method into the follow:

  next() {
    axios.get('http://localhost:3000/api/companyproducts')
      .then(response => {
         this.rows = response.data
      }) 
      .catch(error => console.log(error))
  }

Note that you have also to import axios.So when the script tag starts use:

import axios from 'axios'

Axios is a great package you can learn more here

As i can see in your code you are not using axios but vue-resource.Vue-resource is also good package so you can learn more here

Roland
  • 24,554
  • 4
  • 99
  • 97