Jquery 在数据表中插入新行时如何保持滚动位置

Jquery 在数据表中插入新行时如何保持滚动位置,jquery,jquery-datatables,Jquery,Jquery Datatables,我正在使用DataTables插件来处理我的表。 我希望将JSON对象设置为表的数据源,并在每次更改该对象时更新内容 要初始化我的表,我使用以下代码: var obj = []; // if I make start initialization of the object - the row in a table appears. //var obj = [{"Name": "name1", "Id": "id1", "Value":"value1"}];

我正在使用DataTables插件来处理我的表。 我希望将JSON对象设置为表的数据源,并在每次更改该对象时更新内容

要初始化我的表,我使用以下代码:

     var obj = [];

     // if I make start initialization of the object - the row in a table appears.
     //var obj = [{"Name": "name1", "Id": "id1", "Value":"value1"}];

     var table;

        $(document).ready(function () {

            table = $('#example').dataTable({
                "sPaginationType": "full_numbers",
            "sScrollY": "250px",
            "aaSorting": [],
            "aaData": obj,
            "aoColumns": [
            { "mDataProp": "Name" },
            { "mDataProp": "Id" },
            { "mDataProp": "Value" }
        ]
        });
    }
    );
要更新我的JSON对象,请执行以下操作:

function updateObject() {
    var response = $.ajax({
        type: "GET",
        datatype: "json",
        url: myURL,
        success: function (result) {
            obj = jQuery.parseJSON(result);
            //table.fnDraw(); - tried this but it doesn't work
        }
    });
}
我从服务器获取的JSON数据:

 [{"Name": "name1", "Id": "id1", "Value":"value1"}, 
     {"Name": "name2", "Id": "id2", "Value":"value2"},...]
从服务器获取新数据后是否可以刷新表内容

更新:

table.fnClearTable();
table.fnAddData(obj, false);
table.fnStandingRedraw();

插入新行后,当前页面将丢失,当前滚动位置也将丢失fnStandingRedraw方法有助于保持页面位置。但滚动会跳到第一行。有没有办法计算当前滚动位置,以便在更新()或其他解决方案后跳回?

我认为这可以帮助您。您应该首先删除所有数据,然后再次添加新数据,文档将被删除


我想这对你有帮助。您应该首先删除所有数据,然后再次添加新数据,文档将被删除

我也有类似的情况。 所以我最终解决了这个问题:

function UpdateDatatable(data, $table) {
    $.ajax({
        type: "GET",
        dataType: "JSON",
        url: "../biz/datahandler.ashx",
        data: ({data: data}),
        async: true,
        error: function (xhr, ajaxOptions, thrownError) {
            /* manage the error */
        },
        success: function (dataget) {
            /* maange the success */
            $table.dataTable({
                ...
            })
    });
}
我也有类似的情况。 所以我最终解决了这个问题:

function UpdateDatatable(data, $table) {
    $.ajax({
        type: "GET",
        dataType: "JSON",
        url: "../biz/datahandler.ashx",
        data: ({data: data}),
        async: true,
        error: function (xhr, ajaxOptions, thrownError) {
            /* manage the error */
        },
        success: function (dataget) {
            /* maange the success */
            $table.dataTable({
                ...
            })
    });
}

我还使用fnStandingRedraw()插件在更新后保持分页位置。但我不能保持滚动位置。也许你知道如何处理这个问题?不是真的,让我看看问题是什么?你不应该删除所有数据并插入新数据。对于拥有大型数据集并试图在scroll上添加数据的人来说,这不是一个好的解决方案。我还使用fnStandingRedraw()插件在更新后保持分页位置。但我不能保持滚动位置。也许你知道如何处理这个问题?不是真的,让我看看问题是什么?你不应该删除所有数据并插入新数据。对于拥有大型数据集并试图在scroll上追加数据的用户来说,这不是一个好的解决方案。您找到解决此问题的方法了吗?找到解决此问题的方法了吗?