简体   繁体   中英

Infinite scroll ajax jquery

I am trying to implement the infinite scroll option. I'm using the symfony2 framework.

The html looks like this:

<div id='more' >Loading...</div>
<div id='no-more' >No more results</div>
<div id='result'></div>

The controller:

if($request->isXmlHttpRequest()){
   $appResult = THIS IN AN ARRAY WITH ALL THE RESULTS ......
   $return=array("responseCode"=>400, "appResult"=>$appResult);
   $return=json_encode($return);
   return new Response($return,200,array('Content-Type'=>'application/json'));
}

And the javascript:

<script type="text/javascript">
        var page = 1;


        $(window).scroll(function () {
            $('#more').hide();
            $('#no-more').hide();


            if($(window).scrollTop() + $(window).height() > $(document).height() - 200) {
                $('#more').css("top","400");
                $('#more').show();

            }
            if($(window).scrollTop() + $(window).height() == $(document).height()) {

                $('#more').hide();
                $('#no-more').hide();

                page++;

                var data = {
                    page_num: page
                };

                var actual_count = 30;

                if((page-1)* 12 > actual_count){
                    $('#no-more').css("top","400");
                    $('#no-more').show();
                }else{
                     $.ajax({
                        type: "POST",
                        url: "{{ path('login') }}",
                        data:data,
                        success: function(res) {


WHAT I DO HERE????


                        }
                    });
                }

            }


        });

</script>

The code works fine, it sends the request and receives the response array with all the results. But i don't know what to do with those results, how can i print them in the result div??

I assume "res" is returning HTML and you want that data to go in the #result div? If so place this within the success function:

$('#result').html($(res));

or perhaps just

$('#result').html(res);

If your returned "res" is JSON, you'll have to parse it somehow and then place it in the div. Can you show us what the result of res is?

edit: being that it's JSON, you may have to do something like the following:

var data = $.parseJSON(res);
$.each(data, function(index, value) {
    var currentHTML = $('#result').html();
    $('#result').html(currentHTML + this.description);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM