AJAX分页与Laravel [英] AJAX pagination with Laravel

查看:287
本文介绍了AJAX分页与Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种说法叫做 posts.blade.php 其中被列入 home.blade.php

I have this view called posts.blade.php which gets included in home.blade.php:

<div id="posts">
    @foreach ($posts as $post)
        <div class="list-item clearfix">
            <div class="content">
                <img src="{{ URL::to($post->thumbnail) }}" alt="" />
                <h1>{{{ $post->title }}}</h1>
            </div>
            <div class="score">{{ $post->rating }}</div>
        </div>
    @endforeach
    <div id="pagination">$posts->links()</div>
</div>

在某些职位用户搜索,控制器的 postSearch()函数返回一个JSON响应:

When a user searches for certain posts, the controller's postSearch() function returns a JSON response:

function postSearch() 
{
    $posts = $posts->select(...)->where(...)->orderBy(...)->paginate(5); //Search posts

    return Response::json(View::make('includes.posts', ['posts' => $posts])->render());
}

和jQuery的追加对 #posts DIV的HTML:

And jQuery appends the HTML on the #posts div:

$('#search').submit(function(e) {
    e.preventDefault();
    var form = $(this);

    $.post(form.attr('action'), form.serialize(), function(data) {
        $('#posts').html(data);
    });
});

这完美的作品。但现在,当我点击一个分页链接,页面重新加载,我的搜索​​结果是走了(明显)。我如何分页这些职位?我读过这个,的这个和的这个文章,但我不知道如何实现它。

This works perfect. But now when I click on a pagination link, the page reloads and my search results are gone (obvious). How do I paginate these posts? I've read this, this and this article, but I don't understand how to implement it.

修改

这是我的jQuery的分页至今:

This is my jQuery for the pagination so far:

$('#posts').on('click', '.pagination a', function(e) {
    e.preventDefault();
    var url = $(this).attr('href'),
        page = url.split('page=')[1],
        data = $('#search').serializeArray();

    data.push({page: page}); // Add page variable to post data
    console.log(data);

    $.post($('#search').attr('action'), data, function(data) {
        $('#posts').html(data['posts']);
    });
});

这是数据被发送,当我点击一个分页链接(第2页):

This is data is being send when I click on a pagination link (page 2):

不幸的是没有任何反应,从第1页持续显示的帖子。

Unfortunately nothing happens, the posts from page 1 keep showing.

推荐答案

我不是很熟悉的分页中Laravel但判断从你列出的它看起来并不太难的链接。这code是未经考验的,但...

I'm not very familiar with pagination in Laravel but judging from the links you listed it doesn't look too hard. This code is untested though...

$('#pagination a').on('click', function(e){
    e.preventDefault();
    var url = $(this).attr('href');
    $.post(url, $('#search').serialize(), function(data){
        $('#posts').html(data);
    });
});

更新

在情况下,分页链接是错误的(就像者为OP),你必须自己建立的URL。
只要看看你想要的网址,并添加?页=#号吧。

Update

In case the pagination links are wrong (like the were for the OP) you have to build the url by yourself.
Just take the url you want and add ?page=#number to it.

// page being the page number stripped from the original link
var url = $('#search').attr('action')+'?page='+page;

这篇关于AJAX分页与Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