【发布时间】:2017-05-16 16:59:09
【问题描述】:
我想知道在通过 api 从数据库加载数据时如何使用相同的方法。在第一次数据表加载正常。但是,当我添加新记录时,我需要使用新记录再次加载我的数据表。这是我的html代码:
<table class="table table-striped table-bordered table-hover" id="dataTables-eventos">
<thead>
<tr class="tbheader">
<th class="text-center">#</th>
<th>Nome do Evento</th>
<th class="text-center">Editar</th>
<th class="text-center">Deletar</th>
</tr>
</thead>
<tbody>
<tr v-for="ev in eventos" :key="ev.id" track-by="id">
<td class="text-center">{{ ev.id }}</td>
<td>{{ ev.name }}</td>
<td class="text-center"><a class="cursorpointer" v-on:click="showMessage()"><span class="glyphicon glyphicon-edit"></span></a></td>
<td class="text-center"><span class="glyphicon glyphicon-trash"></span></td>
</tr>
</tbody>
</table>
这是我的 VueJS 代码:
mounted() {
this.getEventos();
},
methods: {
getEventos() {
axios.get('/api/eventos')
.then((response) => {
this.eventos = response.data})
.then((response) => {
$('#dataTables-eventos').DataTable({
responsive: true,
"aaSorting": [[ 1, "asc" ]],
"aoColumnDefs": [
{ "bSortable" : false, "aTargets": [0,2,3] },
{ "searchable": false, "aTargets": [2,3] }
],
language: {
url: '/js/dataTables/localization/pt_BR.json'
}
});
});
},
addNewRecord() {
axios.post('/api/eventos', { nomeEvento: this.nomeEvento });
}
那么,在我的数据库中添加(或编辑)新记录后,如何重新加载我的数据表以便查看更改?
【问题讨论】:
-
能否提供整个 vue 应用的代码?
-
好像
axios.post(...).then(() => this.getEventos()) -
嗨@RoyJ,感谢您的回复,但是您的方法不起作用。数据表已经创建,所以我不能再次调用 this.getEventos()。
-
抱歉,stackoverflow 不允许我添加整个 vue 代码。
-
也许这会有所帮助? github.com/njleonzhang/vue-data-tables
标签: vue.js