diff --git a/samples/Sample.Core/Pages/DataGrid/Examples/Basic.razor b/samples/Sample.Core/Pages/DataGrid/Examples/Basic.razor index 91cb3e0..8978153 100644 --- a/samples/Sample.Core/Pages/DataGrid/Examples/Basic.razor +++ b/samples/Sample.Core/Pages/DataGrid/Examples/Basic.razor @@ -38,10 +38,10 @@ @item.Id - - + + - + diff --git a/src/LoreSoft.Blazor.Controls/Data/DataColumn.cs b/src/LoreSoft.Blazor.Controls/Data/DataColumn.cs index dbd27a0..f32f6a2 100644 --- a/src/LoreSoft.Blazor.Controls/Data/DataColumn.cs +++ b/src/LoreSoft.Blazor.Controls/Data/DataColumn.cs @@ -3,6 +3,7 @@ using System.Reflection; using LoreSoft.Blazor.Controls.Extensions; +using LoreSoft.Blazor.Controls.Utilities; using Microsoft.AspNetCore.Components; @@ -46,6 +47,13 @@ public class DataColumn : ComponentBase public string FooterClass { get; set; } + [Parameter] + public string ColumnClass { get; set; } + + [Parameter] + public string ColumnStyle { get; set; } + + [Parameter] public bool Sortable { get; set; } = true; @@ -83,6 +91,7 @@ public class DataColumn : ComponentBase internal bool CurrentSortDescending { get; set; } + internal string CurrentColumnStyle { get; set; } protected override void OnInitialized() { @@ -103,6 +112,11 @@ protected override void OnParametersSet() { base.OnParametersSet(); + CurrentColumnStyle = new StyleBuilder() + .AddStyle(ColumnStyle) + .AddStyle("width", Width, (v) => v.HasValue()) + .ToString(); + UpdateProperty(); } diff --git a/src/LoreSoft.Blazor.Controls/Data/DataGrid.razor b/src/LoreSoft.Blazor.Controls/Data/DataGrid.razor index 6b09a46..c94b9bd 100644 --- a/src/LoreSoft.Blazor.Controls/Data/DataGrid.razor +++ b/src/LoreSoft.Blazor.Controls/Data/DataGrid.razor @@ -30,14 +30,8 @@ } @foreach (var column in VisibleColumns) { - if (string.IsNullOrEmpty(column.Width)) - { - - } - else - { - - } + } diff --git a/src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs b/src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs new file mode 100644 index 0000000..fe3b3bb --- /dev/null +++ b/src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs @@ -0,0 +1,105 @@ +namespace LoreSoft.Blazor.Controls.Utilities; + +// https://www.meziantou.net/debouncing-throttling-javascript-events-in-a-blazor-application.htm +public static class DelayedAction +{ + public static readonly TimeSpan DefaultDelay = TimeSpan.FromMilliseconds(800); + + public static Action Debounce(this Action action, TimeSpan? interval = null) + { + ArgumentNullException.ThrowIfNull(action); + + var delay = interval ?? DefaultDelay; + var last = 0; + + return () => + { + var current = Interlocked.Increment(ref last); + Task.Delay(delay) + .ContinueWith(_ => + { + if (current == last) + action(); + }); + }; + } + + public static Action Debounce(this Action action, TimeSpan? interval = null) + { + ArgumentNullException.ThrowIfNull(action); + + var delay = interval ?? DefaultDelay; + var last = 0; + + return arg => + { + var current = Interlocked.Increment(ref last); + Task.Delay(delay) + .ContinueWith(_ => + { + if (current == last) + action(arg); + }); + }; + } + + public static Action Throttle(this Action action, TimeSpan? interval = null) + { + ArgumentNullException.ThrowIfNull(action); + + var delay = interval ?? DefaultDelay; + + Task? task = null; + var l = new object(); + + return () => + { + if (task != null) + return; + + lock (l) + { + if (task != null) + return; + + task = Task.Delay(delay) + .ContinueWith(_ => + { + action(); + task = null; + }); + } + }; + } + + public static Action Throttle(this Action action, TimeSpan? interval = null) + { + ArgumentNullException.ThrowIfNull(action); + + var delay = interval ?? DefaultDelay; + + Task? task = null; + var l = new object(); + T? args = default; + + return (T arg) => + { + args = arg; + if (task != null) + return; + + lock (l) + { + if (task != null) + return; + + task = Task.Delay(delay) + .ContinueWith(_ => + { + action(args); + task = null; + }); + } + }; + } +}