0

I am trying to play around with jquery datatable in vue js , however when I change 'Show entries' or enter something in 'Search' no records are displayed Any hint of what am I doing wrong ( I'm not very experienced with Vue Js ) will be highly appreciated Below the code (got it from an example, it seems to load the data in the table ok) :

 template>
  <div class="Example">
      <h2>Implement jQuery DataTable in Vue Js</h2>
      <table class="display table-bordered nowrap" cellspacing="0" width="100%" id="datatable">
        <thead>
          <tr>
            <th>ID</th>
            <th>Product Title</th>
            <th>Product Price</th>
            <th>Created On</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in products" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.product_title}}</td>
            <td>{{item.product_price}}</td>
            <td>{{item.created_at}}</td>
          </tr>
          
        </tbody>
      </table>    
  </div>
</template>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script> 
<script>
import 'jquery/dist/jquery.min.js';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'datatables.net-dt/js/dataTables.dataTables'
import 'datatables.net-dt/css/jquery.dataTables.min.css'
import axios from 'axios';
import $ from 'jquery';
export default {
  mounted(){
    axios
    .get("https://www.testjsonapi.com/products/")
    .then((response)=>
    {
      this.products = response.data;
        $('#datatable').DataTable();
    })
  },
  data: function() {
        return {
            products:[]
        }
    },  
}
</script>

1 Answer 1

0

I just stumble upon this post:

Vuejs and datatables: table empty when using v-for to fill data

...which answers it

...you can call table.destroy() to destroy the current instance before reinitializing it. The key is to delay the reinitialization until $nextTick() so that Vue can flush the DOM of the old DataTables

I left the answer here to whom it may concern.

watch: {
  products(val) {
    $('#datatable').DataTable().destroy();
    this.$nextTick(()=> {
      $('#datatable').DataTable()
    });
  }
}  
Sign up to request clarification or add additional context in comments.

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.