Laravel 5: How to chunk an Ajax response

I am doing a search and based on that I am returning some data via Jquery-Ajax There is no problem to display the data, but I need it 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 break this Ajax Response, any help would be appreciated.

+3


source to share


1 answer


I was in the same situation and after a little research I came out with the following link. Maybe this can help you.

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

+1


source







All Articles