Javascript 使用 DataTable() 更新一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30709958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:37:08  来源:igfitidea点击:

Update a row using DataTable()

javascriptjquerydatatables

提问by filur

I would like to know the correct way to update/redraw a table row using the new API. Old questionssuggest table.fnUpdate. Should I still be using the old API for this?

我想知道使用新 API 更新/重绘表格行的正确方法。老问题提示table.fnUpdate。我还应该为此使用旧的 API 吗?

The new APItells me nothing about updating rows.

新的API告诉我任何关于更新行。

Thanks in advance!

提前致谢!

回答by akosel

I had a similar issue recently. I believe row().data() is what you are looking for https://datatables.net/reference/api/row%28%29.data%28%29

我最近遇到了类似的问题。我相信 row().data() 是你正在寻找的https://datatables.net/reference/api/row%28%29.data%28%29

For example:

例如:

table.row( 0 ).data( newData ).draw();


Alternatively, you can use row().invalidate() https://datatables.net/reference/api/row%28%29.invalidate%28%29

或者,您可以使用 row().invalidate() https://datatables.net/reference/api/row%28%29.invalidate%28%29

var initialData = [new Data(), new Data()];
var table = $('#foo').Datatable({
  data: initialData
});
initialData[0].bar = 5;
table.row(0).invalidate().draw();

This is more useful if you are deriving your data from an external data source.

如果您从外部数据源派生数据,这会更有用。

回答by Vahap Gencdal

$(document).ready(function () {
    $('#dataTable').on('click', '.update', function () {
        var pageParamTable = $('#dataTable').DataTable();
        var tableRow = pageParamTable.row($(this).parents('tr'));
        var rData = [
            yourdata1,
            yourdata2,
            '<a href="#" data-href="1" class="update">Update</a>',
            '<a href="#" data-href="2" class="delete">Delete</a>'
        ];
        pageParamTable
                .row( tableRow )
                .data(rData)
                .draw();
    });
});

We are using this code our projects you can use this.

我们正在使用此代码我们的项目您可以使用此代码。