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

1
  • 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. Commented Mar 31, 2018 at 19:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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()
}

2 Comments

next() method not get trigger therefore i di not getting data
Ok, do you know about lifecycle hooks? You can trigger next() in the created() hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.