2

我正在https://github.com/matfish2/vue-tables使用Laravel

这是vue代码:

    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 }],
}
});

如何从数据库中获取 tableData 的数据?Vue资源?我有一条路线/api/footage可以为我提供以下信息

[
{
"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"
}
]

现在,用户和素材具有一对多的关系。我将如何向用户展示每个条目?(也是编辑和删除的ID)

这是刀片代码

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

提前谢谢你。

4

2 回答 2

3

您可以添加一个ready()函数来在构建组件时调用 API:

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

您可能需要根据 API 响应的格式调整代码。如果您使用的是 es2016,则更清洁的版本:

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

你应该vue-resource在此之前包括,是的。这使您可以使用this.$http

根据@BillCriswell,您可以在created()函数中执行此操作以更快地触发 API 调用

于 2016-04-04T13:34:58.473 回答
0

在获取异步数据时,使用v-if组件上的一些“已加载”标志以确保模板的顺利编译。

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

见:https ://github.com/matfish2/vue-tables/issues/20 ,最后评论

于 2016-04-05T18:53:41.340 回答