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

时间:2016-12-08 12:53:49

标签: javascript json datatables-1.10

我创建了一个数据表,如果按下" plus"按钮。 ajax请求会将按下按钮的行的id发布到URL。问题是如果我重新排序行,新添加的行将消失。我发现了一些使用json和datable.draw()的技巧,但我不知道该怎么做。有人可以帮忙吗?



$(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
				}		
			});
    });	
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要使用DataTables api添加行,以便它知道它。不要单独使用JQuery。

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