Skip to content

Commit

Permalink
Merge pull request #27 from neozhu/blazor
Browse files Browse the repository at this point in the history
update table tool layout
  • Loading branch information
neozhu authored Mar 18, 2023
2 parents 0f1968a + 25d3fdf commit d75b70c
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 113 deletions.
219 changes: 121 additions & 98 deletions src/Templates/Pages/.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PageTitle>@Title</PageTitle>
<style>
.mud-table-toolbar {
height: 84px !important;
height: 120px !important;
}
</style>

Expand All @@ -34,95 +34,115 @@
@bind-SelectedItem="_currentDto"
Hover="true" @ref="_table">
<ToolBarContent>
<div class="justify-start pt-3">
<MudText Typo="Typo.h6">@Title</MudText>
<MudHidden Breakpoint="Breakpoint.SmAndDown">
<MudButton DisableElevation Variant="Variant.Outlined"
Size="Size.Small"
Disabled="@_loading"
OnClick="@(()=>OnRefresh())"
StartIcon="@Icons.Material.Filled.Refresh" IconColor="Color.Surface" Color="Color.Primary"
Style="margin-right: 4px; margin-bottom:4px">@ConstantString.REFRESH</MudButton>
@if (_canCreate)
{
<MudButton DisableElevation Variant="Variant.Outlined" Color="Color.Primary"
StartIcon="@Icons.Material.Filled.Add"
Size="Size.Small"
Disabled="@_loading"
OnClick="OnCreate"
Style="margin-right: 4px; margin-bottom:4px"
IconColor="Color.Surface">@ConstantString.CREATE</MudButton>
}
@if (_canDelete)
{
<MudButton DisableElevation Variant="Variant.Outlined" Color="Color.Error"
StartIcon="@Icons.Material.Filled.Delete"
Disabled="@(!(_selectedItems.Count>0))"
Size="Size.Small"
Style="margin-right: 4px; margin-bottom:4px"
OnClick="OnDeleteChecked"
IconColor="Color.Surface">@ConstantString.DELETE</MudButton>
}
@if (_canExport)
{
<MudLoadingButton @bind-Loading="_exporting" DisableElevation Variant="Variant.Outlined" Color="Color.Primary"
Label="@ConstantString.EXPORT"
Disabled="@_loading"
StartIcon="@Icons.Custom.FileFormats.FileExcel"
Size="Size.Small"
Style="margin-right: 4px; margin-bottom:4px"
OnClick="OnExport"
IconColor="Color.Surface">
@ConstantString.EXPORT
</MudLoadingButton>
}
@if (_canImport)
{
<MudFileUpload T="IBrowserFile" FilesChanged="OnImportData" Accept=".xlsx" Style="display:inline-block; margin-right: 4px; margin-bottom:4px;margin-top:0px">
<ButtonTemplate>
<MudButton HtmlTag="label" Size="Size.Small"
Variant="Variant.Outlined"
Color="Color.Primary"
StartIcon="@Icons.Material.Filled.Upload"
for="@context">
@if (_uploading)
{
<MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" />
<MudText Class="ms-2"> @ConstantString.UPLOADING</MudText>
}
else
{
<MudText>@ConstantString.IMPORT</MudText>
}
</MudButton>
</ButtonTemplate>
</MudFileUpload>
}
</MudHidden>
<MudHidden Breakpoint="Breakpoint.SmAndDown" Invert="true">
<MudMenu AnchorOrigin="Origin.BottomLeft" StartIcon="@Icons.Material.Filled.KeyboardCommandKey" EndIcon="@Icons.Material.Filled.KeyboardArrowDown" Label="@L["Action"]" Color="Color.Primary" Variant="Variant.Filled">
<MudMenuItem Disabled="@_loading" OnClick="@(()=>OnRefresh())">@ConstantString.REFRESH</MudMenuItem>
@if (_canCreate)
{
<MudMenuItem OnClick="OnCreate">@ConstantString.CREATE</MudMenuItem>
}
@if (_canDelete)
{
<MudMenuItem OnClick="OnDeleteChecked">@ConstantString.DELETE</MudMenuItem>
}
@if (_canExport)
<div class="d-flex align-start flex-grow-1">
<div class="d-flex gap-4">
<MudIcon Icon="@Icons.Material.Filled.Window" Size="Size.Large" />
<div class="d-flex flex-column">
<MudText Typo="Typo.caption">@Title</MudText>
<MudEnumSelect Style="min-width:120px" TEnum="{itemname}ListView" ValueChanged="OnChangedListView" Value="_query.ListView" Dense="true" Label="List View">
</MudEnumSelect>
</div>
</div>
<div class="flex-grow-1" />

