Is it possible to set the number of pages without having the records?
No.
I want a more efficient paging, load page data when clicked on page.
That's what he already does. You just need to change the behavior of the Datatable.
The datatable it loads all the data creating the paging, and I have a client that loads 30 thousand records, this will leave slow to load the data.
Go. That's why you should use Datatables Ajax support to do this load more selectively.
For example:
<script>
jQuery(document).ready(function () {
var datatable = $('table').DataTable({
order: [1, "asc"],
processing: true,
paging: true,
serverSide: true,
ajax: {
url: '/Cidades/PesquisarAjax',
type: 'POST'
},
columns: [
{
"name": "Estado",
"title": "Estado",
"render": function (data, type, full, meta) {
return full.Estado.Nome;
}
},
{ "name": "Nome", "data": "Nome", "title": "Nome" },
{ "name": "CodigoIbge", "data": "CodigoIbge", "title": "Código no IBGE" },
{ "name": "CodigoSiafi", "data": "CodigoSiafi", "title": "Código no SIAFI" }
],
});
datatable.columns().every(function () {
var that = this;
$('input', this.footer()).on('keyup change', function () {
that
.search(this.value)
.draw();
});
});
});
</script>
/Cidades/PesquisaAjax
returns a JSON something like this:
{
"draw": 0,
"data": [ /* Aqui são objetos JSON cujos nomes de propriedades são as colunas da tela, e os valores são os valores das respectivas colunas */ ],
"recordsFiltered": 100,
"recordsTotal": 30000
}
See more here .