Hello, for you to do this you need to use jQuery Datatable Server-Side
I’ll make an example for you to have a better idea.
Based on a table, it would look like this:
<script type="text/javascript">
$(document).ready(function () {
$('#myTable').DataTable({
"ajax": {
"url": "/home/loaddataserver",
"type": "POST",
"datatype":"json"
},
"processing": true,
"serverSide": true,
"filter": false,
"orderMulti": false,
"paging": true,
"info":false,
"columns": [
{ "data": "id", "name": "id", "autoWidth": true }, //index 0
{ "data": "nome", "name": "nome", "autoWidth": true }, //index 1
{ "data": "fornecedor.nome", "name":"fornecedor.nome", "autoWidth": true },//index 2
{ "data": "categoria.nome", "name":"categoria.nome", "autoWidth": true },//index 3
{ "data": "precoDeCusto", "name":"precoDeCusto", "autoWidth": true }, //index 4
{ "data": "precoDeVenda", "name": "precoDeVenda", "autoWidth": true }, //index 5
{ "data": "medicao", "name":"medicao", "autoWidth": true }, //index 6
{ "data": "status", "name":"status", "autoWidth": true } //index 7
],
"language": {
"lengthMenu": "Visualizando _MENU_ Registros por página",
"zeroRecords": "Não existe registros para visualização",
"info": "Mostrando página _PAGE_ de _PAGES_",
"infoEmpty": "No records available",
"infoFiltered": "(filtered from _MAX_ total records)",
"paginate": {
"previous": "Página Anterior",
"next": "Próxima página"
}
}
});
</script>
In your controller you need to do the method that will send and receive the data using json, follow an example done in Asp.net core:
[HttpPost]
public IActionResult loaddataserver()
{
var draw = Request.Form["draw"].FirstOrDefault();
var start = Request.Form["start"].FirstOrDefault();
var length = Request.Form["length"].FirstOrDefault();
var sortColumn = Request.Form["columns"] + Request.Form["order[0][column]"].FirstOrDefault() + "][name]".FirstOrDefault();
var sortColumnDir = Request.Form["order[0][dir]"].FirstOrDefault();
int pageSize = length != null ? Convert.ToInt32(length) : 0;
int skip = start != null ? Convert.ToInt32(start) : 0;
int totalRecords = 0;
// var v = ( database.Produtos.Include(p => p.Categoria).Include(p => p.Fornecedor).Where(p => p.Id <= 35));
var v = (database.Produtos.Include(p => p.Categoria).Include(p => p.Fornecedor));
//SORT
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{
// v = v.OrderBy(sortColumn + " " + sortColumnDir);
}
totalRecords = v.Count();
var data = v.Skip(skip).Take(pageSize).ToList();
return Json(new { draw = draw, recordsFiltered = totalRecords, recordsTotal = totalRecords, data = data });
}
Thank you so much for the answer, I only have a doubt in the second part, where should I put it? in the file mentioning in
URL
?– Samuel Verissimo
The second part and the controller code, are you using MVC in php ? see link "url": "/home/loaddataserver", is sending a post to a controller, if the answer helped, do not forget brand as a solution to help other people too
– Harry
Excuse ignorance, I am very lay/new in this direction, I do not own this directory or anything like, apparently I do not use "MVC" so... but if I create a file, mention in the url and put the second part, it will work?
– Samuel Verissimo
Yes, it will work, since you are bringing the correct data, because what I posted and an example, you have to adjust to the data of your table, just follow as a model
– Harry
Yes yes, I’ll try here and get back to you... I’m using your code and a site that explains well too website, from now on, thank you!
– Samuel Verissimo
Well, so far nothing... is displayed at all, I’m probably missing something...
– Samuel Verissimo