Mike
Mike

Reputation: 552

Vue.JS Vue-Tables Laravel Relationship

I am using https://github.com/matfish2/vue-tables with Laravel.

This is the vue code:

    Vue.use(VueTables.client, {
        compileTemplates: true,
        highlightMatches: true,
       pagination: {
        dropdown:true,
         chunk:5
        },
        filterByColumn: true,
        texts: {
            filter: "Search:"
        },
        datepickerOptions: {
            showDropdowns: true
        }
    });

    new Vue({
        el: "#people",
        methods: {
            deleteMe: function(id) {
                alert("Delete " + id);
            }
        },
        data: {
            options: {
                columns: ['created_at', 'name', 'profession', 'footage_date', 'type', 'link', 'note'],
                dateColumns: ['footage_date'],
                headings: {
                    created_at: 'Added',
                    name: 'Name',
                    profession: 'Profesion',
                    footage_date: 'Footage Date',
                    type: 'Type',
                    link: 'Link',
                    note: 'Note',
                    edit: 'Edit',
                    delete: 'Delete'
                },
                templates: {

                    edit: "<a href='#!/{id}/edit'><i class='glyphicon glyphicon-edit'></i></a>",
                    delete: "<a href='javascript:void(0);' @click='$parent.deleteMe({id})'><i class='glyphicon glyphicon-erase'></i></a>"
                },
            },
            tableData: [{ InsertDataHere }],
}
});

How do I get the data from DB for tableData ? Vue-resources? I have a route /api/footage that gives me the following

[
{
"id": 2,
"user_id": 11,
"profession": "profession",
"type": "GvG",
"footage_date": {
"date": "2016-04-01 00:00:00.000000",
"timezone_type": 2,
"timezone": "GMT"
},
"link": "some link",
"note": "description",
"created_at": "1 hour ago",
"updated_at": "2016-04-03 23:06:32"
}
]

Now, User and Footage have a one to many relationship. How would I go about to show the user for each entry as well? ( also the ID for edit and delete )

This is the blade code

 <div id="people" class="container">
   <v-client-table :data="tableData" :options="options"></v-client-table>
 </div>

Thank you in advance.

Upvotes: 2

Views: 1895

Answers (2)

Matanya
Matanya

Reputation: 6346

When fetching async data use v-if along with some "loaded" flag on the component to ensure smooth compilation of templates.

<v-client-table v-if="loaded" :data="tableData" :options="options"></v-client-table>

See: https://github.com/matfish2/vue-tables/issues/20, last comment

Upvotes: 0

Jeff
Jeff

Reputation: 25221

You can add a ready() function to call the API when the component is built:

ready:function(){
    this.$http.get('/api/footage')
        .then(function(response){
            this.tableData = response.data
        }.bind(this))
}

You may have to tweak the code based on the format of your API response. Cleaner version if youre using es2016:

ready(){
    this.$http.get('/api/footage')
        .then(({data})=>{
            this.tableData = data
        })
}

You should include vue-resource before this, yes. That allows you to use this.$http

As per @BillCriswell you could do this in the created() function to fire off the API call even sooner

Upvotes: 3

Related Questions