简体   繁体   English

在 Bootstrap 模式中实现无限滚动?

[英]Implementing infinite scroll in Bootstrap modal?

I'm trying to implement infinite scroll in a Bootstrap modal.我正在尝试在 Bootstrap 模式中实现无限滚动。 I've tried several infinite scroll libraries but none of them have worked within a modal.我尝试了几个无限滚动库,但没有一个在模态中工作过。

Here's what my code looks like right now.这是我的代码现在的样子。

When the modal is opened, jQuery listens for the event and requests data from the server:当模态打开时,jQuery 侦听事件并从服务器请求数据:

$(document).on('show.bs.modal', '#modal', function(event)
{
    var modal = $(this);
    var modalBody = modal.find('.modal-body');

    $.ajax({
        url: '/messages',
        type: 'post',
        dataType: 'json'
        success: function(data)
        {
            // Append the rendered view to the modal body
            modalBody.append(data.data.view);
        }
    });
});

Here is the getData() method it retrieves the messages from:这是它从中检索消息的getData()方法:

public function getData()
{
    $messages = Message::paginate(10);

    return response()->json([
        'success' => true,
        'data' => [
            'view' => view('layouts.messages', ['messages' => $messages])->render()
        ]
    ], 200);
}

Here is my layouts.messages blade file:这是我的layouts.messages刀片文件:

<div class="messages-container">
    @foreach ($messages as $message)
        <div class="message">{{ $message->text }}</div>
    @endforeach
</div>

{{ $messages->links() }}

In the end, the modal looks like this:最后,模态看起来像这样:

<div class="modal fade" id="modal" tabindex="-1">
   <div class="modal-dialog">
      <div class="modal-content">
         <div class="modal-body">
            <div class="messages-container">
                @foreach ($messages as $message)
                    <div class="message">{{ $message->text }}</div>
                @endforeach
            </div>

            {{ $messages->links() }}
         </div>
      </div>
   </div>
</div>

As you can see, I need to append more data to the .content-container on each load.如您所见,我需要在每次加载时向.content-container附加更多数据。

From here, how can I implement infinite scrolling the modal so that if the user scrolls down to the bottom of the modal, it loads the next page from the server and appends it to the modal body?从这里开始,我如何实现模态的无限滚动,以便如果用户向下滚动到模态的底部,它会从服务器加载下一页并将其附加到模态主体?

A quick google search resulted in: http://jscroll.com/一个快速的谷歌搜索结果: http : //jscroll.com/

It specifically mentions the ability to load data via Ajax.它特别提到了通过 Ajax 加载数据的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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