Пагинация запросов и исправление отображений

This commit is contained in:
2025-04-18 22:47:25 +04:00
parent 29aee61d4c
commit db432929bf
3 changed files with 249 additions and 38 deletions
+99 -5
View File
@@ -1,6 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Pf2eModel;
using Pf2eModel.Model;
using Pf2eModel.Model.Base;
@@ -29,10 +31,32 @@ public partial class MainViewForm : Form
TypesListBox.SelectedIndex = 0;
}
private void TypesListBox_SelectedIndexChanged(object sender, EventArgs _)
public const int PerPage = 20;
public int Count;
public int Page
{
if (TypesListBox.SelectedItem is null)
get => Convert.ToInt32(PageTextBox.Text);
set
{
PageTextBox.Text = value.ToString();
UpdateTreeView();
}
}
public int MaxPage => Convert.ToInt32(Math.Ceiling(1.0 * Count / PerPage));
private void UpdateTreeView()
{
EntityType type = (EntityType) TypesListBox.SelectedItem!;
Count = Context.Entities.Count(e => e.Type == type);
if (Page < 1 || Page > MaxPage)
{
Page = Math.Min(MaxPage, Math.Max(1, Page));
return;
}
@@ -40,15 +64,71 @@ public partial class MainViewForm : Form
.Where(e => e.Type == (EntityType) TypesListBox.SelectedItem)
.OrderBy(e => e.Id)
.Include(e => e.Sources)
.ThenInclude(s => s.Source)
.Include(e => e.Aliases)
.Include(e => e.Keywords)
.Include(e => e.Externals)
.ThenInclude(x => x.Target)
.Include(e => e.Traits)
.ThenInclude(t => t.Entity)
.Select(e => new EntityGridRow(e))
.Skip((Page - 1) * PerPage)
.Take(PerPage)
.ToList();
EntityGridView.Rows[0].Selected = true;
PaginationLabel.Text = $"Всего: {Count} объектов, {MaxPage} страниц";
FirstPageButton.Enabled = Page > 1;
PrevPageButton.Enabled = Page > 1;
NextPageButton.Enabled = Page < MaxPage;
LastPageButton.Enabled = Page < MaxPage;
}
private void TypesListBox_SelectedIndexChanged(object sender, EventArgs _)
{
if (TypesListBox.SelectedItem is null)
{
return;
}
Page = 1;
}
private void FirstPageButton_Click(object sender, EventArgs e)
{
Page = 1;
}
private void PrevPageButton_Click(object sender, EventArgs e)
{
Page--;
}
private void NextPageButton_Click(object sender, EventArgs e)
{
Page++;
}
private void LastPageButton_Click(object sender, EventArgs e)
{
Page = MaxPage;
}
private void PageTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
if (e.KeyChar == (char) Keys.Return)
{
UpdateTreeView();
}
}
private void EntityGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
int rowsCount = EntityGridView.SelectedRows.Count;
@@ -72,18 +152,31 @@ public partial class MainViewForm : Form
{
JsonSerializerSettings settings = new()
{
Converters = [new EntityConverter()]
Converters = [
new EntityConverter(),
new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() },
]
};
string result = JsonConvert.SerializeObject(entity, settings);
Type type = EntityOfTypeAttribute.Resolve(entity);
string subResult = JsonConvert.SerializeObject(Context.Find(type, entity.Id), settings);
string subResult = JsonConvert.SerializeObject(ResolveEntity(entity), settings);
return result[..^1] + $",\"SubEntity\":{subResult}}}";
}
public IEntity? ResolveEntity(Entity entity)
{
switch (entity.Type)
{
case EntityType.Ancestry:
return Context.Ancestries
.FirstOrDefault(e => e.Id == entity.Id);
}
return null;
}
public static void JsonToTreeview(TreeView treeView, string jsonString, string rootName)
{
@@ -92,11 +185,11 @@ public partial class MainViewForm : Form
: JObject.Parse(jsonString);
treeView.Nodes.Clear();
foreach (TreeNode node in Element2TreeNode(json, rootName).Nodes)
{
treeView.Nodes.Add(node);
}
treeView.ExpandAll();
}
private static TreeNode Element2TreeNode(object element, string text = "")
@@ -156,4 +249,5 @@ public partial class MainViewForm : Form
public Entity GetEntity() => _e;
}
}