0

Im using jQuery datatables for displaying my data in my users component in Vue.js, but when I run my code it displays the data but it has some text that says "No data found". Can someone help me with this? I really don't have any idea because I'm new in this Frontend tools.

table

Users.vue:

<template>
  <table id="table" class="table table-striped table-bordered">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Type</th>
        <th>Created</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="user in users" :key="user.id">
        <td>{{ user.id }}</td>
        <td>{{ user.name }}</td>
        <td>{{ user.email }}</td>
        <td>{{ user.type }}</td>
        <td>{{ user.created_at }}</td>
      </tr>
    </tbody>
  </table>
</template>


<script>
export default {
  data() {
    return {
      users: [],
      form: new Form({
        id: "",
        name: "",
        email: "",
        password: "",
        type: ""
      })
    };
  },
  methods: {
    loadUsers() {
      axios.get("api/user").then(({ data }) => (this.users = data));
    }
  },
  created() {
    console.log("Component mounted.");
    this.loadUsers();
  }
};

$(document).ready(function() {
  $("#table").DataTable({});
});
</script>

2 Answers 2

1

Probably, you should init your widget DataTable after receiving data from api, more precisely in then method.

axios.get("api/user").then(({ data }) => {
  this.users = data;
  this.$nextTick(() => {
     $("#table").DataTable({});
  });
});

Explanation about Vue.$nextTick:

Defer the callback to be executed after the next DOM update cycle. Use it immediately after you’ve changed some data to wait for the DOM update. This is the same as the global Vue.nextTick, except that the callback’s this context is automatically bound to the instance calling this method.

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

10 Comments

how could I do that sir? can you show me some few codes so that I can play with it?
Oh ,yea, and don't forget to wait for DOM changes through this.$nextTick;
It works sir but it needs to be reloaded in order to work and it throws me an error Vue warn]: Error in nextTick: "TypeError: $(...).DataTable is not a function" then after I reload its gone. then when I click to other component then go back, it shows again the error
Can you provide how you include this widget into the project?
Make sure that your widget is loaded in moment of initialization. Then reply.
|
0

axios.get("api/user").then(({ data }) => {
  this.users = data;
  $(function() {
     $("#table").DataTable({});
  });
});

Comments

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.