<div class="d-flex flex-column justify-end">
<div class="d-flex">
<MudHidden Breakpoint="Breakpoint.SmAndDown">
<MudButton DisableElevation Variant="Variant.Outlined"
Size="Size.Small"
Disabled="@_loading"
OnClick="@(()=>OnRefresh())"
StartIcon="@Icons.Material.Filled.Refresh" IconColor="Color.Surface" Color="Color.Primary"
Style="margin-right: 4px; margin-bottom:4px">@ConstantString.REFRESH</MudButton>
@if (_canCreate)
{
<MudButton DisableElevation Variant="Variant.Outlined" Color="Color.Primary"
StartIcon="@Icons.Material.Filled.Add"
Size="Size.Small"
Disabled="@_loading"
OnClick="OnCreate"
Style="margin-right: 4px; margin-bottom:4px"
IconColor="Color.Surface">@ConstantString.CREATE</MudButton>
}
@if (_canDelete)
{
<MudButton DisableElevation Variant="Variant.Outlined" Color="Color.Error"
StartIcon="@Icons.Material.Filled.Delete"
Disabled="@(!(_selectedItems.Count>0))"
Size="Size.Small"
Style="margin-right: 4px; margin-bottom:4px"
OnClick="OnDeleteChecked"
IconColor="Color.Surface">@ConstantString.DELETE</MudButton>
}
@if (_canExport)
{
<MudLoadingButton @bind-Loading="_exporting" DisableElevation Variant="Variant.Outlined" Color="Color.Primary"
Label="@ConstantString.EXPORT"
Disabled="@_loading"
StartIcon="@Icons.Custom.FileFormats.FileExcel"
Size="Size.Small"
Style="margin-right: 4px; margin-bottom:4px"
OnClick="OnExport"
IconColor="Color.Surface">
@ConstantString.EXPORT
</MudLoadingButton>
}
@if (_canImport)
{
<MudFileUpload T="IBrowserFile" FilesChanged="OnImportData" Accept=".xlsx" Style="margin-top:0px" >
<ButtonTemplate>
<MudButton HtmlTag="label"
Size="Size.Small"
Variant="Variant.Outlined"
Color="Color.Primary"
Disabled="@_loading"
StartIcon="@Icons.Material.Filled.Upload"
for="@context">
@if (_uploading)
{
<MudProgressCircular Size="Size.Small" Indeterminate="true" />
@ConstantString.UPLOADING
}
else
{
@ConstantString.IMPORT
}
</MudButton>
</ButtonTemplate>
</MudFileUpload>
}
</MudHidden>
<MudHidden Breakpoint="Breakpoint.SmAndDown" Invert="true">
@if (_canCreate)
{
<MudButton DisableElevation Variant="Variant.Outlined" Color="Color.Primary"
StartIcon="@Icons.Material.Filled.Add"
Size="Size.Small"
Disabled="@_loading"
OnClick="OnCreate"
Style="margin-right: 4px; margin-bottom:4px"
IconColor="Color.Surface">@ConstantString.CREATE</MudButton>
}
@if (_canDelete)
{
<MudButton DisableElevation Variant="Variant.Outlined" Color="Color.Error"
StartIcon="@Icons.Material.Filled.Delete"
Disabled="@(!(_selectedItems.Count>0))"
Size="Size.Small"
Style="margin-right: 4px; margin-bottom:4px"
OnClick="OnDeleteChecked"
IconColor="Color.Surface">@ConstantString.DELETE</MudButton>
}
</MudHidden>
</div>
@if (_canSearch)
{
<MudMenuItem OnClick="OnExport">@ConstantString.EXPORT</MudMenuItem>
<MudTextField T="string" ValueChanged="@(s=>OnSearch(s))" Value="@_query.Keyword" Placeholder="@ConstantString.SEARCH" Adornment="Adornment.End"
AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Small"></MudTextField>
}
</MudMenu>
</MudHidden>
</div>
<MudSpacer />
@if (_canSearch)
{
<MudTextField T="string" ValueChanged="@(s=>OnSearch(s))" Value="@_searchString" Placeholder="@ConstantString.SEARCH" Adornment="Adornment.End"
AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Small"></MudTextField>
}

