最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - add datatable row to top or bottom of the table? - Stack Overflow

programmeradmin6浏览0评论

My datatable by default sorting for 1 column. Dynamically I am adding row to the table, row is adding sometimes in middle of table, sometime add to 2 page .. etc because first column is name.

But I want add top of the table or last of the table. Can we do that? How?

var t = $('#tblRoster').DataTable();
t.row.add( [memberCustomerName, memberPosition,memberMasterCustomer,
    memberCustomerEmail, endDate,'<a href="#" class="linkbutton 
    padding-left-20" id="addCancelButton">Remove</a>']).draw( false );

My datatable by default sorting for 1 column. Dynamically I am adding row to the table, row is adding sometimes in middle of table, sometime add to 2 page .. etc because first column is name.

But I want add top of the table or last of the table. Can we do that? How?

var t = $('#tblRoster').DataTable();
t.row.add( [memberCustomerName, memberPosition,memberMasterCustomer,
    memberCustomerEmail, endDate,'<a href="#" class="linkbutton 
    padding-left-20" id="addCancelButton">Remove</a>']).draw( false );
Share Improve this question edited Dec 20, 2016 at 15:20 Gyrocode. 58.9k16 gold badges156 silver badges191 bronze badges asked Dec 20, 2016 at 3:53 James123James123 11.7k70 gold badges198 silver badges355 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Position of newly added data in entirely based on the ordering condition applied to the DataTable.

Suppose you want to add Row at last of the DataTable. So you need to take following steps:

  1. Add New Row in DataTable.

  2. Draw DataTable with specified order.

  3. Move the page focus to last page of the DataTable (optional).

So you can achieve it this way.

$('#AddRow').on('click', function() {
     var row = ['testing', 'testing', 'testing', 'testing', 'testing', 'testing'];
     table.row.add(row).draw(false);
     table.order([1, 'asc']).draw();
     table.page('last').draw(false);
});

Here is its Demo

发布评论

评论列表(0)

  1. 暂无评论