简体   繁体   English

在 laravel 中 ajax 成功后如何刷新数据表?

[英]How refresh datatable after ajax success in laravel?

I use datatable to show my data.我使用数据表来显示我的数据。 I store data in ajax request & append that after a successful response.在成功响应后,我将数据存储在 ajax 请求和 append 中。 Data is appended successfully.数据附加成功。 But the problem is datatable is not updated when appending an item.但问题是附加项目时数据表未更新。 If I reload the page then It works.如果我重新加载页面,那么它可以工作。 See image red mark.见图像红色标记。 Data is appended but datatable is not updated.附加了数据,但未更新数据表。 在此处输入图像描述

Here is my ajax success response这是我的 ajax 成功响应

 success: function(response) {
   if (response.status == true) {
      var href = "{{ route('home') }}/";
       $('#dataTable tbody').prepend( "<tr class='data" + response.data.id + "'>" +
           "<td><img src='" + href + "storage/sliders/" + response.data.image + "'></td>" +
            "<td>" + response.data.name + "</td>" +
            "<td>" + response.data.title+ "</td>" +
             "</tr>" ); 
           }
      }

How to auto-refresh datatable after ajax success? ajax成功后如何自动刷新数据表?

try like bellow:-尝试如下: -

var table = $('#table_id').DataTable({

//ajax method that gets data from databases. 

});

use this method to refresh your table:使用此方法刷新您的表:

table.ajax.reload();

if it gives an error like如果它给出一个错误

DataTables warning: table id=table_id- Invalid JSON response数据表警告:表 id=table_id-无效的 JSON 响应

then use the below function before adding data to the table to solve the error:然后在向表中添加数据之前使用下面的 function 来解决错误:

table.clear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM