2

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

enter image description here

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I didint notice that sir that I was filling the table before datatable was initialized. Thanks for this
2

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();
}

3 Comments

I get the same output sir. why is this happening?
i think you're missing to add the datatables css file
someone figured it out. Thanks for the answer sir!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.