我是第一次尝试实施Laravel分页,我很遗憾该做什么。
我使用以下方法从数据库中提取数据
$data = DB::table('news')->orderBy('created_at', 'asc')->paginate(15);
return $data;
我得到以下
{"total":3,"per_page":15,"current_page":1,"last_page":1,"next_page_url":null,"prev_page_url":null,"from":1,"to":3,"data":[{"id":1,"news_id":"3869577","news_title":"First Title","news_content":"First", "created_at":"2016-06-06 09:20:33","updated_at":"2016-06-06 09:20:33"},{"id":2,"news_id":"11081411","news_title":"Second Title","news_content":"Second.","created_at":"2016-06-07 07:20:33","updated_at":"2016-06-07 07:20:33"},{"id":3,"news_id":"14646385","news_title":"Third Title","news_content":"Third","created_at":"2016-06-07 14:58:33","updated_at":"2016-06-07 14:58:33"}]}
然后我创建了实现EndlessScrollListener
AbsListView.OnScrollListener
在我的片段上,我有:
listview.setOnScrollListener(new EndlessScrollListener() {
@Override
public boolean onLoadMore(int page, int totalItemsCount) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to your AdapterView
customLoadMoreDataFromApi(page);
// or customLoadMoreDataFromApi(totalItemsCount);
return true; // ONLY if more data is actually being loaded; false otherwise.
}
});
然后
// Append more data into the adapter
public void customLoadMoreDataFromApi(int offset) {
// This method probably sends out a network request and appends new data items to your adapter.
// Use the offset value and add it as a parameter to your API request to retrieve paginated data.
// Deserialize API response and then construct new objects to append to the adapter
}
问题
感谢您的帮助
答案 0 :(得分:0)
我基本上只是直接回答你的两个问题
如果您有超过15个元素,Laravel会为您提供URL。然后,您可以从您在代码段中显示的next_page_url参数中提取网址。它是null,因为你有< 15个元素并为15个元素设置分页。
您无需为此创建新路线。该网址将调用您设置分页的路线。分页功能将监听给定的参数(...?page = 2),并为您提供匹配的元素。