如何刷新数据表 [英] How to refresh DataTables

查看:29
本文介绍了如何刷新数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DataTables 插件来使我的表格具有交互性.

I am using DataTables plugin to make my table interactive.

该表格在页面上由 PHP 回显.

The table is echo'd by PHP on the page.

当我向数据库添加记录时,我正在使用 jQuery load() 加载表,但这会破坏 DataTables.

When I add a record to the DB I am loading the table using jQuery load() but this breaks DataTables.

如何在保持 DataTables 完整的同时更新表格?

How can I update the table while still keeping DataTables Intact?

注意:我使用 DOM 作为数据源而不是服务器端处理.

Note: I am using DOM as the data source and not server side processing.

推荐答案

如果要完全重新加载整个表,请将初始数据表初始化代码包装在一个函数中.在页面加载时调用该函数.当用 ajax 完全替换表时,您可能应该删除由插件创建的表父 div 作为它创建的所有非表 DOm 元素的包装器.如果表 ID="example" ,包装器 id="example_wrapper".

If doing a complete reload of the whole table, wrap your initial datatables initialization code in a function. Call that function on page load. When replacing the table completely with ajax you likely should remove the table parent div that is created by plugin as a wrapper for all the non table DOm elements it creates. If table ID="example" , wrapper id="example_wrapper".

这里有足够的代码可能会让您顺利进行.有一些简单的方法可以只更新行,但由于请求是重新加载完整的表,所以我遵循了

Here's enough code will likely get you well on your way. There are easy ways to only update rows but since request is for a complete table reload I've followed that

function initDataTables(){
    $('#myTable').datatables({/* put all current options here*/})

}


/* within ready event of pageload */

$(function(){
    initDataTables();
    /* all other page load code*/

});

/* use $.get to reload table */

$.get( tableUpdateUrl, data, function( returnData){

    $('#myTable').parent().replaceWith(returnData);

    /* re-initalize plugin*/

    initDataTables();
}); 

这篇关于如何刷新数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