How to put paging in foreach that returns a JSON list with ajax

Asked

Viewed 742 times

0

I call this function which returns me a list and populates a table in the view, updating every time I pass a parameter through the filter. Everything works perfectly, just need to put paging, because has search that returns more than 100 lines. I did a lot of research, but I was not right to use the pluggins. I really wanted to just implement what is already done.

function CarregaGridCliente(representante, vendedor) {

        $("#tbl > tbody").empty();
            $.ajax({
                type: "GET",
                url: "/Representantes/Clientes/Listar",
                mtype: 'GET',
                async: false,
                datatype: 'json',
                data: { representante: representante, vendedor: vendedor },
                success: function (data) {

                    $.each(data, function (i, element) {
                        $("#tbl > tbody").append(
                            "<tr>" +
                            "     <td>" + element.A1_COD + "</td>" +
                            "     <td>" + element.A1_LOJA + "</td>" +
                            "     <td>" + element.A1_NOME + "</td>" +
                            "     <td>" + element.A1_CGC + "</td>" +
                            "     <td>" + element.A1_MUN + "</td>" +
                            "     <td>" + element.A1_TEL + "</td>" +

                            "</tr>"
                        );
                    });
                }
        });

    } 

Veja como a lista retorna certinho, preciso só colocar a paginação pra diminuir as linhas do resultado

  • This can be implemented on the server side. List (Representatives/Clients/List)?

  • If you want to do it on the client side (which wouldn’t be interesting since you returned all the data), you can use the Datatables: https://datatables.net/

  • public Jsonresult List(representative string, seller string) { var listRecourse = Dao.Get(representative,seller). Tolist(); Return Json(listRecourse, Jsonrequestbehavior.Allowget); }

  • i already tried using datatables.net . I couldn’t adapt with the code I programmed

  • Please insert the comment with the code of the List method in the question to make it clearer

2 answers

2

To use dataTables is very simple. I made an example in a few minutes. Follow these steps:

Download the plugin on this link

In step 2 check option jQuery 3;

In step 3 download the files;

Unzip the files in a folder in your project;

At the head of your html link the required files as example below:

<link rel="stylesheet" type="text/css" href="DataTables/datatables.min.css">

<script src="DataTables/datatables.min.js"></script>
<script src="DataTables/jQuery-3.2.1/jquery-3.2.1.min.js"></script>
<script src="DataTables/DataTables-1.10.16/js/jquery.dataTables.min.js">    </script>

<script>
$(document).ready(function() {
    $('#myTable').DataTable();
} );
</script>

In your table identify through id:

<table id="myTable" class="display" style="width:100%">
</table>

The end result is this:

inserir a descrição da imagem aqui

  • For translation use this link
  • I hope I can help.

1


As you already have an object of the type List on the server side, you can use the methods Skip and Take for paging. They respectively "skip" some items and "take" some items from a list.

For example, a list of 200 items called list, to page 50 by 50, taking the third page, would be like this:

var paginada = lista.Skip(99).Take(50);

In your case, the methods will look something like this:

public JsonResult Listar(string representante, string vendedor, int pagina, int tamanhoPagina) 
{ 
    var listaRecurso = Dao.Get(representante,vendedor).ToList(); 
    var listaPaginada = listaRecurso.Skip((tamanhoPagina - 1) * pagina).Take(tamanhoPagina);
    return Json(listaPaginada, JsonRequestBehavior.AllowGet); 
} 

Understand that it will be necessary to pass to the method List plus two parameters, the page number and the total data you want to return per page

  • thanks, I’ll read about it... but how would my front-end look?

  • ai you need to assemble the pagination on the front. It can be manual or using some plugin like this: http://flaviusmatis.github.io/simplePagination.js/

  • Already implemented in the controller, already returning as expected. I will now see how I do something manual, without plugin, simple even, so I can do the "next" and the "previous" on the front. If you have a simple example, I would appreciate it even more. Thank you Ricardo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.