From 2236111e0177cb095b9c0dfee8bc22f771e19acb Mon Sep 17 00:00:00 2001 From: "Antti K. Koskela" Date: Mon, 14 Oct 2019 15:39:15 +0300 Subject: [PATCH 1/2] Implements SearchTermParamName to be used without ApiUrl specified (issue #300) --- .../Components/MatTable/MatTable.razor | 846 +++++++++--------- src/MatBlazor/MatBlazor.csproj | 1 + 2 files changed, 429 insertions(+), 418 deletions(-) diff --git a/src/MatBlazor/Components/MatTable/MatTable.razor b/src/MatBlazor/Components/MatTable/MatTable.razor index 6cf70e8c..a114b6a2 100644 --- a/src/MatBlazor/Components/MatTable/MatTable.razor +++ b/src/MatBlazor/Components/MatTable/MatTable.razor @@ -1,418 +1,428 @@ -@namespace MatBlazor -@inherits BaseMatTable -@typeparam TableItem -@inject System.Net.Http.HttpClient Http - -@using System -@using System.Collections.Generic -@using System.Threading -@using System.Reflection; -@using Microsoft.AspNetCore.Components - -@if (!String.IsNullOrWhiteSpace(FilterByColumnName)) -{ - -} - - - - @MatTableHeader - - - @if (ItemList != null) - { - @foreach (var item in ItemList) - { - @MatTableRow(item) - } - } - - @if (@ShowFooter) - { - - } -
-
- - @if (@ShowPaging) - { -
-
-
- Items per page: - - - @if (PageSizes != null) - { - foreach (var item in PageSizes) - { - @item.Text - } - } - -
- -
- Page @CurrentPage of @TotalPages -
- NavigateToPage(PageDirection.First)) Disabled=@(CurrentPage <= 1)> - NavigateToPage(PageDirection.Previous)) Disabled=@(CurrentPage <= 1)> - @*@for (int i = StartPage; i <= EndPage; i++) - { - var currentPage = i; - UpdateList(currentPage))> - @currentPage - - }*@ - - NavigateToPage(PageDirection.Last)) Disabled=@(CurrentPage == EndPage)> -
-
-
-
- } - - @code{ - - protected string PageSizeStr - { - get => PageSize.ToString(); - set - { - PageSize = Convert.ToInt32(value); - CurrentPage = 1; - TotalPages = Math.Max(1, (int)Math.Ceiling(Items.Count() / (decimal)PageSize)); - EndPage = TotalPages; - StartPage = 1; - SetPageSize(PageDirection.Next); - UpdateList(1).GetAwaiter(); - } - } - - [Parameter] - public RenderFragment MatTableHeader { get; set; } - - [Parameter] - public RenderFragment MatTableRow { get; set; } - - /// - /// Not Functioning - /// - [Parameter] - public PageSizeStructure[] PageSizes { get; set; } - - /// - /// Specifies the data for the table. - /// - [Parameter] - public IEnumerable Items { get; set; } - - protected IEnumerable ItemList { get; set; } - - protected override async Task OnInitializedAsync() - { - if (DebounceMilliseconds <= 0) - { - DebounceMilliseconds = 800; - } - if (PageSizes == null) - { - PageSizes = new PageSizeStructure[] - { - new PageSizeStructure() {Text = "5", Value = 5}, - new PageSizeStructure() {Text = "10", Value = 10}, - new PageSizeStructure() {Text = "25", Value = 25}, - new PageSizeStructure() {Text = "50", Value = 50}, - new PageSizeStructure() {Text = "100", Value = 100}, - new PageSizeStructure() {Text = "*", Value = -1} - }; - PageSize = 5; - } - CurrentPage = 1; - - if (!string.IsNullOrWhiteSpace(ApiUrl) && (RequestApiOnlyOnce || LoadInitialData)) - { - try - { - if (!string.IsNullOrWhiteSpace(PagingDataPropertyName) && - !string.IsNullOrWhiteSpace(PagingRecordsCountPropertyName)) - { - await SearchPagedData(); - } - else - { - await SearchData(); - } - } - catch (Exception ex) - { - Console.WriteLine("Error OnInitialized: " + ex.Message); - ErrorMessage = ex.Message; - } - } - else if (Items != null && Items.Count() > 0) - { - FilterLocalProvidedData(); - } - } - - protected override async Task OnParametersSetAsync() - { - await LoadData(); - } - - async Task UpdateList(int currentPage) - { - ItemList = Items.Skip((currentPage - 1) * PageSize).Take(PageSize); - CurrentPage = currentPage; - this.StateHasChanged(); - await LoadData(); - } - - async Task OnPageSizeChange(ChangeEventArgs eventArgs) - { - PageSize = Convert.ToInt32(eventArgs.Value?.ToString()); - CurrentPage = 1; - await LoadData(); - } - - async Task LoadData() - { - try - { - if (RequestApiOnlyOnce) - { - FilterData(); - } - else if (!string.IsNullOrWhiteSpace(ApiUrl) && - !string.IsNullOrWhiteSpace(PagingDataPropertyName) && - !string.IsNullOrWhiteSpace(PagingRecordsCountPropertyName)) - { - await SearchPagedData(); - } - else if (!string.IsNullOrWhiteSpace(ApiUrl) && - (string.IsNullOrWhiteSpace(PagingDataPropertyName) || - string.IsNullOrWhiteSpace(PagingRecordsCountPropertyName))) - { - await SearchData(); - } - else - { - FilterData(); - } - } - catch (Exception ex) - { - Console.WriteLine("Error LoadData: " + ex.Message); - ErrorMessage = ex.Message; - } - await base.InvokeAsync(StateHasChanged); - } - - async Task SearchData() - { - try - { - Items = await Http.GetJsonAsync(ApiUrl + SearchTermParam(SearchTerm)); - ItemList = Items.Skip(RecordsFrom).Take(PageSize); - RecordsCount = Items.Count(); - TotalPages = Math.Max(1, (int)Math.Ceiling(Items.Count() / (decimal)PageSize)); - EndPage = TotalPages; - RecordsFrom = (CurrentPage - 1) * PageSize; - RecordsTo = RecordsFrom + PageSize; - SetPageSize(PageDirection.Next); - } - catch (Exception ex) - { - Console.WriteLine("Error SearchData: " + ex.Message); - ErrorMessage = ex.Message; - } - } - - void FilterLocalProvidedData() - { - RecordsFrom = (CurrentPage - 1) * PageSize; - RecordsTo = RecordsFrom + PageSize; - ItemList = Items.Skip(RecordsFrom).Take(PageSize); - TotalPages = Math.Max(1, (int)Math.Ceiling(Items.Count() / (decimal)PageSize)); - EndPage = TotalPages; - SetPageSize(PageDirection.Next); - } - - async Task SearchPagedData() - { - var PagedData = await Http.GetJsonAsync(ApiUrl + SearchTermParam(SearchTerm)); - Items = (IEnumerable) - PagedData - .GetType() - .GetProperty(PagingDataPropertyName) - .GetValue(PagedData); - var Count = Convert.ToInt32( - PagedData - .GetType() - .GetProperty(PagingRecordsCountPropertyName) - .GetValue(PagedData) - ); - RecordsFrom = (CurrentPage - 1) * PageSize; - RecordsTo = (RecordsFrom + PageSize < Count) ? RecordsFrom + PageSize : Count; - ItemList = Items; - TotalPages = Math.Max(1, (int)Math.Ceiling(Count / (decimal)PageSize)); - EndPage = TotalPages; - SetPageSize(PageDirection.First); - } - - void FilterData() - { - try - { - RecordsFrom = (CurrentPage - 1) * PageSize; - - if (!string.IsNullOrWhiteSpace(SearchTerm)) - { - if (string.IsNullOrWhiteSpace(FilterByColumnName)) - { - throw new ArgumentNullException("FilterByColumnName param cannot be null"); - } - - var filteredCollection = new List(); - foreach (var item in Items) - { - var propertyInfo = item.GetType().GetProperty(FilterByColumnName, BindingFlags.Public | BindingFlags.Instance); - - if (propertyInfo == null) - { - throw new NotSupportedException(FilterByColumnName + " column does not exists in the current context"); - } - - var propertyValue = propertyInfo.GetValue(item, null)?.ToString(); - - if (propertyValue.ToLower().Contains(SearchTerm.ToLower())) - { - filteredCollection.Add(item); - } - } - - RecordsFilteredCount = filteredCollection.Count; - RecordsTo = (RecordsFrom + PageSize < RecordsFilteredCount) ? RecordsFrom + PageSize : RecordsFilteredCount; - TotalPages = Math.Max(1, (int)Math.Ceiling(RecordsFilteredCount / (decimal)PageSize)); - EndPage = TotalPages; - - if (PageSize <= 0) - { - ItemList = filteredCollection; - } - else - { - ItemList = filteredCollection.Skip(RecordsFrom).Take(PageSize); - } - } - else - { - if (PageSize <= 0) - { - ItemList = Items; - } - else - { - ItemList = Items.Skip(RecordsFrom).Take(PageSize); - } - RecordsTo = (RecordsFrom + PageSize < Items.Count()) ? RecordsFrom + PageSize : Items.Count(); - TotalPages = Math.Max(1, (int)Math.Ceiling(Items.Count() / (decimal)PageSize)); - EndPage = TotalPages; - } - SetPageSize(PageDirection.Next); - StateHasChanged(); - } - catch (Exception ex) - { - Console.WriteLine("Error LoadData: " + ex.Message); - ErrorMessage = ex.Message; - } - } - - public void SetPageSize(PageDirection direction) - { - if ((direction == PageDirection.Next || direction == PageDirection.Last) && (EndPage < TotalPages)) - { - StartPage = EndPage + 1; - if (EndPage + PageSize < TotalPages) - { - EndPage = StartPage + PageSize - 1; - } - else - { - EndPage = TotalPages; - } - this.StateHasChanged(); - } - else if ((direction == PageDirection.Previous || direction == PageDirection.First) && (StartPage > 1)) - { - EndPage = StartPage - 1; - StartPage = StartPage - PageSize; - } - } - - async Task NavigateToPage(PageDirection direction) - { - switch (direction) - { - case (PageDirection.First): - CurrentPage = StartPage; - break; - case (PageDirection.Last): - CurrentPage = EndPage; - break; - case (PageDirection.Next): - if ((CurrentPage < TotalPages) && (CurrentPage == EndPage)) - { - SetPageSize(PageDirection.Next); - } - CurrentPage++; - break; - case (PageDirection.Previous): - if ((CurrentPage > 1) && (CurrentPage == StartPage)) - { - SetPageSize(PageDirection.Previous); - } - CurrentPage--; - break; - } - - // C# 8.0 Code - //CurrentPage = (direction) switch - //{ - // PageDirection.First => StartPage, - // PageDirection.Last => EndPage, - // PageDirection.Next => ((Func)(() => - // { - // if ((CurrentPage < TotalPages) && (CurrentPage == EndPage)) - // { - // SetPageSize(PageDirection.Next); - // } - // return CurrentPage + 1; - // }))(), - // PageDirection.Previous => ((Func)(() => - // { - // if ((CurrentPage > 1) && (CurrentPage == StartPage)) - // { - // SetPageSize(PageDirection.Previous); - // } - // return CurrentPage - 1; - // }))(), - // _ => throw new NotSupportedException() - //}; - - await UpdateList(CurrentPage); - } - - void OnInput(ChangeEventArgs eventArgs) - { - Debounce(eventArgs, DebounceMilliseconds, async (e) => - { - SearchTerm = ((ChangeEventArgs)e).Value?.ToString(); - CurrentPage = 1; - await LoadData(); - }); - } - - } +@namespace MatBlazor +@inherits BaseMatTable +@typeparam TableItem +@inject System.Net.Http.HttpClient Http +@inject NavigationManager NavMan + +@using System +@using System.Collections.Generic +@using System.Threading +@using System.Reflection; +@using Microsoft.AspNetCore.Components + +@if (!String.IsNullOrWhiteSpace(FilterByColumnName)) +{ + +} + + + + @MatTableHeader + + + @if (ItemList != null) + { + @foreach (var item in ItemList) + { + @MatTableRow(item) + } + } + + @if (@ShowFooter) + { + + } +
+
+ +@if (@ShowPaging) +{ +
+
+
+ Items per page: + + + @if (PageSizes != null) + { + foreach (var item in PageSizes) + { + @item.Text + } + } + +
+ +
+ Page @CurrentPage of @TotalPages +
+ NavigateToPage(PageDirection.First)) Disabled=@(CurrentPage <= 1)> + NavigateToPage(PageDirection.Previous)) Disabled=@(CurrentPage <= 1)> + @*@for (int i = StartPage; i <= EndPage; i++) + { + var currentPage = i; + UpdateList(currentPage))> + @currentPage + + }*@ + + NavigateToPage(PageDirection.Last)) Disabled=@(CurrentPage == EndPage)> +
+
+
+
+} + +@code{ + + protected string PageSizeStr + { + get => PageSize.ToString(); + set + { + PageSize = Convert.ToInt32(value); + CurrentPage = 1; + TotalPages = Math.Max(1, (int)Math.Ceiling(Items.Count() / (decimal)PageSize)); + EndPage = TotalPages; + StartPage = 1; + SetPageSize(PageDirection.Next); + UpdateList(1).GetAwaiter(); + } + } + + [Parameter] + public RenderFragment MatTableHeader { get; set; } + + [Parameter] + public RenderFragment MatTableRow { get; set; } + + /// + /// Not Functioning + /// + [Parameter] + public PageSizeStructure[] PageSizes { get; set; } + + /// + /// Specifies the data for the table. + /// + [Parameter] + public IEnumerable Items { get; set; } + + protected IEnumerable ItemList { get; set; } + + protected override async Task OnInitializedAsync() + { + if (DebounceMilliseconds <= 0) + { + DebounceMilliseconds = 800; + } + if (PageSizes == null) + { + PageSizes = new PageSizeStructure[] + { + new PageSizeStructure() {Text = "5", Value = 5}, + new PageSizeStructure() {Text = "10", Value = 10}, + new PageSizeStructure() {Text = "25", Value = 25}, + new PageSizeStructure() {Text = "50", Value = 50}, + new PageSizeStructure() {Text = "100", Value = 100}, + new PageSizeStructure() {Text = "*", Value = -1} + }; + PageSize = 5; + } + CurrentPage = 1; + + // If search term name has been specified, fetch the value (if available) and perform search and filtering right away + if (!string.IsNullOrWhiteSpace(SearchTermParamName)) + { + var uri = new Uri(NavMan.Uri); + SearchTerm = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue(SearchTermParamName, out var type) ? type.First() : ""; + + if (!string.IsNullOrWhiteSpace(SearchTerm)) FilterData(); + } + + if (!string.IsNullOrWhiteSpace(ApiUrl) && (RequestApiOnlyOnce || LoadInitialData)) + { + try + { + if (!string.IsNullOrWhiteSpace(PagingDataPropertyName) && + !string.IsNullOrWhiteSpace(PagingRecordsCountPropertyName)) + { + await SearchPagedData(); + } + else + { + await SearchData(); + } + } + catch (Exception ex) + { + Console.WriteLine("Error OnInitialized: " + ex.Message); + ErrorMessage = ex.Message; + } + } + else if (Items != null && Items.Count() > 0) + { + FilterLocalProvidedData(); + } + } + + protected override async Task OnParametersSetAsync() + { + await LoadData(); + } + + async Task UpdateList(int currentPage) + { + ItemList = Items.Skip((currentPage - 1) * PageSize).Take(PageSize); + CurrentPage = currentPage; + this.StateHasChanged(); + await LoadData(); + } + + async Task OnPageSizeChange(ChangeEventArgs eventArgs) + { + PageSize = Convert.ToInt32(eventArgs.Value?.ToString()); + CurrentPage = 1; + await LoadData(); + } + + async Task LoadData() + { + try + { + if (RequestApiOnlyOnce) + { + FilterData(); + } + else if (!string.IsNullOrWhiteSpace(ApiUrl) && + !string.IsNullOrWhiteSpace(PagingDataPropertyName) && + !string.IsNullOrWhiteSpace(PagingRecordsCountPropertyName)) + { + await SearchPagedData(); + } + else if (!string.IsNullOrWhiteSpace(ApiUrl) && + (string.IsNullOrWhiteSpace(PagingDataPropertyName) || + string.IsNullOrWhiteSpace(PagingRecordsCountPropertyName))) + { + await SearchData(); + } + else + { + FilterData(); + } + } + catch (Exception ex) + { + Console.WriteLine("Error LoadData: " + ex.Message); + ErrorMessage = ex.Message; + } + await base.InvokeAsync(StateHasChanged); + } + + async Task SearchData() + { + try + { + Items = await Http.GetJsonAsync(ApiUrl + SearchTermParam(SearchTerm)); + ItemList = Items.Skip(RecordsFrom).Take(PageSize); + RecordsCount = Items.Count(); + TotalPages = Math.Max(1, (int)Math.Ceiling(Items.Count() / (decimal)PageSize)); + EndPage = TotalPages; + RecordsFrom = (CurrentPage - 1) * PageSize; + RecordsTo = RecordsFrom + PageSize; + SetPageSize(PageDirection.Next); + } + catch (Exception ex) + { + Console.WriteLine("Error SearchData: " + ex.Message); + ErrorMessage = ex.Message; + } + } + + void FilterLocalProvidedData() + { + RecordsFrom = (CurrentPage - 1) * PageSize; + RecordsTo = RecordsFrom + PageSize; + ItemList = Items.Skip(RecordsFrom).Take(PageSize); + TotalPages = Math.Max(1, (int)Math.Ceiling(Items.Count() / (decimal)PageSize)); + EndPage = TotalPages; + SetPageSize(PageDirection.Next); + } + + async Task SearchPagedData() + { + var PagedData = await Http.GetJsonAsync(ApiUrl + SearchTermParam(SearchTerm)); + Items = (IEnumerable) + PagedData + .GetType() + .GetProperty(PagingDataPropertyName) + .GetValue(PagedData); + var Count = Convert.ToInt32( + PagedData + .GetType() + .GetProperty(PagingRecordsCountPropertyName) + .GetValue(PagedData) + ); + RecordsFrom = (CurrentPage - 1) * PageSize; + RecordsTo = (RecordsFrom + PageSize < Count) ? RecordsFrom + PageSize : Count; + ItemList = Items; + TotalPages = Math.Max(1, (int)Math.Ceiling(Count / (decimal)PageSize)); + EndPage = TotalPages; + SetPageSize(PageDirection.First); + } + + void FilterData() + { + try + { + RecordsFrom = (CurrentPage - 1) * PageSize; + + if (!string.IsNullOrWhiteSpace(SearchTerm)) + { + if (string.IsNullOrWhiteSpace(FilterByColumnName)) + { + throw new ArgumentNullException("FilterByColumnName param cannot be null"); + } + + var filteredCollection = new List(); + foreach (var item in Items) + { + var propertyInfo = item.GetType().GetProperty(FilterByColumnName, BindingFlags.Public | BindingFlags.Instance); + + if (propertyInfo == null) + { + throw new NotSupportedException(FilterByColumnName + " column does not exists in the current context"); + } + + var propertyValue = propertyInfo.GetValue(item, null)?.ToString(); + + if (propertyValue.ToLower().Contains(SearchTerm.ToLower())) + { + filteredCollection.Add(item); + } + } + + RecordsFilteredCount = filteredCollection.Count; + RecordsTo = (RecordsFrom + PageSize < RecordsFilteredCount) ? RecordsFrom + PageSize : RecordsFilteredCount; + TotalPages = Math.Max(1, (int)Math.Ceiling(RecordsFilteredCount / (decimal)PageSize)); + EndPage = TotalPages; + + if (PageSize <= 0) + { + ItemList = filteredCollection; + } + else + { + ItemList = filteredCollection.Skip(RecordsFrom).Take(PageSize); + } + } + else + { + if (PageSize <= 0) + { + ItemList = Items; + } + else + { + ItemList = Items.Skip(RecordsFrom).Take(PageSize); + } + RecordsTo = (RecordsFrom + PageSize < Items.Count()) ? RecordsFrom + PageSize : Items.Count(); + TotalPages = Math.Max(1, (int)Math.Ceiling(Items.Count() / (decimal)PageSize)); + EndPage = TotalPages; + } + SetPageSize(PageDirection.Next); + StateHasChanged(); + } + catch (Exception ex) + { + Console.WriteLine("Error LoadData: " + ex.Message); + ErrorMessage = ex.Message; + } + } + + public void SetPageSize(PageDirection direction) + { + if ((direction == PageDirection.Next || direction == PageDirection.Last) && (EndPage < TotalPages)) + { + StartPage = EndPage + 1; + if (EndPage + PageSize < TotalPages) + { + EndPage = StartPage + PageSize - 1; + } + else + { + EndPage = TotalPages; + } + this.StateHasChanged(); + } + else if ((direction == PageDirection.Previous || direction == PageDirection.First) && (StartPage > 1)) + { + EndPage = StartPage - 1; + StartPage = StartPage - PageSize; + } + } + + async Task NavigateToPage(PageDirection direction) + { + switch (direction) + { + case (PageDirection.First): + CurrentPage = StartPage; + break; + case (PageDirection.Last): + CurrentPage = EndPage; + break; + case (PageDirection.Next): + if ((CurrentPage < TotalPages) && (CurrentPage == EndPage)) + { + SetPageSize(PageDirection.Next); + } + CurrentPage++; + break; + case (PageDirection.Previous): + if ((CurrentPage > 1) && (CurrentPage == StartPage)) + { + SetPageSize(PageDirection.Previous); + } + CurrentPage--; + break; + } + + // C# 8.0 Code + //CurrentPage = (direction) switch + //{ + // PageDirection.First => StartPage, + // PageDirection.Last => EndPage, + // PageDirection.Next => ((Func)(() => + // { + // if ((CurrentPage < TotalPages) && (CurrentPage == EndPage)) + // { + // SetPageSize(PageDirection.Next); + // } + // return CurrentPage + 1; + // }))(), + // PageDirection.Previous => ((Func)(() => + // { + // if ((CurrentPage > 1) && (CurrentPage == StartPage)) + // { + // SetPageSize(PageDirection.Previous); + // } + // return CurrentPage - 1; + // }))(), + // _ => throw new NotSupportedException() + //}; + + await UpdateList(CurrentPage); + } + + void OnInput(ChangeEventArgs eventArgs) + { + Debounce(eventArgs, DebounceMilliseconds, async (e) => + { + SearchTerm = ((ChangeEventArgs)e).Value?.ToString(); + CurrentPage = 1; + await LoadData(); + }); + } + +} diff --git a/src/MatBlazor/MatBlazor.csproj b/src/MatBlazor/MatBlazor.csproj index bca588a3..35efec97 100644 --- a/src/MatBlazor/MatBlazor.csproj +++ b/src/MatBlazor/MatBlazor.csproj @@ -45,6 +45,7 @@ + From 29aa2d4b681a25cd8a8cbdf88313486483c490e4 Mon Sep 17 00:00:00 2001 From: "Antti K. Koskela" Date: Tue, 5 Jan 2021 09:18:56 +0200 Subject: [PATCH 2/2] Fixes a missing injection --- src/MatBlazor/Components/MatTable/MatTable.razor | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MatBlazor/Components/MatTable/MatTable.razor b/src/MatBlazor/Components/MatTable/MatTable.razor index 14913c2a..f8a69b6e 100644 --- a/src/MatBlazor/Components/MatTable/MatTable.razor +++ b/src/MatBlazor/Components/MatTable/MatTable.razor @@ -2,6 +2,7 @@ @inherits BaseMatTable @typeparam TableItem @inject System.Net.Http.HttpClient Http +@inject NavigationManager NavMan @using System @using System.Collections.Generic