Symfony 3 Infinite Scrolling Ajax

时间:2017-09-23 15:03:02

标签: javascript php jquery ajax symfony

我正在尝试在Ajax中进行无限滚动。

我真的不知道遗失了什么。

Html索引:

<div class="container">
        <!-- Menu -->

        {% include '::announce/menu.html.twig' %}

        {% for announce in announces|slice(0, 4) %}
        <!-- Announce Preview -->
            {% include '::announce/' ~ game ~ '/preview.html.twig' %}
        {% endfor %}            
</div>

<div id="content"> loop content </div>

<nav id="pagination">
    <p><a href="2">Page 2</a></p>
</nav>


{% endblock %}


{% block javascripts %}    

 <script src="{{ asset('js/jquery-ias.min.js') }}"></script>                   

{% endblock %}

我的控制器索引:

    public function indexAction(Request $request, $game)
    {
        $em = $this->getDoctrine()->getManager();
        $announces $em->getRepository('PlatformBundle:Announce')->byGame($game);

        return $this->render('announce/index.html.twig', array(
            'announces' => $announces,
            'game' => $game
        ));
    }

有了这个我的索引页面有4个宣布 现在我向您展示我的滚动代码并添加更多宣布

Ajax文件:

$(window).scroll(function () {
            if($(window).scrollTop() + $(window).height()>= $(document).height()){
                getmoredata();
            }

        })

        function getmoredata() {
            $.ajax({
                type: "GET",
                url: "{{ path('announce_page', {'id': '2', 'game': game}) }}",
                dataType: "json",
                cache: false,
                success: function (response) {
                        $("#content").append(response.classifiedList);
                        $('#spinner').hide();
                        console.log(response);

                },
                error: function (response) {
                    console.log(response);
                }
            });
        }

这是页面的控制器:

public function pageAction(Request $request, $game)
{
    $em = $this->getDoctrine()->getManager();
    $announces = $em->getRepository('PlatformBundle:Announce')->byGame($game);

    $list = $this->renderView('announce/result.html.twig', array(
        'announces' => $announces,
        'game' => $game
    ));

    $response = new JsonResponse();
    $response->setData(array('classifiedList' => $list));
    return $response;

}

最后一个代码,它是ajax(result.html.twig)

的html内容
    {% for announce in announces|slice(4, 4) %}

    <!-- Affichage Annonce -->

        {% include '::announce/' ~ game ~ '/preview.html.twig' %}

    {% endfor %}

对于简历,我的索引有4个宣布,当我在底部滚动时,我有一个肯定的ajax请求没有错误。但是在4宣布后没有任何事情出现。

如果我直接用分页宣布第2页,我可以看到另外4个宣布。所以我的路由工作正常,但我觉得有些东西在ajax代码中不起作用。

有什么想法吗? :)

由于

0 个答案:

没有答案