Skip to content

Commit

Permalink
add CellAttribute and RowAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Jan 14, 2024
1 parent c44f968 commit 13a2a78
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 60 deletions.
16 changes: 13 additions & 3 deletions samples/Sample.ClientSide/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ public static async Task Main(string[] args)
.AddHttpClient<GitHubClient>()
.AddHttpMessageHandler<ProgressBarHandler>();

services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("default"));
services
.AddHttpClient<RandomDataClient>()
.AddHttpMessageHandler<ProgressBarHandler>();

services.AddProgressBar();
services.AddScoped(sp => sp
.GetRequiredService<IHttpClientFactory>()
.CreateClient("default")
);

services
.AddProgressBar();

await builder.Build().RunAsync();
await builder
.Build()
.RunAsync();
}
}
}
28 changes: 28 additions & 0 deletions samples/Sample.Core/Models/Bank.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Text.Json.Serialization;

namespace Sample.Core.Models
{
public class Bank
{
[JsonPropertyName("id")]
public int Id { get; set; }

[JsonPropertyName("uid")]
public Guid Identifier { get; set; }

[JsonPropertyName("account_number")]
public string AccountNumber { get; set; }

[JsonPropertyName("iban")]
public string IBAN { get; set; }

[JsonPropertyName("bank_name")]
public string BankName { get; set; }

[JsonPropertyName("routing_number")]
public string RoutingNumber { get; set; }

[JsonPropertyName("swift_bic")]
public string SwiftBIC { get; set; }
}
}
8 changes: 5 additions & 3 deletions samples/Sample.Core/Models/StateLocation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Sample.Core.Models
using Sample.Core.Pages.DataGrid.Examples;

namespace Sample.Core.Models
{
public class StateLocation
{
Expand All @@ -8,7 +10,7 @@ public StateLocation(string name, string value)
Value = value;
}

public string Name { get; set; }
public string Value { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
}
56 changes: 56 additions & 0 deletions samples/Sample.Core/Pages/DataGrid/Examples/Banks.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@using Sample.Core.Services
@using Sample.Core.Models

@inject RandomDataClient RandomDataClient;

<h3>Banks</h3>

<DataGrid Data="Data" class="table table-hover" @ref="DataGrid" RowAttributes="RowAttributes">
<DataColumns>
<DataColumn TItem="Bank" Property="p => p.Id" Width="70px" data-test="testing" CellAttributes="CellAttributes" />
<DataColumn TItem="Bank" Property="p => p.AccountNumber" Width="150px" Title="Account" SortIndex="0" />
<DataColumn TItem="Bank" Property="p => p.IBAN" Width="200px" />
<DataColumn TItem="Bank" Property="p => p.BankName" />
<DataColumn TItem="Bank" Property="p => p.RoutingNumber" Width="200px" />
<DataColumn TItem="Bank" Property="p => p.SwiftBIC" Width="200px" />
</DataColumns>
<DataPagination Context="grid">
<DataPager PageSize="10" />
<DataSizer />
<div>@grid.Pager.StartItem - @grid.Pager.EndItem of @grid.Pager.Total</div>
</DataPagination>
</DataGrid>


@code {
public ICollection<Bank> Data { get; set; } = new List<Bank>();

private DataGrid<Bank> DataGrid { get; set; }

protected async override Task OnInitializedAsync()
{
var results = await RandomDataClient.GetBanks();

Data = results;

// triger refresh
StateHasChanged();
await DataGrid.RefreshAsync(true);
}

private Dictionary<string, object> CellAttributes(Bank bank)
{
return new Dictionary<string, object>
{
{ "data-key-" + bank.Id, "Test " + bank.BankName }
};
}

private Dictionary<string, object> RowAttributes(Bank bank)
{
return new Dictionary<string, object>
{
{ "data-row-" + bank.Id, "Test " + bank.BankName }
};
}
}
4 changes: 4 additions & 0 deletions samples/Sample.Core/Pages/DataGrid/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

<Virtual />

<div class="mb-5"></div>

<Banks />

@code {

}
3 changes: 1 addition & 2 deletions samples/Sample.Core/Services/GitHubClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sample.Core.Models.GitHub;

using System;
using System.Net.Http;
using System.Net.Http.Headers;
Expand Down Expand Up @@ -39,6 +40,4 @@ public async Task<SearchResult<Repository>> SearchRepositories(string text, int?
return result;
}
}


}
33 changes: 33 additions & 0 deletions samples/Sample.Core/Services/RandomDataClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Sample.Core.Models;

