简体   繁体   English

行重新排序后,数据表新添加的行消失

[英]datatable new added row disappears after row-reorder

I have created a datatable where you can add rows if you press the "plus" button. 我创建了一个数据表,如果按“加号”按钮,可以添加行。 An ajax request will post the id of the row where the button was pressed to a URL. ajax请求会将按下按钮的行的id发布到URL。 The problem is if I reorder the rows the new added row disappears. 问题是如果我重新排序行,新添加的行将消失。 I found some tips to do it with json and datable.draw() but I don't know how to do it. 我找到了一些使用json和datable.draw()做的提示,但我不知道该怎么做。 Can someone help? 有人可以帮忙吗?

 $(document).ready(function() { var oTable = $('#tabelle_benutzerHead').DataTable({ responsive: true, "bFilter": false, "info": false, "scrollCollapse": true, "paging": false, rowReorder: true }); oTable.on( 'row-reorder', function ( e, diff, edit ) { var draggedRow = diff[(diff.length - 1)].node; var draggedRowId = $(draggedRow).attr('id'); <!-- dragged and dropped Row --> var PreviousRow = diff[(diff.length - 2)].node; var PreviousRowId = $(PreviousRow).attr('id'); <!-- the row before the dragged and dropped --> $.ajax({ type: "POST", url: "myurl.com", data: { draggedRowId, PreviousRowId } }); }); }); var Uhrzeit; var SpNr; var Platz; var Heimmannschaft; var Gastmannschaft; var tableRowAppend = '<tr><td>' + Uhrzeit + '</td><td>' + SpNr + '</td><td>' + Platz + '</td><td>'+ Heimmannschaft + '</td><td>'+ Gastmannschaft + '</td><td><button class="btn btn-default AddDaten" type="button"><i class="glyphicon glyphicon-plus-sign"></i></button></td></tr>'; $('#tabelle_benutzerHead').delegate('button.AddDaten', 'click', function() { var row = $(this).closest('tr'); // get the parent row of the clicked button $(tableRowAppend).insertAfter(row); // insert content var rowId = $(row).attr('id'); // clicked RowId $.ajax({ type: "POST", url: "myurl.com", data: { rowId } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table class="table display" id="tabelle_benutzerHead" cellspacing="0" width="100%"> <thead data-spy="affix" data-offset-top="100"> <tr> <th>Uhrzeit</th> <th>SpNr</th> <th>Platz</th> <th>Heimmannschaft</th> <th>Gastmannschaft</th> <th>Freispiele</th> </tr> </thead> <tbody> <tr id="Row1"> <td>08:00</td> <td>134</td> <td>Platz 1</td> <td>Team 5</td> <td>Team 3</td> <td><button class="btn btn-default AddDaten" type="button"><i class="glyphicon glyphicon-plus-sign"></i></button></td> </tr> <tr id="Row2"> <td>09:00</td> <td>76</td> <td>Platz 3</td> <td>Team 7</td> <td>Team 1</td> <td><button class="btn btn-default AddDaten" type="button"><i class="glyphicon glyphicon-plus-sign"></i></button></td> </tr> <tr id="Row3"> <td>17:30</td> <td>45</td> <td>Platz 11</td> <td>Team 2</td> <td>Team 9</td> <td><button class="btn btn-default AddDaten" type="button"><i class="glyphicon glyphicon-plus-sign"></i></button></td> </tr> </tbody> </table> 

You need to add the row using the DataTables api, so it knows about it. 您需要使用DataTables api添加行,因此它知道它。 Do not use JQuery alone. 不要单独使用JQuery。

Refer to https://datatables.net/reference/api/row.add() 请参阅https://datatables.net/reference/api/row.add()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM