【问题标题】:Laravel 5: How to Paginate an Ajax ResponseLaravel 5:如何对 Ajax 响应进行分页
【发布时间】:2015-09-24 22:06:06
【问题描述】:

我正在搜索并基于此我通过 Jquery-Ajax 返回一些数据 显示数据没有问题,但我需要将它们分页。

jQuery

$(document).ready( function() {

    $(".client_search_option").change(function(){

        var selectedClientTypeVal = "";
        var selectedSmsDecisionVal = "";

        var selectedClientType = $('input[type=radio][name=clientType]:checked');
        var selectedSmsDecision = $('input[type=radio][name=sms_decision]:checked');

        if (selectedClientType.length > 0) {
            selectedClientTypeVal = selectedClientType.val();
        }

        if (selectedSmsDecision.length > 0) {
            selectedSmsDecisionVal = selectedSmsDecision.val();
        }

        //alert(selectedClientTypeVal);
        //alert(selectedSmsDecisionVal);

        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            url: 'http://localhost/pages/clientSearchAjax',
            type: 'POST',
            data: {_token: CSRF_TOKEN, selectedClientTypeVal:selectedClientTypeVal,selectedSmsDecisionVal:selectedSmsDecisionVal},
            dataType: 'JSON',
            success: function (data) {
                console.log(data);
            },
            error:function(){
                alert("An error has occured !");
            }         
        });
    });
});

控制器

public function clientSearch(){
    $client_option = Input::get('selectedClientTypeVal');
    $sms_option = Input::get('selectedSmsDecisionVal');

    if($client_option == 'all' && $sms_option == 'all'){
        $ajax_clients = Client::with('clientType')->paginate(5);
    }else{
        $ajax_clients = Client::with('clientType')->where('clienttype_id', $client_option)->where('send_sms', $sms_option)->paginate(5);
    }

    return $ajax_clients->toJson();
}

我如何为这个 Ajax 响应分页,任何帮助将不胜感激。

【问题讨论】:

    标签: php jquery ajax pagination laravel-5


    【解决方案1】:

    我也遇到了同样的情况,经过一番研究,我得出了以下链接。也许这可以帮助你。

    控制器方法

    public function showPosts()
    {
        $posts = Post::paginate(5);
    
        if (Request::ajax()) {
            return Response::json(View::make('posts', array('posts' => $posts))->render());
        }
    
        return View::make('blog', array('posts' => $posts));
    }
    

    jQuery 部分

    <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 {
                    getPosts(page);
                }
            }
        });
    
        $(document).ready(function() {
            $(document).on('click', '.pagination a', function(e) {
                getPosts($(this).attr('href').split('page=')[1]);
                e.preventDefault();
            });
        });
    
        function getPosts(page) {
            $.ajax({
                url: '?page=' + page,
                dataType: 'json',
            }).done(function(data) {
                $('.posts').html(data);
                location.hash = page;
            }).fail(function() {
                alert('Posts could not be loaded.');
            });
        }
    </script>
    

    查看this link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-09
      • 2023-04-01
      • 2016-07-13
      • 2016-12-15
      • 2016-04-09
      • 2021-03-25
      • 2018-09-09
      • 2015-08-05
      相关资源
      最近更新 更多