using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Net.Mime;

namespace Sample.Core.Services
{
public class RandomDataClient
{
public HttpClient HttpClient { get; }

public RandomDataClient(HttpClient httpClient)
{
httpClient.BaseAddress = new Uri("https://random-data-api.com/");

var mimeTypeHeader = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json);
httpClient.DefaultRequestHeaders.Accept.Add(mimeTypeHeader);

HttpClient = httpClient;
}

public async Task<List<Bank>> GetBanks(int size = 100)
{
var url = $"/api/v2/banks";

if (size > 0)
url += $"?size={size}";

return await HttpClient.GetFromJsonAsync<List<Bank>>(url);
}
}
}
67 changes: 35 additions & 32 deletions src/LoreSoft.Blazor.Controls/DataColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;

using Microsoft.AspNetCore.Components;

namespace LoreSoft.Blazor.Controls
Expand All @@ -12,14 +13,15 @@ public class DataColumn<TItem> : ComponentBase
{
private Func<TItem, object> _propertyAccessor;
private string _propertyName;
private int? _sortIndex;
private bool? _sortDescending;

[CascadingParameter(Name = "Grid")]
protected DataGrid<TItem> Grid { get; set; }

[Parameter(CaptureUnmatchedValues = true)]
public Dictionary<string, object> Attributes { get; set; }
public Dictionary<string, object> UnmatchedAttributes { get; set; }

[Parameter]
public Func<TItem, Dictionary<string, object>> CellAttributes { get; set; }

[Parameter]
public Expression<Func<TItem, object>> Property { get; set; }
Expand Down Expand Up @@ -50,18 +52,10 @@ public class DataColumn<TItem> : ComponentBase
public bool Sortable { get; set; } = true;

[Parameter]
public int SortIndex
{
get => _sortIndex ?? -1;
set => SetInitialValue(ref _sortIndex, value);
}
public int SortIndex { get; set; } = -1;

[Parameter]
public bool SortDescending
{
get => _sortDescending ?? false;
set => SetInitialValue(ref _sortDescending, value);
}
public bool SortDescending { get; set; }


[Parameter]
Expand All @@ -80,6 +74,11 @@ public bool SortDescending
public RenderFragment FooterTemplate { get; set; }


internal int CurrentSortIndex { get; set; } = -1;

internal bool CurrentSortDescending { get; set; }


protected override void OnInitialized()
{
if (Grid == null)
Expand All @@ -88,27 +87,13 @@ protected override void OnInitialized()
if (Property == null)
throw new InvalidOperationException("DataColumn Property parameter is required");

CurrentSortIndex = SortIndex;
CurrentSortDescending = SortDescending;

// register with parent grid
Grid.AddColumn(this);
}

protected void OnChange()
{
InvokeAsync(StateHasChanged);
Grid?.RefreshAsync();
}

// parameter properties with internal changes can only be set once
protected void SetInitialValue<T>(ref T field, T value)
{
if (field != null)
return;

field = value;
OnChange();
}


internal string HeaderTitle()
{
if (!string.IsNullOrEmpty(Title))
Expand Down Expand Up @@ -146,8 +131,8 @@ internal string CellValue(TItem data)

internal void UpdateSort(int index, bool descending)
{
_sortIndex = index;
_sortDescending = descending;
CurrentSortIndex = index;
CurrentSortDescending = descending;
}

private string PropertyName()
Expand Down Expand Up @@ -186,5 +171,23 @@ private string ToTitle(string value)

return spacedName.ToString();
}

internal Dictionary<string, object> ComputeAttributes(TItem data)
{
var attributes = new Dictionary<string, object>();
if (CellAttributes != null)
{
var computed = CellAttributes(data);
if (computed != null)
foreach (var attribute in computed)
attributes[attribute.Key] = attribute.Value;
}

if (UnmatchedAttributes != null)
foreach (var attribute in UnmatchedAttributes)
attributes[attribute.Key] = attribute.Value;

return attributes;
}
}
}
Loading

0 comments on commit 13a2a78

Please sign in to comment.