Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ public class TreeFoo : Foo
ParentId = parentId,
Name = localizer["Foo.Name", $"{id + i:d4}"],
DateTime = System.DateTime.Now.AddDays(i - 1),
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
Count = Random.Next(1, 100),
Complete = Random.Next(1, 100) > 50,
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
Count = Random.Shared.Next(1, 100),
Complete = Random.Shared.Next(1, 100) > 50,
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
}).ToList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ public class TreeFoo : Foo
ParentId = parentId,
Name = localizer["Foo.Name", $"{id + i:d4}"],
DateTime = System.DateTime.Now.AddDays(i - 1),
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
Count = Random.Next(1, 100),
Complete = Random.Next(1, 100) > 50,
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
Count = Random.Shared.Next(1, 100),
Complete = Random.Shared.Next(1, 100) > 50,
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
}).ToList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<TableColumn @bind-Field="@context.DateTime" Width="120" FormatString="yyyy-MM-dd" Align="Alignment.Center" />
<TableColumn @bind-Field="@context.Name" Width="100" />
<TableColumn @bind-Field="@context.Address" />
<TableColumn @bind-Field="@context.Count" Formatter="@IntFormatter" Width="60" Align="Alignment.Right" />
<TableColumn @bind-Field="@context.Count" Width="60" Align="Alignment.Right" />
</TableColumns>
</Table>
</DemoBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ protected override void OnInitialized()
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
private static Task<string> IntFormatter(object? d)
private static Task<string?> IntFormatter(object d)
{
var ret = "";
string? ret = null;
if (d is TableColumnContext<Foo, object?> data && data.Value != null)
{
var val = (int)data.Value;
ret = val.ToString("0.00");
ret = $"Sales: {val:0.00}";
}
return Task.FromResult(ret);
}
Expand All @@ -58,7 +58,7 @@ private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)

// 先处理过滤再处理排序 提高性能
var isFiltered = false;
if (options.Filters.Any())
if (options.Filters.Count != 0)
{
items = items.Where(options.Filters.GetFilterFunc<Foo>());
isFiltered = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<TableColumn @bind-Field="@context.DateTime" Width="120" FormatString="yyyy-MM-dd" Align="Alignment.Center" />
<TableColumn @bind-Field="@context.Name" Width="100" />
<TableColumn @bind-Field="@context.Address" ShownWithBreakPoint="BreakPoint.Medium" />
<TableColumn @bind-Field="@context.Count" ShownWithBreakPoint="BreakPoint.Large" Formatter="@IntFormatter" Width="60" />
<TableColumn @bind-Field="@context.Count" ShownWithBreakPoint="BreakPoint.Large" Width="60" />
</TableColumns>
</Table>
</DemoBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@ public partial class TablesColumnList
[NotNull]
private Table<Foo>? TableColumnVisible { get; set; }

/// <summary>
/// IntFormatter
/// </summary>
/// <param name = "d"></param>
/// <returns></returns>
private static Task<string> IntFormatter(object? d)
{
var ret = "";
if (d is TableColumnContext<Foo, object?> data && data.Value != null)
{
var val = (int)data.Value;
ret = val.ToString("0.00");
}

return Task.FromResult(ret);
}

/// <summary>
/// OnInitialized 方法
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ private class TreeFoo : Foo
ParentId = parentId,
Name = localizer["Foo.Name", $"{id + i:d4}"],
DateTime = System.DateTime.Now.AddDays(i - 1),
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
Count = Random.Next(1, 100),
Complete = Random.Next(1, 100) > 50,
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
Count = Random.Shared.Next(1, 100),
Complete = Random.Shared.Next(1, 100) > 50,
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
}).ToList();
}
}
25 changes: 10 additions & 15 deletions src/BootstrapBlazor.Server/Data/Foo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ public class Foo
public int ReadonlyColumn { get; init; }

#region Static methods
/// <summary>
/// 随机数 Random 实例
/// </summary>
protected static readonly Random Random = new();

