如何使用新的JSON数据手动更新数据表 [英] How to manually update datatables table with new JSON data

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

问题描述

我正在使用插件 jQuery数据表,并加载我在DOM底部加载的数据,并启动插件以这种方式:

I am using plugin jQuery datatables and load my data which I have loaded in DOM at the bottom of page and initiates plugin in this way:

var myData = [
    {
        "id": 1,
        "first_name": "John",
        "last_name": "Doe"
    }
];

$('#table').dataTable({
    data: myData
        columns: [
        { data: 'id' },
        { data: 'first_name' },
        { data: 'last_name' }
    ]
});

现在。在执行某些操作后,我想使用ajax获取新数据(但不是在数据表中构建ajax选项 - 不要误会我),并使用这些数据更新表。我如何使用数据表API?文档非常混乱,我找不到解决方案。任何帮助将非常感谢。谢谢。

Now. after performing some action I want to get new data using ajax (but not ajax option build in datatables - don't get me wrong!) and update the table with these data. How can i do that using datatables API? The documentation is very confusing and I can not find a solution. Any help will be very much appreciated. Thanks.

推荐答案

解决方案:(注意:此解决方案适用于数据表版本1.10.4那个时候)没有遗留版本):

SOLUTION: (Notice: this solution is for datatables version 1.10.4 (at the moment) not legacy version):

var datatable = $('#table').dataTable().api();

$.get('myUrl', function(newDataArray) {
    datatable.clear();
    datatable.rows.add(newDataArray);
    datatable.draw();
});

API参考资料:

https://datatables.net/reference/api/clear()

https://datatables.net/reference/api/rows.add()

https://datatables.net/reference/api/draw()

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

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