简体   繁体   English

ajax成功后刷新ajax datatable

[英]refresh ajax datatable after ajax success

I use DataTable. 我使用DataTable。 I send data to datatable onclick in json file. 我将数据发送到json文件中的datatable onclick。 The problem is When I use $('.datatable-ajax-source table').dataTable().fnDestroy(); 问题是当我使用$('.datatable-ajax-source table').dataTable().fnDestroy(); I loose the filter, row and pagination. 我松开过滤器,行和分页。 I get just the table like below: 我得到的表格如下:

在此处输入图片说明

This my fuction javascript: 这是我的功能javascript:

$('#ButtonPostJson').on('click', function() {           
    var forsearch = $("#searchItem").val();
    $.ajax({
        processing: true,
        serverSide: true,
        type: 'post',
        url: 'path.json',
        dataType: "json",
        success: function(data) {
            $.each(data, function(i, data) {
                var body = "<tr>";
                body += "<td>" + data.name + "</td>";
                body += "</tr>";
            $('.datatable-ajax-source table').append(body);
        });

        /*DataTables instantiation.*/
        $('.datatable-ajax-source table').dataTable().fnDestroy();
    },
    error: function() {
        alert('Fail!');
    }
});
});

And when I replace $('.datatable-ajax-source table').dataTable().fnDestroy(); 当我替换$('.datatable-ajax-source table').dataTable().fnDestroy(); with $('.datatable-ajax-source table').dataTable(); $('.datatable-ajax-source table').dataTable(); I get this table who I want 我得到想要的这张桌子

在此处输入图片说明

But when I want to search for a new term the old data is still there,I have to refrech the page manually for doing a new search. 但是,当我想搜索一个新词时,旧数据仍然存在,我必须手动刷新页面以进行新搜索。 How can I refresh or update my datatable? 如何刷新或更新数据表?

I'm using fnStandingRedraw to refresh the server side data and it works like a charm (unfortunately its deprecated though). 我正在使用fnStandingRedraw刷新服务器端数据,它的工作原理像一个超级按钮(不幸的是,它已弃用)。 You can use https://datatables.net/plug-ins/api/fnStandingRedraw 您可以使用https://datatables.net/plug-ins/api/fnStandingRedraw

var ajaxSourceDataTable;

//define datatable
ajaxSourceDataTable = $('.datatable-ajax-source table').dataTable() 

//use this code to redraw/refresh datatable
ajaxSourceDataTable.fnStandingRedraw();

You're missing the best parts of dataTables using it in this manner. 您会丢失以这种方式使用dataTables的最佳部分。 Don't build your HTML manually.... use the .rows.add() (and related) functions to redraw your table as needed. 不要手动构建HTML。...根据需要使用.rows.add() (及相关)函数来重绘表。

Try this: 尝试这个:

https://jsfiddle.net/xgpgLydb/ https://jsfiddle.net/xgpgLydb/

var tbl = $('#example').DataTable({
    columns: [{
    data: 'Prop1',
    title: 'Example Prop'
  },{
    data: 'Prop2',
    title: 'Example Prop 2'
  }]
});

var req = $.ajax({
    url: '/echo/json/',
    method: 'post',
    data: {
        json: JSON.stringify([{
            Prop1: 'Foo',
          Prop2: 'Bar'
        },{
            Prop1: 'Fred',
          Prop2: 'Flinstone'
        }]),
        delay: 1
    }
});

req.done(function(response) {
    tbl.rows().remove().draw();
    tbl.rows.add(response).draw();
});

这个链接在jQuery DataTables中刷新数据确实很有帮助,我从中得到启发来解决我的问题。

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

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