2
I have the following situation: I want to get to the end of the table to make an Ajax request and return another 10 lines in my table. For example:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table {
width: 100%;
}
thead, tbody, tr, td, th { display: block; }
thead{
background: #ccc url(https://www.devfuria.com.br/html-css/tabelas/bar.gif) repeat-x left center;
}
tr:after {
content: ' ';
display: block;
visibility: hidden;
clear: both;
}
thead th {
height: 30px;
text-align: left;
}
tbody {
height: 120px;
overflow-y: scroll;
}
thead {
/* fallback */
}
tbody td, thead th {
width: 19.2%;
float: left;
}
</style>
<title></title>
</head>
<body>
<table id="table" class="tabela">
<thead>
<tr>
<th>Make</th>
<th>Model</th>
<th>Color</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
</tbody>
</table>
<h1 class="teste">hey</h1>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(".teste").click(function() {
alert('hey')
});
$("#table").click(function() {
alert('table')
});
// detecta quando chega ao fim da div
$("tbody").scroll(function() {
if ($(this).scrollTop() + $(this).height() == $(this).get(0).scrollHeight) {
alert("final")
var row = 2;
$.ajax({
//envia dados para o fetch_data.php
url: 'teste3.php',
type: 'post',
data: {row:row},
success: function(response){
$("tbody").html(response);
}
});
}
});
</script>
</body>
</html>
I do the Ajax request when the scroll reaches the end of the table for the teste3.php file but when the value is returned the previous ones are gone, I want the table list to only increase.
Follow the code of the teste3.php file:
<?php
$row = $_POST['row']+1;
echo " <tr>
<td class='filterable-cell'>heye</td>
<td class='filterable-cell'>Escort</td>
<td class='filterable-cell'>Blue</td>
<td class='filterable-cell'>2000</td>
</tr>";
I think you need to use an append: https://api.jquery.com/append/
– cezar
I’ll try @Cezarcruz
– Sarah Tavares