Create automatic pagination with Laravel and jquery

Asked

Viewed 219 times

0

I need to create a manual pagination in the Standard. I don’t really understand the logic of this topic:

Manual Paging in Laravel 5.1

But I need to make him create the automatic pages. Not manual. Look at the example I did. It even works only it’s not automatic:

 public function index(Request $request)
 {
 $qtd = $request['qtd'];
 $page = $request['page'];
 Paginator::currentPageResolver
(function () use ($page)
{
   return $page;
});
 $clientes = Clientes::paginate($qtd);
 $clientes = $clientes->appends
 (Request::capture()->except('page'));
 return response()->json
 (['clientes'=>$clientes], 200);
}

In this output it even creates the amount of pages of type last_page = 5. Up to 5 pages. But I need to implement this in javascript:

JAVASCRIPT THAT BRINGS THE PAGINATION:

 function retorna_cliente(valor)
 {
var id_cliente = "";
var nome_cliente = "";
var data_nascimento_cliente = "";
var telefone_cliente = "";
var celular_cliente = "";
var cpf_cliente = "";
var cnpj_cliente = "";
var endereco_cliente = "";
var email_cliente = "";
var cliente = "";
var mostra_dados_pessoa = "";
var container_mostra_cliente = $('.mostra_clientes');
var itemHTML = "";
var mensagem_cliente = "Nenhum cliente encontrado";
var quantidade = $("#qtd").val();
var pagina = 5;
var total_page = "";
var page = "";

if (valor == null)
{
  total_page = 1;
}
else if (valor == 'proximo')
{
  page = 1;
  total_page = page + 1;
}
else if (valor == 'anterior')
{
  page = 1;
  total_page = page - 1;
}

$.ajax({
  url: url_base + "clientes?qtd=" + quantidade + "&page=" + total_page,
  type: 'GET',
  dataType: 'json',
  success: function (data)
  {
    var retorno = data.clientes.data;
    if (retorno == 0)
    {
      $('.classe-cliente').css('display','none');
      $('.cliente-error-registro').css('display','block');
      $('.cliente-error-registro .mensagem-erro').html(mensagem_cliente);
    }
    else
    {

      $.each(data, function(key,item) {

        let current_page = item.current_page;
        let last_page = item.last_page;
        let next_page_url = item.next_page_url;
        let prev_page_url = item.prev_page_url;
        let clientes = item.data;

        if (next_page_url == null)
        {
          $('.actionBar').html("<a id='btn-prox-pagina-cliente' title='Próxima Página' class='buttonNext btn btn-success' style='display:none;'>Próxima Página >></a><a id='btn-pagina-anterior-cliente' title='Pàgina Anterior' class='buttonPrevious btn btn-primary' style='display: block; clear:both; width:150px; float: left;'><< Página Anterior</a>");
        }
        else if (prev_page_url == null)
        {
          $('.actionBar').html("<a id='btn-prox-pagina-cliente' title='Próxima Página' class='buttonNext btn btn-success'>Próxima Página >></a><a id='btn-pagina-anterior-cliente' title='Pàgina Anterior' class='buttonPrevious btn btn-primary' style='display: none;'><< Página Anterior</a>");
        }
        else if (next_page_url != null || prev_page_url != null)
        {
          $('.actionBar').html("<a id='btn-prox-pagina-cliente' title='Próxima Página' class='buttonNext btn btn-success'>Próxima Página >></a><a id='btn-pagina-anterior-cliente' title='Pàgina Anterior' class='buttonPrevious btn btn-primary'><< Página Anterior</a>");
        }

        for (var i in clientes) {

          id_cliente = clientes[i].id;
          nome_cliente = clientes[i].nome;
          telefone_cliente = clientes[i].telefone;
          cpf_cliente = clientes[i].cpf;
          cnpj_cliente = clientes[i].cnpj;
          endereco_cliente = clientes[i].endereco;
          email_cliente = clientes[i].email;
          cliente = clientes[i].cliente;

          if (cpf_cliente == null)
          {
            mostra_dados_pessoa = cnpj_cliente;
          }
          else
          {
            mostra_dados_pessoa = cpf_cliente;
          }

          itemHTML += "<tr>";
          itemHTML += "<td><th><input type='checkbox' value='" +  id_cliente + "' name='verifica_check_box[]' id='verifica_check_box' class='flat'/></th></td>";
          itemHTML += "<td>" + nome_cliente + "</td>";
          itemHTML += "<td>" + telefone_cliente + "</td>";
          itemHTML += "<td>" + mostra_dados_pessoa  + "</td>";
          itemHTML += "<td>" + endereco_cliente + "</td>";
          itemHTML += "<td>" + email_cliente  + "</td>";
          itemHTML += "<td>" + cliente  + "</td>";
          itemHTML += "</tr>";
        }

      });
      container_mostra_cliente.html(itemHTML);
    }

  },
  error: function (XMLHttpRequest, textStatus, errorThrown)
  {
    console.log(data);
  }
});
 }
  • Why do you need to create in Javascript if Paging already comes with the data ready. !!!? if it already does the calculation and returns the values you could not use these values ?

  • I’m doing a Restfull API and I need it to be like this

  • I know the Laravel works with paginate there. But at the moment I’m not using views. Aguma suggestion?

  • how so you are not using view?

  • I don’t use view. Actually the Standard only does the query understand? It’s an API. The frontend I use separately. On the Laravel I only enter the information, do consultation etc

  • When you return paginate for example if you can use the data in the Rest of your api. using angular would also be a good option. There are easier paths. make javascript-generated html I think it’s hard to maintain.

  • Putz. But then I’ll have to do it again! It’s long gone player...kkkkk. Even if I solve it by own javascript. How could I do?

Show 2 more comments
No answers

Browser other questions tagged

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