</div>
</div>
</ToolBarContent>
<ColGroup>
<MudHidden Breakpoint="Breakpoint.SmAndDown">
Expand Down Expand Up @@ -188,11 +208,10 @@
private HashSet<{itemname}Dto> _selectedItems = new HashSet<{itemname}Dto>();
private MudTable<{itemname}Dto> _table = default!;
private {itemname}Dto _currentDto = new();

private string _searchString = string.Empty;
private bool _loading;
private bool _uploading;
private bool _downloading;
private bool _exporting;
[Inject]
private IMediator _mediator { get; set; } = default!;
[Inject]
Expand Down Expand Up @@ -226,7 +245,6 @@
try
{
_loading = true;
_query.Keyword = _searchString;
_query.Sort = string.IsNullOrEmpty(state.SortLabel) ? "Id" : state.SortLabel;
_query.SortBy = (state.SortDirection == SortDirection.Ascending ? AutoFilterer.Enums.Sorting.Ascending : AutoFilterer.Enums.Sorting.Descending);
_query.Page = state.Page + 1;
Expand All @@ -243,15 +261,19 @@
private async Task OnSearch(string text)
{
_selectedItems = new();
_searchString = text;
_query.Keyword = text;
await _table.ReloadServerData();
}
private async Task OnChangedListView({itemname}ListView listview)
{
_query.ListView = listview;
await _table.ReloadServerData();
}

private async Task OnRefresh()
{
{itemname}CacheKey.Refresh();
_selectedItems = new();
_searchString = string.Empty;
_query.Keyword = string.Empty;
await _table.ReloadServerData();
}

Expand All @@ -260,7 +282,7 @@
var command = new AddEdit{itemname}Command();
var parameters = new DialogParameters
{
{ nameof(_{itemname}FormDialog.model),command },
{ nameof(_{itemname}FormDialog.model),command },
};
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Medium, FullWidth = true };
var dialog = DialogService.Show<_{itemname}FormDialog>
Expand Down Expand Up @@ -325,10 +347,11 @@

