【发布时间】:2019-09-06 17:18:54
【问题描述】:
我正在努力解决这个问题,但我一辈子都做不到!
我正在调用一个分页的 API,每个页面有 20 个项目。我正在尝试在我的 UI 上实现无限滚动以使用来自此 API 的数据,并且我们一次加载 12 个项目,因此我需要计算出我需要从中获取的页码以及从该 API 中获取的页码。
例如:
我目前在第 10 项,我想再取 20 项,这意味着我需要从第 1 页中取 10 项,然后从第 2 页中取 10 项。但我不知道该怎么做!
这是我正在使用的模型:
public class PagesToLoad
{
public int Page { get; set; }
public int Take { get; set; }
public int Skip { get; set; }
}
这是方法签名
public static List<PagesToLoad> GetPage(int currentlyLoaded, int toLoad, int maxItemsPerPage)
我会向您展示我尝试过的方法,但这毫无意义,因为我不知道该怎么做。
几个例子:
currentlyLoaded = 0,我想加载 12,maxItemsPerPage 是 20。
所以结果应该是:
new List<PagesToLoad>
{
new PagesToLoad
{
Page = 1,
Skip = 0,
Take = 12
},
}
currentlyLoaded = 10,我想加载 20,maxItemsPerPage 是 20。
所以结果应该是:
new List<PagesToLoad>
{
new PagesToLoad
{
Page = 1,
Skip = 10,
Take = 10
},
new PagesToLoad
{
Page = 2,
Take = 10,
Skip = 0
}
}
更新:
我编写了一些单元测试来尝试使用 NUnit 测试其中一些场景。 以下测试目前仅测试它是否返回了我们应该从中获取的正确页面,并且当前不测试当前页面上的正确位置
[TestCaseSource(nameof(TestData))]
public void TestPaginationPagesToLoad(int currentlyLoaded, int toLoad, int maxItemsPerPage, int[] expectedPages)
{
var result = PaginationHelper.GetNextPages(currentlyLoaded, toLoad, maxItemsPerPage);
var pages = result.Select(x => x.Page).ToArray();
Assert.That(pages.Length, Is.EqualTo(expectedPages.Length), "Did not contain the correct amount of pages");
for (int i = 0; i < pages.Length; i++)
{
Assert.That(pages[i], Is.EqualTo(expectedPages[i]));
}
}
public static IEnumerable<TestCaseData> TestData
{
get
{
yield return new TestCaseData(0, 10, 20, new [] { 1 }).SetName("Load_First_Page");
yield return new TestCaseData(20, 10, 20, new [] { 2 }).SetName("Load_Second_Page");
yield return new TestCaseData(0, 20, 20, new [] { 1 }).SetName("Load_Full_First_Page");
yield return new TestCaseData(20, 20, 20, new [] { 2 }).SetName("Load_Full_Second_Page");
yield return new TestCaseData(10, 20, 20, new [] { 1, 2 }).SetName("Load_Half_First_Page_And_Half_Second_Page");
yield return new TestCaseData(19, 20, 20, new [] { 1, 2 }).SetName("Load_End_First_Page_And_Most_Second_Page");
}
}
【问题讨论】:
标签: c# math pagination