duanpin9531 2015-07-07 12:11
浏览 60
已采纳

Laravel 5:如何分析Ajax响应

I am making a search and based on this i am returning some data via Jquery-Ajax There is no problem to display the data but i need them as paginated.

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 !");
            }         
        });
    });
});

Controller

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();
}

How can i paginate this Ajax Response, any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dongqing6755 2015-07-07 12:50
    关注

    I was in the same situation, and after a bit research, I came out with the following link. May be this can help you out.

    Controller Method

    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 Part

    <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>
    

    Check this link

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么