Reputation: 1187
I already installed dataTables using npm i dataTables --save
, and now I can see the dataTable format in my vue component, the problem is the searchbar,paginations are not working well. See my screenshot
code in my component
<table class="table table-hover" id="myTable">
<thead>
<tr>
<th>Room Name</th>
<th>Building</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="room in roomsData" :key="room.id">
<td>{{room.room_desc}}</td>
<td>{{room.bldg}}</td>
<td>
<a href="#" data-toggle="modal" data-target="#exampleModal" @click="editModal(room)">
<i class="fa fa-edit"></i>
</a>
<a href="#" @click="deleteRoom(room.id)">
<i class="fa fa-trash text-red"></i>
</a>
</td>
</tr>
</tbody>
</table>
Methods
myTable(){
$(document).ready( function () {
$('#myTable').DataTable();
});
}
getRoomsDataTable(){
axios.get('/getRoomsDatatable')
.then((res) => {
this.roomsData = res.data
}) .catch((e) => {
console.log(e)
})
},
Upvotes: 2
Views: 1475
Reputation: 4930
After you've instantiated data-tables, you can't update the data in it by updating the dom. You'll need to use the datatable api to trigger a refresh using the latest data.
In a mounted
hook, instantiate dataTables and store the reference.
{
...
data() {
return {
dtRef: null
}
},
mounted() {
this.dtRef = $('#myTable').DataTable();
}
}
After that, clear & update the data once axios returns.
axios.get('/getRoomsDatatable')
.then((res) => {
this.roomsData = res.data
datatable.clear();
datatable.rows.add(res.data);
datatable.draw();
})
You'll need to fiddle with this a little bit, making sure your datatable is configured to correctly display the data.
Alternately, after the data has been fetched, you can destroy the datatable object and re-instantiate it, but you may experience a flash of content before the styling is readded.
Upvotes: 2
Reputation: 1
Try to put only $('#myTable').DataTable();
inside the mounted hook as follows
methods:{
getRoomsDataTable(){
axios.get('/getRoomsDatatable')
.then((res) => {
this.roomsData = res.data
})
.catch((e) => {
console.log(e)
})
},
},
mounted(){
$('#myTable').DataTable();
}
Upvotes: 2