доведена до ума пагинация на сайте

This commit is contained in:
Данияр Аглиуллов 2023-03-18 00:02:03 +04:00
parent c6a95ff78f
commit 5c58ac9317
3 changed files with 52 additions and 42 deletions

View File

@ -10,7 +10,7 @@ namespace ConfectioneryClientApp
private static readonly HttpClient _client = new(); private static readonly HttpClient _client = new();
public static ClientViewModel? Client { get; set; } = null; public static ClientViewModel? Client { get; set; } = null;
public static int CurrentPage { get; set; } = 1; public static int CurrentPage { get; set; } = 0;
public static void Connect(IConfiguration configuration) public static void Connect(IConfiguration configuration)
{ {

View File

@ -4,7 +4,9 @@ using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels; using ConfectioneryContracts.ViewModels;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.SearchModels;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace ConfectioneryClientApp.Controllers namespace ConfectioneryClientApp.Controllers
{ {
@ -153,12 +155,15 @@ namespace ConfectioneryClientApp.Controllers
{ {
return Redirect("~/Home/Enter"); return Redirect("~/Home/Enter");
} }
ViewBag.CurrentPage = APIClient.CurrentPage; return View();
return View(APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}&page={APIClient.CurrentPage}"));
} }
/// <summary>
/// Switches the page.
/// </summary>
/// <returns>Возвращает кортеж с таблицой в html, текущей страницей писем, выключать ли кнопку пред. страницы, выключать ли кнопку след. страницы</returns>
[HttpGet] [HttpGet]
public void SwitchPage(bool isNext) public Tuple<string?, string?, bool, bool>? SwitchPage(bool isNext)
{ {
if (isNext) if (isNext)
{ {
@ -168,11 +173,28 @@ namespace ConfectioneryClientApp.Controllers
{ {
if (APIClient.CurrentPage == 1) if (APIClient.CurrentPage == 1)
{ {
return; return null;
} }
APIClient.CurrentPage--; APIClient.CurrentPage--;
} }
Mails();
var res = APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client!.Id}&page={APIClient.CurrentPage}");
if (isNext && (res == null || res.Count == 0))
{
APIClient.CurrentPage--;
return Tuple.Create<string?, string?, bool, bool>(null, null, APIClient.CurrentPage != 1, false);
}
StringBuilder htmlTable = new();
foreach (var mail in res)
{
htmlTable.Append("<tr>" +
$"<td>{mail.DateDelivery}</td>" +
$"<td>{mail.Subject}</td>" +
$"<td>{mail.Body}</td>" +
"</tr>");
}
return Tuple.Create<string?, string?, bool, bool>(htmlTable.ToString(), APIClient.CurrentPage.ToString(), APIClient.CurrentPage != 1, true);
} }
} }
} }

View File

@ -1,18 +1,7 @@
@using ConfectioneryContracts.ViewModels @{
@model List<MessageInfoViewModel>
@{
ViewData["Title"] = "Mails"; ViewData["Title"] = "Mails";
} }
<div class="text-center"> <div class="text-center">
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
@ -27,21 +16,7 @@
</th> </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="mails-table-body">
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DateDelivery)
</td>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem => item.Body)
</td>
</tr>
}
</tbody> </tbody>
</table> </table>
<ul class="pagination justify-content-center"> <ul class="pagination justify-content-center">
@ -51,17 +26,16 @@
<span class="sr-only">Предыдущее</span> <span class="sr-only">Предыдущее</span>
</a> </a>
</li> </li>
<li id="current-page" class="page-item"><a class="page-link">
@ViewBag.CurrentPage.ToString()
</a></li>
<li class="page-item"> <li class="page-item">
<a id="next-page" class="page-link" href="#" aria-label="Next"> <a id="current-page" class="page-link"></a>
</li>
<li id="next-page" class="page-item">
<a class="page-link" href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span> <span aria-hidden="true">&raquo;</span>
<span class="sr-only">Следующее</span> <span class="sr-only">Следующее</span>
</a> </a>
</li> </li>
</ul> </ul>
}
</div> </div>
<script> <script>
@ -70,12 +44,26 @@
method: "GET", method: "GET",
url: "/Home/SwitchPage", url: "/Home/SwitchPage",
data: { isNext: isNext }, data: { isNext: isNext },
success: function () { success: function (result) {
location.reload(); if (result != null) {
if (result.item1 != null && result.item2 != null) {
$("#mails-table-body").html(result.item1);
$("#current-page").text(result.item2);
}
if (result.item3)
$("#prev-page").removeClass("page-item disabled");
else
$("#prev-page").addClass("page-item disabled");
if (result.item4)
$("#next-page").removeClass("page-item disabled");
else
$("#next-page").addClass("page-item disabled");
}
} }
}); });
} }
// Чтобы в первый раз загрузить данные и попасть на первую страницу
onClicked(true);
$("#prev-page").on('click', () => onClicked(false)); $("#prev-page").on('click', () => onClicked(false));
$("#next-page").on('click', () => onClicked(true)); $("#next-page").on('click', () => onClicked(true));