Skip to content

Commit

Permalink
improve datagrid col output
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Aug 15, 2024
1 parent d80e1c2 commit 4bb4ec1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 11 deletions.
6 changes: 3 additions & 3 deletions samples/Sample.Core/Pages/DataGrid/Examples/Basic.razor
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
<a href="#" class="d-block" title="@item.FullName">@item.Id</a>
</Template>
</DataColumn>
<DataColumn TItem="Person" Property="p => p.FirstName" SortIndex="1" />
<DataColumn TItem="Person" Property="p => p.LastName" SortIndex="0" />
<DataColumn TItem="Person" Property="p => p.FirstName" SortIndex="1" ColumnStyle="min-width: 200px" />
<DataColumn TItem="Person" Property="p => p.LastName" SortIndex="0" ColumnStyle="min-width: 200px"/>
<DataColumn TItem="Person" Property="p => p.Score" Width="120px" Style="@SoreStyle" />
<DataColumn TItem="Person" Property="p => p.Location" Sortable="false" />
<DataColumn TItem="Person" Property="p => p.Location" ColumnStyle="min-width: 200px" Sortable="false" />
<DataColumn TItem="Person" Property="p => p.Birthday" Width="110px" Format="d" />
</DataColumns>
<DataPagination Context="grid">
Expand Down
14 changes: 14 additions & 0 deletions src/LoreSoft.Blazor.Controls/Data/DataColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;

using LoreSoft.Blazor.Controls.Extensions;
using LoreSoft.Blazor.Controls.Utilities;

using Microsoft.AspNetCore.Components;

Expand Down Expand Up @@ -46,6 +47,13 @@ public class DataColumn<TItem> : 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;

Expand Down Expand Up @@ -83,6 +91,7 @@ public class DataColumn<TItem> : ComponentBase

internal bool CurrentSortDescending { get; set; }

internal string CurrentColumnStyle { get; set; }

protected override void OnInitialized()
{
Expand All @@ -103,6 +112,11 @@ protected override void OnParametersSet()
{
base.OnParametersSet();

CurrentColumnStyle = new StyleBuilder()
.AddStyle(ColumnStyle)
.AddStyle("width", Width, (v) => v.HasValue())
.ToString();

UpdateProperty();
}

Expand Down
10 changes: 2 additions & 8 deletions src/LoreSoft.Blazor.Controls/Data/DataGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@
}
@foreach (var column in VisibleColumns)
{
if (string.IsNullOrEmpty(column.Width))
{
<col />
}
else
{
<col style="width: @column.Width" />
}
<col class="@column.ColumnClass"
style="@column.CurrentColumnStyle" />
}
</colgroup>
<thead>
Expand Down
105 changes: 105 additions & 0 deletions src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs
Original file line number Diff line number Diff line change
@@ -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<T> Debounce<T>(this Action<T> 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;

Check warning on line 52 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 52 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 52 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 52 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 52 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 52 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
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<T> Throttle<T>(this Action<T> action, TimeSpan? interval = null)
{
ArgumentNullException.ThrowIfNull(action);

var delay = interval ?? DefaultDelay;

Task? task = null;

Check warning on line 81 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 81 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 81 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 81 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 81 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 81 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
var l = new object();
T? args = default;

Check warning on line 83 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 83 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 83 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 83 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 83 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 83 in src/LoreSoft.Blazor.Controls/Utilities/DelayedAction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

return (T arg) =>
{
args = arg;
if (task != null)
return;

lock (l)
{
if (task != null)
return;

task = Task.Delay(delay)
.ContinueWith(_ =>
{
action(args);
task = null;
});
}
};
}
}

0 comments on commit 4bb4ec1

Please sign in to comment.