Tengo una tabla en una vista parcial que se actualiza cada 5 segundos. El problema es que cuando se actualiza vuelve a la parte superior y me gustaría mantener la misma posición que antes de actualizar.
He investigado y sé que puedo lograr esto con algo como esto:
var scrollPosition = $(window).scrollTop(); // Your ajax polling code, on success you do the code below. $(window).scrollTop(scrollPosition);El problema es que no puedo averiguar dónde incorporar exactamente este código y cómo debería verse como un todo.
Mi código está a continuación:
<div class="row"> <div class="col-md-12"> <div class="row"> <div class="col-md-12" id="refresh"> @Html.Action("EntryEventUser", "Home"), </div> </div> <script> function loadPartialView() { $.ajax({ url: "@Url.Action("EntryEventUser", "Home")", type: 'GET', // <-- make a async request by GET dataType: 'html', // <-- to expect an html response success: function(result) { $('#refresh').html(result); } }); } $(function() { var scrollPosition = $("loadPartialView").scrollTop(); loadPartialView(); // first time // re-call the function each 5 seconds window.setInterval("loadPartialView()", 5000); $("loadPartialView").scrollTop(scrollPosition); }); </script> <script> function myFunction() { var input, filter, table, tr, td, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); table = document.getElementById("myTable"); tr = table.getElementsByTagName("tr"); for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[0]; if (td) { if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } </script> </div> </div> </div>