【问题标题】:Dynamically adding rows is NOT adding on the top of a table动态添加行不是在表格顶部添加
【发布时间】:2015-11-05 09:20:03
【问题描述】:

在 Jquery Datatable 中,我尝试动态添加行。它是动态添加的,但它没有添加到表格的顶部。

谁能帮我使用 Jquery Datatable 在表格顶部动态添加行。

这是小提琴http://jsfiddle.net/inDiscover/yo83k3z9/

内联代码

<!DOCTYPE html>
<html>
<head>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css">
<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-1.7.2.min.js"></script>

<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
var giCount = 1;

jQuery(document).ready( function () {
    jQuery('#table_id').DataTable();
} );

function fnClickAddRow() {
    jQuery('#table_id').dataTable().fnAddData( [
        giCount+".1",
        giCount+".2",
        ] );

    giCount++;
}


</script>
</head>
<body>
<a onClick="fnClickAddRow()">Add Row</a>
<table id="table_id" class="display">
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
        </tr>
    </thead>
    <tbody>        
    </tbody>
</table>
</body>
</html>

【问题讨论】:

标签: jquery datatables


【解决方案1】:

顺序由第一列设置。默认情况下,它按升序排序,因此新内容似乎被附加到表格的底部。如果您将排序顺序更改为降序,它将按您的要求工作:

jQuery('#table_id').DataTable({
    "order": [[ 0, "desc" ]]
});

Updated example

【讨论】:

    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多