private async Task OnExport()
{
_downloading = true;
_exporting = true;
var request = new Export{nameofPlural}Query()
{
Keyword = _searchString,
Keyword = _query.Keyword,
ListView = _query.ListView,
Sort = string.IsNullOrEmpty(_table.TableContext.SortFieldLabel) ? "Id" : _table.TableContext.SortFieldLabel,
SortBy = (_table.TableContext.SortDirection == SortDirection.Ascending ? AutoFilterer.Enums.Sorting.Ascending : AutoFilterer.Enums.Sorting.Descending)
};
Expand All @@ -342,7 +365,7 @@
{
Snackbar.Add($"{result.ErrorMessage}", MudBlazor.Severity.Error);
}
_downloading = false;
_exporting = false;
}
private async Task OnImportData(IBrowserFile file)
{
Expand Down
17 changes: 3 additions & 14 deletions src/Templates/Queries/Export/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


using {selectns}.{nameofPlural}.DTOs;
using {selectns}.{nameofPlural}.Queries.Pagination;

namespace {namespace};

Expand All @@ -11,6 +12,8 @@ public class Export{nameofPlural}Query : OrderableFilterBase, IRequest<Result<by
[CompareTo("Name", "Description")] // <-- This filter will be applied to Name or Description.
[StringFilterOptions(StringFilterOption.Contains)]
public string? Keyword { get; set; }
[CompareTo(typeof(Search{nameofPlural}WithListView), "Id")]
public {itemname}ListView ListView { get; set; } = {itemname}ListView.All;
}

public class Export{nameofPlural}QueryHandler :
Expand Down Expand Up @@ -50,17 +53,3 @@ public class Export{nameofPlural}QueryHandler :
return await Result<byte[]>.SuccessAsync(result);;
}
}


public class {nameofPlural}ExportSpecification : Specification<{itemname}>
{
public {nameofPlural}ExportSpecification(Export{nameofPlural}Query request)
{
Criteria = q => q.Name != null;
if (!string.IsNullOrEmpty(request.Keyword))
{
And(x => x.Name.Contains(request.Keyword));
}

}
}
31 changes: 30 additions & 1 deletion src/Templates/Queries/Pagination/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ public class {nameofPlural}WithPaginationQuery : PaginationFilterBase, ICacheabl
[CompareTo("Name", "Description")] // <-- This filter will be applied to Name or Description.
[StringFilterOptions(StringFilterOption.Contains)]
public string? Keyword { get; set; }
[CompareTo(typeof(Search{nameofPlural}WithListView), "Id")]
public {itemname}ListView ListView { get; set; } = {itemname}ListView.All; //<-- When the user selects a different ListView,
// a custom query expression is executed on the filter.
public override string ToString()
{
return $"Search:{Keyword},Sort:{Sort},SortBy:{SortBy},{Page},{PerPage}";
return $"Listview:{ListView},Search:{Keyword},Sort:{Sort},SortBy:{SortBy},{Page},{PerPage}";
}
public string CacheKey => {itemname}CacheKey.GetPaginationCacheKey($"{this}");
public MemoryCacheEntryOptions? Options => {itemname}CacheKey.MemoryCacheEntryOptions;
Expand Down Expand Up @@ -58,4 +61,30 @@ public class {nameofPlural}PaginationSpecification : Specification<{itemname}>
}

}
}
public class Search{nameofPlural}WithListView : FilteringOptionsBaseAttribute
{
public override Expression BuildExpression(Expression expressionBody, PropertyInfo targetProperty, PropertyInfo filterProperty, object value)
{
var today = DateTime.Now.Date;
var start = Convert.ToDateTime(today.ToString("yyyy-MM-dd",CultureInfo.CurrentCulture) + " 00:00:00", CultureInfo.CurrentCulture);
var end = Convert.ToDateTime(today.ToString("yyyy-MM-dd",CultureInfo.CurrentCulture) + " 23:59:59", CultureInfo.CurrentCulture);
var listview = ({itemname}ListView)value;
return listview switch {
{itemname}ListView.All => expressionBody,
{itemname}ListView.CreatedToday => Expression.GreaterThanOrEqual(Expression.Property(expressionBody, "Created"),
Expression.Constant(start, typeof(DateTime?)))
.Combine(Expression.LessThanOrEqual(Expression.Property(expressionBody, "Created"),
Expression.Constant(end, typeof(DateTime?))),
CombineType.And),
_=> expressionBody
};
}
}
public enum {itemname}ListView
{
[Description("All")]
All,
[Description("Created Toady")]
CreatedToday,
}

0 comments on commit d75b70c

Please sign in to comment.