From 3ab34618aaa3527b4fda5b24efe7ac5d6d8f3cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herceg?= Date: Sat, 7 Oct 2023 17:45:42 +0200 Subject: [PATCH] Sample for static command loading added --- .../GridViewStaticCommandViewModel.cs | 142 ++++++++++++++++-- .../GridView/GridViewStaticCommand.dothtml | 56 +++++-- 2 files changed, 171 insertions(+), 27 deletions(-) diff --git a/src/Samples/Common/ViewModels/ControlSamples/GridView/GridViewStaticCommandViewModel.cs b/src/Samples/Common/ViewModels/ControlSamples/GridView/GridViewStaticCommandViewModel.cs index 886eeed48e..d9157dd373 100644 --- a/src/Samples/Common/ViewModels/ControlSamples/GridView/GridViewStaticCommandViewModel.cs +++ b/src/Samples/Common/ViewModels/ControlSamples/GridView/GridViewStaticCommandViewModel.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DotVVM.Framework.Controls; @@ -8,18 +9,18 @@ namespace DotVVM.Samples.BasicSamples.ViewModels.ControlSamples.GridView { public class GridViewStaticCommandViewModel : DotvvmViewModelBase { - public GridViewStaticCommandViewModel() - { - CustomersDataSet = new GridViewDataSet + public GridViewDataSet StandardDataSet { get; set; } = new() { + PagingOptions = new PagingOptions { - PagingOptions = new PagingOptions - { - PageSize = 10 - } - }; - } + PageSize = 10 + } + }; + + public NextTokenGridViewDataSet NextTokenDataSet { get; set; } = new(); + + public NextTokenHistoryGridViewDataSet NextTokenHistoryDataSet { get; set; } = new(); - public GridViewDataSet CustomersDataSet { get; set; } + public MultiSortGridViewDataSet MultiSortDataSet { get; set; } = new(); private static IQueryable GetData() { @@ -29,7 +30,14 @@ private static IQueryable GetData() new CustomerData {CustomerId = 2, Name = "John Deer", BirthDate = DateTime.Parse("1984-03-02")}, new CustomerData {CustomerId = 3, Name = "Johnny Walker", BirthDate = DateTime.Parse("1934-01-03")}, new CustomerData {CustomerId = 4, Name = "Jim Hacker", BirthDate = DateTime.Parse("1912-11-04")}, - new CustomerData {CustomerId = 5, Name = "Joe E. Brown", BirthDate = DateTime.Parse("1947-09-05")} + new CustomerData {CustomerId = 5, Name = "Joe E. Brown", BirthDate = DateTime.Parse("1947-09-05")}, + new CustomerData {CustomerId = 6, Name = "Jack Daniels", BirthDate = DateTime.Parse("1956-07-06")}, + new CustomerData {CustomerId = 7, Name = "James Bond", BirthDate = DateTime.Parse("1965-05-07")}, + new CustomerData {CustomerId = 8, Name = "John Smith", BirthDate = DateTime.Parse("1974-03-08")}, + new CustomerData {CustomerId = 9, Name = "Jack & Jones", BirthDate = DateTime.Parse("1976-03-22")}, + new CustomerData {CustomerId = 10, Name = "Jim Bill", BirthDate = DateTime.Parse("1974-09-20")}, + new CustomerData {CustomerId = 11, Name = "James Joyce", BirthDate = DateTime.Parse("1982-11-28")}, + new CustomerData {CustomerId = 12, Name = "Joudy Jane", BirthDate = DateTime.Parse("1958-12-14")} }.AsQueryable(); } @@ -38,16 +46,118 @@ public override Task PreRender() // fill dataset if (!Context.IsPostBack) { - CustomersDataSet.LoadFromQueryable(GetData()); + StandardDataSet.LoadFromQueryable(GetData()); + NextTokenDataSet.LoadFromQueryable(GetData()); + NextTokenHistoryDataSet.LoadFromQueryable(GetData()); + MultiSortDataSet.LoadFromQueryable(GetData()); } return base.PreRender(); } [AllowStaticCommand] - public void DeleteCustomerData(int customerId) + public async Task> LoadStandard(GridViewDataSetOptions options) + { + var dataSet = new GridViewDataSet(); + dataSet.ApplyOptions(options); + dataSet.LoadFromQueryable(GetData()); + return dataSet; + } + + [AllowStaticCommand] + public async Task LoadToken(GridViewDataSetOptions options) + { + var dataSet = new NextTokenGridViewDataSet(); + dataSet.ApplyOptions(options); + dataSet.LoadFromQueryable(GetData()); + return dataSet; + } + + [AllowStaticCommand] + public async Task LoadTokenHistory(GridViewDataSetOptions options) + { + var dataSet = new NextTokenHistoryGridViewDataSet(); + dataSet.ApplyOptions(options); + dataSet.LoadFromQueryable(GetData()); + return dataSet; + } + + [AllowStaticCommand] + public async Task LoadMultiSort(GridViewDataSetOptions options) + { + var dataSet = new MultiSortGridViewDataSet(); + dataSet.ApplyOptions(options); + dataSet.LoadFromQueryable(GetData()); + return dataSet; + } + + public class NextTokenGridViewDataSet : GenericGridViewDataSet, RowEditOptions> + { + public NextTokenGridViewDataSet() : base(new NoFilteringOptions(), new SortingOptions(), new CustomerDataNextTokenPagingOptions(), new RowInsertOptions(), new RowEditOptions()) + { + } + } + + public class CustomerDataNextTokenPagingOptions : NextTokenPagingOptions, IApplyToQueryable, IPagingOptionsLoadingPostProcessor + { + public IQueryable ApplyToQueryable(IQueryable queryable) + { + var token = int.Parse(CurrentToken ?? "0"); + + return queryable.Cast() + .OrderBy(c => c.CustomerId) + .Where(c => c.CustomerId > token) + .Take(3) + .Cast(); + } + + public void ProcessLoadedItems(IQueryable filteredQueryable, IList items) + { + var lastToken = items.Cast() + .OrderByDescending(c => c.CustomerId) + .FirstOrDefault()?.CustomerId; + + NextPageToken = (lastToken ?? 0).ToString(); + } + } + + public class NextTokenHistoryGridViewDataSet : GenericGridViewDataSet, RowEditOptions> + { + public NextTokenHistoryGridViewDataSet() : base(new NoFilteringOptions(), new SortingOptions(), new CustomerDataNextTokenHistoryPagingOptions(), new RowInsertOptions(), new RowEditOptions()) + { + } + } + + public class CustomerDataNextTokenHistoryPagingOptions : NextTokenHistoryPagingOptions, IApplyToQueryable, IPagingOptionsLoadingPostProcessor { - var customer = CustomersDataSet.Items.First(s => s != null && s.CustomerId == customerId); - CustomersDataSet.Items.Remove(customer); + public IQueryable ApplyToQueryable(IQueryable queryable) + { + var token = PageIndex < TokenHistory.Count - 1 ? int.Parse(TokenHistory[PageIndex - 1] ?? "0") : 0; + + return queryable.Cast() + .OrderBy(c => c.CustomerId) + .Where(c => c.CustomerId > token) + .Take(3) + .Cast(); + } + + public void ProcessLoadedItems(IQueryable filteredQueryable, IList items) + { + if (PageIndex == TokenHistory.Count) + { + var lastToken = items.Cast() + .OrderByDescending(c => c.CustomerId) + .FirstOrDefault()?.CustomerId; + + TokenHistory.Add((lastToken ?? 0).ToString()); + } + } + } + + public class MultiSortGridViewDataSet : GenericGridViewDataSet, RowEditOptions> + { + public MultiSortGridViewDataSet() : base(new NoFilteringOptions(), new MultiCriteriaSortingOptions(), new PagingOptions(), new RowInsertOptions(), new RowEditOptions()) + { + } } } -} \ No newline at end of file +} diff --git a/src/Samples/Common/Views/ControlSamples/GridView/GridViewStaticCommand.dothtml b/src/Samples/Common/Views/ControlSamples/GridView/GridViewStaticCommand.dothtml index c59d0bdd3c..0e51e74c09 100644 --- a/src/Samples/Common/Views/ControlSamples/GridView/GridViewStaticCommand.dothtml +++ b/src/Samples/Common/Views/ControlSamples/GridView/GridViewStaticCommand.dothtml @@ -8,23 +8,57 @@
-

GridView with IGridViewDataSet

- +

Standard data set

+ > - - - - - - + + + + <%-- +

NextToken paging options

+ + > + + + + + + + + - +

NextTokenHistory data set

+ + > + + + + + + + + + +

MultiSort data set

+ + > + + + + + + - + + --%>
- \ No newline at end of file +