Единообразная пагинация на web и desktop

This commit is contained in:
prodigygirl 2023-04-22 08:28:11 +04:00
parent b9eb82d9df
commit efcfea5b94
4 changed files with 127 additions and 27 deletions

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.dataGridViewMail = new System.Windows.Forms.DataGridView();
this.buttonPrev = new System.Windows.Forms.Button();
this.buttonNext = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewMail)).BeginInit();
this.SuspendLayout();
//
@ -41,11 +43,33 @@
this.dataGridViewMail.Size = new System.Drawing.Size(801, 449);
this.dataGridViewMail.TabIndex = 0;
//
// buttonPrev
//
this.buttonPrev.Location = new System.Drawing.Point(266, 455);
this.buttonPrev.Name = "buttonPrev";
this.buttonPrev.Size = new System.Drawing.Size(90, 23);
this.buttonPrev.TabIndex = 1;
this.buttonPrev.Text = "Предыдущая";
this.buttonPrev.UseVisualStyleBackColor = true;
this.buttonPrev.Click += new System.EventHandler(this.buttonPrev_Click);
//
// buttonNext
//
this.buttonNext.Location = new System.Drawing.Point(453, 455);
this.buttonNext.Name = "buttonNext";
this.buttonNext.Size = new System.Drawing.Size(90, 23);
this.buttonNext.TabIndex = 2;
this.buttonNext.Text = "Следующая";
this.buttonNext.UseVisualStyleBackColor = true;
this.buttonNext.Click += new System.EventHandler(this.buttonNext_Click);
//
// FormMail
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.ClientSize = new System.Drawing.Size(800, 483);
this.Controls.Add(this.buttonNext);
this.Controls.Add(this.buttonPrev);
this.Controls.Add(this.dataGridViewMail);
this.Name = "FormMail";
this.Text = "Письма";
@ -58,5 +82,7 @@
#endregion
private DataGridView dataGridViewMail;
private Button buttonPrev;
private Button buttonNext;
}
}

View File

@ -9,6 +9,9 @@ namespace FurnitureAssembly
{
private readonly ILogger _logger;
private readonly IMessageInfoLogic _logic;
private readonly int pageSize = 5;
private int _page = 1;
private int _totalPages = 1;
public FormMail(ILogger<FormMail> logger, IMessageInfoLogic logic)
{
InitializeComponent();
@ -17,12 +20,23 @@ namespace FurnitureAssembly
}
private void FormMail_Load(object sender, EventArgs e)
{
LoadData();
buttonPrev.Enabled = false;
}
private void LoadData()
{
try
{
var list = _logic.ReadList(null);
if (list != null)
{
var totalCount = list!.Capacity;
_totalPages = (int)Math.Ceiling((decimal)totalCount / pageSize);
list = list!.Skip((_page - 1) * pageSize).Take(pageSize).ToList();
dataGridViewMail.DataSource = list;
dataGridViewMail.Columns["ClientId"].Visible = false;
dataGridViewMail.Columns["MessageId"].Visible = false;
@ -37,5 +51,33 @@ namespace FurnitureAssembly
MessageBoxIcon.Error);
}
}
private void buttonPrev_Click(object sender, EventArgs e)
{
if (_page > 1)
{
_page -= 1;
buttonNext.Enabled = true;
LoadData();
if (_page == 1)
{
buttonPrev.Enabled = false;
}
}
}
private void buttonNext_Click(object sender, EventArgs e)
{
if (_page < _totalPages)
{
_page += 1;
buttonPrev.Enabled = true;
LoadData();
if (_page == _totalPages)
{
buttonNext.Enabled = false;
}
}
}
}
}

View File

@ -2,6 +2,7 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
@ -148,13 +149,21 @@ namespace FurnitureAssemblyClientApp.Controllers
}
[HttpGet]
public IActionResult Mails()
public IActionResult Mails(int page = 1)
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={ APIClient.Client.Id}"));
int pageSize = 2;
var data = APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}");
int totalItems = data.Count;
int totalPages = (int)Math.Ceiling((decimal)totalItems / pageSize);
var model = data.Skip((page - 1) * pageSize).Take(pageSize).ToList();
ViewBag.TotalPages = totalPages;
ViewBag.CurrentPage = page;
return View(model);
}
}

View File

@ -10,34 +10,57 @@ ViewData["Title"] = "Mails";
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<table class="table">
<thead>
<tr>
<th>Дата письма</th>
<th>Заголовок</th>
<th>Текст</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
<table class="table">
<thead>
<tr>
<th>Дата письма</th>
<th>Заголовок</th>
<th>Текст</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem =>
<tr>
<td>
@Html.DisplayFor(modelItem =>
item.DateDelivery)
</td>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem =>item.Body)
</td>
</tr>
</td>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem =>item.Body)
</td>
</tr>
}
</tbody>
</table>
</tbody>
</table>
@if (ViewBag.TotalPages > 1)
{
<div class="pagination">
@if (ViewBag.CurrentPage > 1)
{
<a href="@Url.Action("Mails", new { page = ViewBag.CurrentPage - 1 })">Previous</a>
}
else
{
<span>Previous</span>
}
@if (ViewBag.CurrentPage < ViewBag.TotalPages)
{
<a href="@Url.Action("Mails", new { page = ViewBag.CurrentPage + 1 })">Next</a>
}
else
{
<span>Next</span>
}
</div>
}
}
</div>