【问题标题】:Why is datatable adding row the top of my table? [duplicate]为什么数据表在我的表格顶部添加行? [复制]
【发布时间】:2012-05-24 18:56:16
【问题描述】:

可能重复:
using jquery datatable plugin, does fnAddAdd() add rows to the top or bottom of a html tabld

我正在尝试在我的应用程序中使用数据表并查看动态添加行的示例。它有效但很奇怪。它在我现有的行上方添加行。我该如何解决这个问题?

事实上,请看这个演示页面:

http://datatables.net/examples/api/add_row.html

然后单击“单击以添加新行”。你会看到 1.1, 1.2, 1.3, 1.4 被添加到 allan, allan, allan, allan 行上方。

我如何告诉数据表将该行添加到艾伦下方而不是上方。

【问题讨论】:

  • @Alexis:它不是重复的。该解决方案对我不起作用,所以我再次发布。如果我将 false 作为第二个参数传递,则不会添加行本身。

标签: javascript jquery datatables


【解决方案1】:

我对发布在以下位置的答案感到非常惊讶:

Using jQuery DataTables plugin, does fnAddData() add rows to the top or bottom of an html table?

不知道为什么会被接受。

@fbfcn 的答案也可能是从那里引用的。不过我不确定!

事实上,如果您将 false 作为第二个参数传递,则不会添加任何行本身!

正确答案和唯一需要的更改是将“bSort”参数设置为 false。设置它,你就完成了!

回答它以供将来参考。

【讨论】:

    【解决方案2】:

    添加新行函数有一个可选的布尔值,表示表格是否被重绘。因此,您在演示中看到的内容按第 1 列排序,当添加新项目时,结果将被重新排序。如果您传递 false ,则重绘将不会发生并且会按照您的预期运行,但不会被排序。您可以通过降序对第 1 列进行排序然后添加行来查看正在发生的情况。

    $(this).fnAddData(data, false);
    

    来自API

    Input parameters:   
    {array|object}: The data to be added to the table. This can be:
                    1D array of data - add a single row with the data provided
                    2D array of arrays - add multiple rows in a single call
                    object - data object when using mDataProp
                    array of objects - multiple data objects when using mDataProp
    {bool} [default=true]: redraw the table or not
    

    【讨论】:

    • 如果我通过 false,则不会添加行本身。这就是我所做的: $('#purForm').dataTable().fnAddData(['', '', '', '', '', ''], false);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2019-08-05
    • 2010-11-24
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多