ant-design-blazor/components/table/TableModels/QueryModel.cs
AnaNikolasevic 21a076dff5 feat(module: table): enable to reload with an outside query model (#2129)
* feat/ApplyPredefinedQueryModel: add opportunity to serialize/deserialize QueryModel and apply to Table with ITable.ReloadData(QueryModel queryModel) method; fix current selected value in 'PageSize / page' selection (value was not updated in case using ReloadData(PageIndex, PageSize) method)

* feat/applyPredefinedQueryModel: implement System.Text.Json instead Newtonsoft.Json for serializing/deserializing QueryModel

* feat/applyPredefinedQueryModel: add minor change

* feat/applyPredefinedQueryModel: add method for converting from JsonElement to object type

* feat/applyPredefinedQueryModel: add demo file for save and load table configuration

* feature/applyPredefinedQueryModel: add minor change

* feat/applyPredefinedQueryModel: prevent applying before not saved configuration

* fix sorter restore

* reest data before reload data

* fix test

Co-authored-by: Ana Nikolasevic <23nikolasevic@gmail.com>
Co-authored-by: ElderJames <shunjiey@hotmail.com>
2022-05-29 19:34:46 +08:00

94 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
namespace AntDesign.TableModels
{
public class QueryModel
{
public int PageIndex { get; }
public int PageSize { get; }
public int OffsetRecords => (PageIndex - 1) * PageSize;
public IList<ITableSortModel> SortModel { get; private set; }
public IList<ITableFilterModel> FilterModel { get; private set; }
public QueryModel()
{
this.SortModel = new List<ITableSortModel>();
this.FilterModel = new List<ITableFilterModel>();
}
internal QueryModel(int pageIndex, int pageSize)
{
this.PageSize = pageSize;
this.PageIndex = pageIndex;
this.SortModel = new List<ITableSortModel>();
this.FilterModel = new List<ITableFilterModel>();
}
#if NET5_0_OR_GREATER
[JsonConstructor]
#endif
public QueryModel(int pageIndex, int pageSize, IList<ITableSortModel> sortModel, IList<ITableFilterModel> filterModel)
{
this.PageIndex = pageIndex;
this.PageSize = pageSize;
this.SortModel = sortModel;
this.FilterModel = filterModel;
}
}
public class QueryModel<TItem> : QueryModel, ICloneable
{
internal QueryModel(int pageIndex, int pageSize) : base(pageIndex, pageSize)
{
}
#if NET5_0_OR_GREATER
[JsonConstructor]
#endif
public QueryModel(int pageIndex, int pageSize, IList<ITableSortModel> sortModel, IList<ITableFilterModel> filterModel) : base(pageIndex, pageSize, sortModel, filterModel)
{
}
internal void AddSortModel(ITableSortModel model)
{
SortModel.Add(model);
}
internal void AddFilterModel(ITableFilterModel model)
{
FilterModel.Add(model);
}
public IQueryable<TItem> ExecuteQuery(IQueryable<TItem> query)
{
foreach (var sort in SortModel.OrderBy(x => x.Priority))
{
query = sort.SortList(query);
}
foreach (var filter in FilterModel)
{
query = filter.FilterList(query);
}
return query;
}
public IQueryable<TItem> CurrentPagedRecords(IQueryable<TItem> query) => query.Skip(OffsetRecords).Take(PageSize);
public object Clone()
{
var sorters = this.SortModel.Select(x => x.Clone() as ITableSortModel).ToList();
var filters = this.FilterModel.ToList();
return new QueryModel<TItem>(PageIndex, PageSize, sorters, filters);
}
}
}