0

I want to display the data in table using vue-datatable but it is not working. Here is the code that i am using ,i am getting data in json format but it is not appending with datatable can anyone help me in this or any better way to do this. Thanks in Advance

////main.js//// import Vue from 'vue' import { VuejsDatatableFactory } from 'vuejs-datatable'; Vue.use( VuejsDatatableFactory );

<script>
  import axios from 'axios';
  export default {
    data: () => ({
      drawer: null,
       columns: [
                    {label: 'id', field: 'recData.id'},
                    {label: 'user ID', field: 'recData.userId'},
                    {label: 'Title', field: 'recData.title'},
                    {label: 'Description', field: 'recData.body'},
                ],
                recData:[]
    }),

    created() {
              //api for getting posts details
            const token = 'https://jsonplaceholder.typicode.com/posts';
            axios({
                    method: 'get',
                    url: token,
                })
                .then((response) => {
                    this.recData = response.data;
                })
                .catch((error) => {
                    this.errors = error;
                })   

    },
  }
</script>
   <v-content>
      <v-container class="fill-height" fluid>
        <v-row align="center" justify="center">
          <span class="mb-5">{{ recData }}</span>
         <datatable :columns="columns" :data="recData"></datatable>
        </v-row>
      </v-container>
    </v-content>
enter image description here
HaryanviDeveloper
  • 1,005
  • 1
  • 8
  • 15

1 Answers1

0

The problem is that you are trying to access in the array the object which is inside of another object which is not existed so try the below code try to access the object of an array like this {label: 'id', field: 'id'} not like that {label: 'id', field: 'recData.id'} this this.recData = data line need to be changed to this this.recData = data.data

Try this

    <script>
    import axios from 'axios';
    import Vue from "vue";
    import { VuejsDatatableFactory } from "vuejs-datatable";

    Vue.use(VuejsDatatableFactory);
    export default {
        data() {
            return {

                      drawer: null,
            columns: [
                    {label: 'id', field: 'id'},
                    {label: 'user ID', field: 'userId'},
                    {label: 'Title', field: 'title'},
                    {label: 'Description', field: 'body'},
                ],
                recData:[]

            };
        },
      created() {
                //api for getting posts details

                axios.get('https://jsonplaceholder.typicode.com/posts')
                .then(({ data }) => (this.recData = data.data));

               },
        };
    </script>
Basharmal
  • 1,313
  • 10
  • 30