Skip to content

Commit

Permalink
Sample for static command loading added
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasherceg committed Oct 7, 2023
1 parent c8ce132 commit 3ab3461
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotVVM.Framework.Controls;
Expand All @@ -8,18 +9,18 @@ namespace DotVVM.Samples.BasicSamples.ViewModels.ControlSamples.GridView
{
public class GridViewStaticCommandViewModel : DotvvmViewModelBase
{
public GridViewStaticCommandViewModel()
{
CustomersDataSet = new GridViewDataSet<CustomerData>
public GridViewDataSet<CustomerData> 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<CustomerData> CustomersDataSet { get; set; }
public MultiSortGridViewDataSet MultiSortDataSet { get; set; } = new();

private static IQueryable<CustomerData> GetData()
{
Expand All @@ -29,7 +30,14 @@ private static IQueryable<CustomerData> 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();
}

Expand All @@ -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<GridViewDataSet<CustomerData>> LoadStandard(GridViewDataSetOptions options)
{
var dataSet = new GridViewDataSet<CustomerData>();
dataSet.ApplyOptions(options);
dataSet.LoadFromQueryable(GetData());
return dataSet;
}

[AllowStaticCommand]
public async Task<NextTokenGridViewDataSet> LoadToken(GridViewDataSetOptions<NoFilteringOptions, SortingOptions, CustomerDataNextTokenPagingOptions> options)
{
var dataSet = new NextTokenGridViewDataSet();
dataSet.ApplyOptions(options);
dataSet.LoadFromQueryable(GetData());
return dataSet;
}

[AllowStaticCommand]
public async Task<NextTokenHistoryGridViewDataSet> LoadTokenHistory(GridViewDataSetOptions<NoFilteringOptions, SortingOptions, CustomerDataNextTokenHistoryPagingOptions> options)
{
var dataSet = new NextTokenHistoryGridViewDataSet();
dataSet.ApplyOptions(options);
dataSet.LoadFromQueryable(GetData());
return dataSet;
}

[AllowStaticCommand]
public async Task<MultiSortGridViewDataSet> LoadMultiSort(GridViewDataSetOptions<NoFilteringOptions, MultiCriteriaSortingOptions, PagingOptions> options)
{
var dataSet = new MultiSortGridViewDataSet();
dataSet.ApplyOptions(options);
dataSet.LoadFromQueryable(GetData());
return dataSet;
}

public class NextTokenGridViewDataSet : GenericGridViewDataSet<CustomerData, NoFilteringOptions, SortingOptions, CustomerDataNextTokenPagingOptions, RowInsertOptions<CustomerData>, RowEditOptions>
{
public NextTokenGridViewDataSet() : base(new NoFilteringOptions(), new SortingOptions(), new CustomerDataNextTokenPagingOptions(), new RowInsertOptions<CustomerData>(), new RowEditOptions())
{
}
}

public class CustomerDataNextTokenPagingOptions : NextTokenPagingOptions, IApplyToQueryable, IPagingOptionsLoadingPostProcessor
{
public IQueryable<T> ApplyToQueryable<T>(IQueryable<T> queryable)
{
var token = int.Parse(CurrentToken ?? "0");

return queryable.Cast<CustomerData>()
.OrderBy(c => c.CustomerId)
.Where(c => c.CustomerId > token)
.Take(3)
.Cast<T>();
}

public void ProcessLoadedItems<T>(IQueryable<T> filteredQueryable, IList<T> items)
{
var lastToken = items.Cast<CustomerData>()
.OrderByDescending(c => c.CustomerId)
.FirstOrDefault()?.CustomerId;

NextPageToken = (lastToken ?? 0).ToString();
}
}

public class NextTokenHistoryGridViewDataSet : GenericGridViewDataSet<CustomerData, NoFilteringOptions, SortingOptions, CustomerDataNextTokenHistoryPagingOptions, RowInsertOptions<CustomerData>, RowEditOptions>
{
public NextTokenHistoryGridViewDataSet() : base(new NoFilteringOptions(), new SortingOptions(), new CustomerDataNextTokenHistoryPagingOptions(), new RowInsertOptions<CustomerData>(), 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<T> ApplyToQueryable<T>(IQueryable<T> queryable)
{
var token = PageIndex < TokenHistory.Count - 1 ? int.Parse(TokenHistory[PageIndex - 1] ?? "0") : 0;

return queryable.Cast<CustomerData>()
.OrderBy(c => c.CustomerId)
.Where(c => c.CustomerId > token)
.Take(3)
.Cast<T>();
}

public void ProcessLoadedItems<T>(IQueryable<T> filteredQueryable, IList<T> items)
{
if (PageIndex == TokenHistory.Count)
{
var lastToken = items.Cast<CustomerData>()
.OrderByDescending(c => c.CustomerId)
.FirstOrDefault()?.CustomerId;

TokenHistory.Add((lastToken ?? 0).ToString());
}
}
}

public class MultiSortGridViewDataSet : GenericGridViewDataSet<CustomerData, NoFilteringOptions, MultiCriteriaSortingOptions, PagingOptions, RowInsertOptions<CustomerData>, RowEditOptions>
{
public MultiSortGridViewDataSet() : base(new NoFilteringOptions(), new MultiCriteriaSortingOptions(), new PagingOptions(), new RowInsertOptions<CustomerData>(), new RowEditOptions())
{
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,57 @@
</head>
<body>
<div class="container">
<h1>GridView with IGridViewDataSet</h1>
<dot:GridView DataSource="{value: CustomersDataSet}" class="table table-bordered">
<h1>Standard data set</h1>
<dot:GridView DataSource="{value: StandardDataSet}"
<%--LoadData="{staticCommand: _root.LoadStandard}"--%>>
<Columns>
<!-- comment inside columns collection -->
<dot:GridViewTextColumn HeaderText="Id" ValueBinding="{value: CustomerId}" AllowSorting="True" />
<dot:GridViewTextColumn HeaderText="Name" ValueBinding="{value: Name}" AllowSorting="True" />
<dot:GridViewTextColumn HeaderText="Birth Date" ValueBinding="{value: BirthDate}" FormatString="g" AllowSorting="True" />
<dot:GridViewTemplateColumn HeaderText="Action">
<ContentTemplate>
<dot:Button Click="{command: _root.DeleteCustomerData(CustomerId)}" Text="Static command delete" />
</ContentTemplate>
</dot:GridViewTemplateColumn>
<dot:GridViewTextColumn HeaderText="Message Received" ValueBinding="{value: MessageReceived}" AllowSorting="True" />
</Columns>
</dot:GridView>
<dot:DataPager DataSet="{value: StandardDataSet}" LoadData="{staticCommand: _root.LoadStandard}" />
<%--
<h1>NextToken paging options</h1>
<dot:GridView DataSource="{value: NextTokenDataSet}"
<%--LoadData="{staticCommand: _root.LoadToken}"--%>
>
<Columns>
<dot:GridViewTextColumn HeaderText="Id" ValueBinding="{value: CustomerId}" />
<dot:GridViewTextColumn HeaderText="Name" ValueBinding="{value: Name}" />
<dot:GridViewTextColumn HeaderText="Birth Date" ValueBinding="{value: BirthDate}" FormatString="g" />
<dot:GridViewTextColumn HeaderText="Message Received" ValueBinding="{value: MessageReceived}" />
</Columns>
</dot:GridView>
<dot:DataPager DataSet="{value: NextTokenDataSet}" LoadData="{staticCommand: _root.LoadToken}" />

<!-- comment inside control -->
<h1>NextTokenHistory data set</h1>
<dot:GridView DataSource="{value: NextTokenHistoryDataSet}"
<%--LoadData="{staticCommand: _root.LoadTokenHistory}"--%>
>
<Columns>
<dot:GridViewTextColumn HeaderText="Id" ValueBinding="{value: CustomerId}" />
<dot:GridViewTextColumn HeaderText="Name" ValueBinding="{value: Name}" />
<dot:GridViewTextColumn HeaderText="Birth Date" ValueBinding="{value: BirthDate}" FormatString="g" />
<dot:GridViewTextColumn HeaderText="Message Received" ValueBinding="{value: MessageReceived}" />
</Columns>
</dot:GridView>
<dot:DataPager DataSet="{value: NextTokenHistoryDataSet}" LoadData="{staticCommand: _root.LoadTokenHistory}" />

<h1>MultiSort data set</h1>
<dot:GridView DataSource="{value: MultiSortDataSet}"
<%--LoadData="{staticCommand: _root.LoadMultiSort}"--%>
>
<Columns>
<dot:GridViewTextColumn HeaderText="Id" ValueBinding="{value: CustomerId}" AllowSorting="True" />
<dot:GridViewTextColumn HeaderText="Name" ValueBinding="{value: Name}" AllowSorting="True" />
<dot:GridViewTextColumn HeaderText="Birth Date" ValueBinding="{value: BirthDate}" FormatString="g" AllowSorting="True" />
<dot:GridViewTextColumn HeaderText="Message Received" ValueBinding="{value: MessageReceived}" AllowSorting="True" />
</Columns>
</dot:GridView>
<dot:DataPager DataSet="{value: CustomersDataSet}" class="pagination" />
<dot:DataPager DataSet="{value: MultiSortDataSet}" LoadData="{staticCommand: _root.LoadMultiSort}" />
--%>
</div>
</body>
</html>
</html>

0 comments on commit 3ab3461

Please sign in to comment.