/// <summary>
/// 生成Foo类,随机数据
/// Generate Foo class, random data
Expand All @@ -103,10 +98,10 @@ public class Foo
Id = 1,
Name = localizer["Foo.Name", "1000"],
DateTime = System.DateTime.Now,
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
Count = Random.Next(1, 100),
Complete = Random.Next(1, 100) > 50,
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
Count = Random.Shared.Next(1, 100),
Complete = Random.Shared.Next(1, 100) > 50,
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
};

/// <summary>
Expand All @@ -119,11 +114,11 @@ public class Foo
Id = i,
Name = localizer["Foo.Name", $"{i:d4}"],
DateTime = System.DateTime.Now.AddDays(i - 1),
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
Count = Random.Next(1, 100),
Complete = Random.Next(1, 100) > 50,
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle,
ReadonlyColumn = Random.Next(10, 50)
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
Count = Random.Shared.Next(1, 100),
Complete = Random.Shared.Next(1, 100) > 50,
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle,
ReadonlyColumn = Random.Shared.Next(10, 50)
}).ToList();

/// <summary>
Expand Down Expand Up @@ -162,7 +157,7 @@ public static List<SelectedItem> GetCompleteItems(IStringLocalizer<Foo> localize
/// 通过 Id 获取 Title
/// </summary>
/// <returns></returns>
private static string GetTitle() => Random.Next(1, 80) switch
private static string GetTitle() => Random.Shared.Next(1, 80) switch
{
>= 1 and < 10 => "Clerk",
>= 10 and < 50 => "Engineer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public class AutoGenerateColumnAttribute : AutoGenerateBaseAttribute, ITableColu
/// <summary>
/// <inheritdoc/>
/// </summary>
public Func<object?, Task<string?>>? Formatter { get; set; }
public Func<object, Task<string?>>? Formatter { get; set; }

/// <summary>
/// 获得/设置 编辑模板
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Table/ITableColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public interface ITableColumn : IEditorItem
/// <summary>
/// 获得/设置 列格式化回调委托 <see cref="TableColumnContext{TItem, TValue}"/>
/// </summary>
Func<object?, Task<string?>>? Formatter { get; set; }
Func<object, Task<string?>>? Formatter { get; set; }

/// <summary>
/// 获得/设置 文字对齐方式 默认为 null 使用 Alignment.None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class InternalTableColumn(string fieldName, Type fieldType, string? fieldText =
/// </summary>
public string? PlaceHolder { get; set; }

public Func<object?, Task<string?>>? Formatter { get; set; }
public Func<object, Task<string?>>? Formatter { get; set; }

public Alignment? Align { get; set; }

Expand Down
10 changes: 1 addition & 9 deletions src/BootstrapBlazor/Components/Table/TableColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@

namespace BootstrapBlazor.Components;

#if NET5_0
/// <summary>
/// 表头组件
/// </summary>
/// <typeparam name="TType">绑定字段值类型</typeparam>
public class TableColumn<TType> : BootstrapComponentBase, ITableColumn
#elif NET6_0_OR_GREATER
/// <summary>
/// 表头组件
/// </summary>
/// <typeparam name="TItem">模型泛型</typeparam>
/// <typeparam name="TType">绑定字段值类型</typeparam>
public class TableColumn<TItem, TType> : BootstrapComponentBase, ITableColumn
#endif
{
/// <summary>
/// 获得/设置 相关过滤器
Expand Down Expand Up @@ -293,7 +285,7 @@ public class TableColumn<TItem, TType> : BootstrapComponentBase, ITableColumn
/// <inheritdoc/>
/// </summary>
[Parameter]
public Func<object?, Task<string?>>? Formatter { get; set; }
public Func<object, Task<string?>>? Formatter { get; set; }

/// <summary>
/// 获得/设置 显示模板
Expand Down
44 changes: 44 additions & 0 deletions src/BootstrapBlazor/Components/Table/TableFormatContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using Microsoft.AspNetCore.Components.Rendering;

namespace BootstrapBlazor.Components;

internal class TableFormatContent : ComponentBase
{
/// <summary>
/// 获得/设置 格式化方法
/// </summary>
[Parameter]
[NotNull]
[EditorRequired]
public Func<object?, Task<string?>>? Formatter { get; set; }

/// <summary>
/// 获得/设置 当前显示数据项
/// </summary>
[Parameter]
[NotNull]
[EditorRequired]
public object? Item { get; set; }

private string? _content;

protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();

_content = await Formatter(Item);
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
if (!string.IsNullOrEmpty(_content))
{
builder.AddContent(0, _content);
}
}
}
41 changes: 23 additions & 18 deletions src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static List<IFilterAction> ToSearches(this IEnumerable<ITableColumn> colu
return searches;
}

internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem item) => async builder =>
internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem item) => builder =>
{
var val = col.GetItemValue(item);
if (col.Lookup != null && val != null)
Expand All @@ -194,27 +194,32 @@ internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem i
if (col.Formatter != null)
{
// 格式化回调委托
content = await col.Formatter(new TableColumnContext<TItem, object?>(item, val));
}
else if (!string.IsNullOrEmpty(col.FormatString))
{
// 格式化字符串
content = Utility.Format(val, col.FormatString);
}
else if (col.PropertyType.IsDateTime())
{
content = Utility.Format(val, CultureInfo.CurrentUICulture.DateTimeFormat);
}
else if (val is IEnumerable<object> v)
{
content = string.Join(",", v);
builder.OpenComponent<TableFormatContent>(40);
builder.AddAttribute(45, nameof(TableFormatContent.Formatter), col.Formatter);
builder.AddAttribute(46, nameof(TableFormatContent.Item), new TableColumnContext<TItem, object?>(item, val));
builder.CloseComponent();
}
else
{
content = val?.ToString();
if (!string.IsNullOrEmpty(col.FormatString))
{
// 格式化字符串
content = Utility.Format(val, col.FormatString);
}
else if (col.PropertyType.IsDateTime())
{
content = Utility.Format(val, CultureInfo.CurrentUICulture.DateTimeFormat);
}
else if (val is IEnumerable<object> v)
{
content = string.Join(",", v);
}
else
{
content = val?.ToString();
}
builder.AddContent(30, col.RenderTooltip(content, item));
}

builder.AddContent(30, col.RenderTooltip(content, item));
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/BootstrapBlazor/Services/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
}
}

