pagination with ajax in the Laravel

Asked

Viewed 949 times

1

How could I make an adaptation of my code to make the paging work without updating the page with ajax?

My functional paging code is like this:

defined route

Route::get('/home',   'HomeController@main');

Homecontroller.php

public function main()
{
   //Pega os ultimos usuarios atualiza para criar a paginação
    $users = DB::table('cache_users')->orderBy('timestampOld')->paginate(30);
    return \View::make('Home')->with('users', $users)
}

home.blade.php

@foreach($users as $U)
   <b>$U->name</b>
@endforeach
   {{ $users->links() }}

how I would adapt this code for paging with ajax?

1 answer

1

A practical example that I found some time ago and saved because I knew it could be useful, sorry not to pass the code already with its variables, I answered here in the run :( but I believe you will not have problems in implementing.

home.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Paginação AJAX</title>
</head>
<body>
    <h1>Usuários</h1>
    <div class="users">
        @include('users')
    </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script>
    $(window).on('hashchange', function() {
        if (window.location.hash) {
            var page = window.location.hash.replace('#', '');
            if (page == Number.NaN || page <= 0) {
                return false;
            } else {
                getUsers(page);
            }
        }
    });
    $(document).ready(function() {
        $(document).on('click', '.pagination a', function (e) {
            getUsers($(this).attr('href').split('page=')[1]);
            e.preventDefault();
        });
    });
    function getUsers(page) {
        $.ajax({
            url : '?page=' + page,
            dataType: 'json',
        }).done(function (data) {
            $('.users').html(data);
            location.hash = page;
        }).fail(function () {
            alert('Falha ao carregar usuarios.');
        });
    }
    </script>
</body>
</html>

Homecontroller.php

<?php
classHomeController extends Controller
{
    public function main()
    {
        $users = DB::table('cache_users')->orderBy('timestampOld')->paginate(30);
        if (Request::ajax()) {
            return Response::json(View::make('users', array('users' => $users))->render());
        }
        return View::make('home', array('users' => $users));
    }
}

users.blade.php

@foreach ($users as $user)

    <article>
        <h2>{{ $user->nome}}</h2>
        {{ $user->telefone }}
    </article>

@endforeach

{{ $users->links() }}

Useful link

How to Create an Ajax Pagination Using Laravel

  • I couldn’t adapt this code to my :x

  • received the following reply Non-static method Illuminate Http Request::ajax() should not be called statically, assuming $this from incompatible context

  • haha all right, I can do it this afternoon if I can wait or no one will answer :]

  • A question before, you have a model for users??

  • I do not use the models, I am passing the parameter directly through the controller

  • Try now, it’s clearer I imagine

  • Obs: the problem was the lack of before the Sponse and Request

  • So you managed to solve?

Show 3 more comments

Browser other questions tagged

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