Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Pages/Components/PaginatedPostListButtons/Default.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@using QCVault.Utilities.Entities;
@model PaginatedList<Post>

@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}


<div class="d-flex justify-content-center">
<a asp-page="/PostList"
asp-route-pageNumber="@(Model.PageIndex - 1)"
class="btn btn-primary align-content-center @prevDisabled">
Previous
</a>
<a asp-page="/PostList"
asp-route-pageNumber="@(Model.PageIndex + 1)"
class="btn btn-primary align-content-center @nextDisabled">
Next
</a>
</div>
12 changes: 12 additions & 0 deletions Pages/Components/PaginatedPostListButtons/Default.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace QCVault.Pages.Components.PaginatedButtons
{
public class DefaultModel : PageModel
{
public void OnGet()
{
}
}
}
3 changes: 3 additions & 0 deletions Pages/PostList.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@


<vc:post-list post-List="Model.Posts" />
<vc:paginated-post-list-buttons paginated-Posts="Model.Posts" />



7 changes: 2 additions & 5 deletions Pages/PostList.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
Expand All @@ -17,7 +14,7 @@ public class PostListModel : PageModel
private readonly ILogger<PageModel> logger;
private readonly IPostLoader postLoader;

public IEnumerable<Post> Posts { get; set; }
public PaginatedList<Post> Posts { get; set; }

public PostListModel(ILogger<PageModel> logger, IPostLoader postLoader)
{
Expand All @@ -27,7 +24,7 @@ public PostListModel(ILogger<PageModel> logger, IPostLoader postLoader)

public IActionResult OnGet(int? pageNumber)
{
Posts = postLoader.VisiblePosts();
Posts = PaginatedList<Post>.Create(postLoader.VisiblePosts().AsQueryable(), pageNumber ?? 1, 5);
return Page();
}
}
Expand Down
31 changes: 31 additions & 0 deletions Utilities/Entities/PaginatedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace QCVault.Utilities.Entities
{
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; private set; }
public int TotalPages { get; private set; }

public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);

this.AddRange(items);
}

public bool HasPreviousPage => PageIndex > 1;

public bool HasNextPage => PageIndex < TotalPages;

public static PaginatedList<T> Create(IQueryable<T> source, int pageIndex, int pageSize)
{
var count = source.Count();
var items = source.Skip(( pageIndex - 1 ) * pageSize).Take(pageSize).ToList();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
}
17 changes: 17 additions & 0 deletions ViewComponents/PaginatedPostListButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Mvc;
using QCVault.Utilities.Entities;

namespace QCVault.Components
{
public class PaginatedPostListButtonsViewComponent : ViewComponent
{
public PaginatedPostListButtonsViewComponent()
{
}

public IViewComponentResult Invoke(PaginatedList<Post> paginatedPosts)
{
return View("Default", paginatedPosts);
}
}
}