public static object GetFormatterInvoker(Type type, Func<object?, Task<string?>> formatter)
public static object GetFormatterInvoker(Type type, Func<object, Task<string?>> formatter)
{
var cacheKey = $"{nameof(GetFormatterInvoker)}-{type.GetUniqueTypeName()}";
var invoker = Instance.GetOrCreate(cacheKey, entry =>
Expand All @@ -707,12 +707,12 @@ public static object GetFormatterInvoker(Type type, Func<object?, Task<string?>>
});
return invoker(formatter);

static Expression<Func<Func<object?, Task<string?>>, object>> GetFormatterInvokerLambda(Type type)
static Expression<Func<Func<object, Task<string?>>, object>> GetFormatterInvokerLambda(Type type)
{
var method = typeof(CacheManager).GetMethod(nameof(InvokeFormatterAsync), BindingFlags.Static | BindingFlags.NonPublic)!.MakeGenericMethod(type);
var exp_p1 = Expression.Parameter(typeof(Func<object?, Task<string?>>));
var body = Expression.Call(null, method, exp_p1);
return Expression.Lambda<Func<Func<object?, Task<string?>>, object>>(body, exp_p1);
return Expression.Lambda<Func<Func<object, Task<string?>>, object>>(body, exp_p1);
}
}

Expand Down
Loading
Loading