Skip to content

Commit 0a920d3

Browse files
authored
feat(TableColumn): add IgnoreWhenExport parameter (#6011)
* feat: 增加 TableExportAttribute 标签类 * feat: 增加 IgnoreWhenExport 参数 * feat: 增加 IgnoreWhenExport 参数 * revert: 删除 TableExportAttribute * refactor: 增加参数标签 * refactor: 增加参数继承逻辑 * test: 增加单元测试
1 parent f89a1fa commit 0a920d3

File tree

10 files changed

+62
-3
lines changed

10 files changed

+62
-3
lines changed

src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,9 @@ public class AutoGenerateColumnAttribute : AutoGenerateBaseAttribute, ITableColu
315315
/// <inheritdoc/>
316316
/// </summary>
317317
public bool IsMarkupString { get; set; }
318+
319+
/// <summary>
320+
/// <inheritdoc/>
321+
/// </summary>
322+
public bool? IgnoreWhenExport { get; set; }
318323
}

src/BootstrapBlazor/Components/Table/ITableColumn.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace BootstrapBlazor.Components;
1010
/// </summary>
1111
public interface ITableColumn : IEditorItem
1212
{
13+
/// <summary>
14+
/// Gets or sets whether the current item is ignored when export operation. Default is false.
15+
/// </summary>
16+
bool? IgnoreWhenExport { get; set; }
17+
1318
/// <summary>
1419
/// Gets or sets whether sorting is allowed. Default is null.
1520
/// </summary>

src/BootstrapBlazor/Components/Table/InternalTableColumn.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,9 @@ class InternalTableColumn(string fieldName, Type fieldType, string? fieldText =
322322
/// <inheritdoc/>
323323
/// </summary>
324324
public Func<ITableColumn, string?, SearchFilterAction>? CustomSearch { get; set; }
325+
326+
/// <summary>
327+
/// <inheritdoc/>
328+
/// </summary>
329+
public bool? IgnoreWhenExport { get; set; }
325330
}

src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public IEnumerable<ITableColumn> GetVisibleColumns()
470470
{
471471
// 不可见列
472472
var items = VisibleColumns.Where(i => i.Visible);
473-
return Columns.Where(i => !i.GetIgnore() && items.Any(v => v.Name == i.GetFieldName()) && ScreenSize >= i.ShownWithBreakPoint);
473+
return Columns.Where(i => !i.GetIgnoreExport() && !i.GetIgnore() && items.Any(v => v.Name == i.GetFieldName()) && ScreenSize >= i.ShownWithBreakPoint);
474474
}
475475

476476
private bool GetColumnsListState(ColumnVisibleItem item) => VisibleColumns.Find(i => i.Name == item.Name) is { Visible: true } && VisibleColumns.Where(i => i.Visible).DistinctBy(i => i.Name).Count(i => i.Visible) == 1;

src/BootstrapBlazor/Components/Table/TableColumn.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public class TableColumn<TItem, TType> : BootstrapComponentBase, ITableColumn
5151
[Parameter]
5252
public Expression<Func<TType>>? FieldExpression { get; set; }
5353

54+
/// <summary>
55+
/// <inheritdoc/>
56+
/// </summary>
57+
[Parameter]
58+
public bool? IgnoreWhenExport { get; set; }
59+
5460
/// <summary>
5561
/// <inheritdoc/>
5662
/// </summary>

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ private static void CopyValue(this ITableColumn col, ITableColumn dest)
111111
if (col.ToolboxTemplate != null) dest.ToolboxTemplate = col.ToolboxTemplate;
112112
if (col.IsRequiredWhenAdd.HasValue) dest.IsRequiredWhenAdd = col.IsRequiredWhenAdd;
113113
if (col.IsRequiredWhenEdit.HasValue) dest.IsRequiredWhenEdit = col.IsRequiredWhenEdit;
114+
if (col.IgnoreWhenExport.HasValue) dest.IgnoreWhenExport = col.IgnoreWhenExport;
114115
}
115116

116117
/// <summary>
@@ -343,6 +344,8 @@ private static RenderFragment RenderContent(this ITableColumn col, string? text)
343344

344345
internal static bool GetVisible(this ITableColumn col) => col.Visible ?? true;
345346

347+
internal static bool GetIgnoreExport(this ITableColumn col) => col.IgnoreWhenExport ?? false;
348+
346349
internal static bool GetShowCopyColumn(this ITableColumn col) => col.ShowCopyColumn ?? false;
347350

348351
internal static bool GetShowTips(this ITableColumn col) => col.ShowTips ?? false;

test/UnitTest/Attributes/AutoGenerateClassTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public void AutoGenerateColumn_Ok()
7171
HeaderTextWrap = true,
7272
IsMarkupString = true,
7373

74-
RequiredErrorMessage = "test"
74+
RequiredErrorMessage = "test",
75+
IgnoreWhenExport = true
7576
};
7677
Assert.Equal(1, attr.Order);
7778
Assert.True(attr.Ignore);
@@ -105,6 +106,7 @@ public void AutoGenerateColumn_Ok()
105106
Assert.True(attr.HeaderTextEllipsis);
106107
Assert.Equal("test header tooltip", attr.HeaderTextTooltip);
107108
Assert.True(attr.IsMarkupString);
109+
Assert.True(attr.IgnoreWhenExport);
108110

109111
var attrInterface = (ITableColumn)attr;
110112
attrInterface.ShowLabelTooltip = true;

test/UnitTest/Components/InternalTableColumnTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void InternalTableColumn_Ok()
8686
SetValue("IsRequiredWhenAdd", true);
8787
SetValue("IsRequiredWhenEdit", true);
8888
SetValue("LookupService", null);
89+
SetValue("IgnoreWhenExport", true);
8990

9091
void SetValue(string propertyName, object? val) => type!.GetProperty(propertyName)!.SetValue(instance, val);
9192
}

test/UnitTest/Components/TableTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8208,6 +8208,37 @@ public void IsMarkupString_Ok()
82088208
Assert.Equal("<div class=\"table-cell\">&lt;div&gt;Address - Test&lt;/div&gt;</div>", cells[1].InnerHtml);
82098209
}
82108210

8211+
[Fact]
8212+
public void IgnoreWhenExport_Ok()
8213+
{
8214+
var items = new Foo[] { new() { Name = "Name", Address = "Address" } };
8215+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
8216+
{
8217+
pb.AddChildContent<Table<Foo>>(pb =>
8218+
{
8219+
pb.Add(a => a.RenderMode, TableRenderMode.Table);
8220+
pb.Add(a => a.Items, items);
8221+
pb.Add(a => a.TableColumns, foo => builder =>
8222+
{
8223+
builder.OpenComponent<TableColumn<Foo, string>>(0);
8224+
builder.AddAttribute(1, "Field", "Name");
8225+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
8226+
builder.CloseComponent();
8227+
8228+
builder.OpenComponent<TableColumn<Foo, string>>(0);
8229+
builder.AddAttribute(1, "Field", "Address");
8230+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Address", typeof(string)));
8231+
builder.AddAttribute(3, "IgnoreWhenExport", true);
8232+
builder.CloseComponent();
8233+
});
8234+
});
8235+
});
8236+
8237+
var table = cut.FindComponent<Table<Foo>>();
8238+
var columns = table.Instance.GetVisibleColumns();
8239+
Assert.Single(columns);
8240+
}
8241+
82118242
[Fact]
82128243
public void OnSelectedRows_Ok()
82138244
{

test/UnitTest/Utils/UtilityTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,13 @@ public void GenerateTableColumns_Ok()
581581
{
582582
var cols = Utility.GetTableColumns<Cat>(new InternalTableColumn[]
583583
{
584-
new(nameof(Cat.Name), typeof(string)) { Text = "test-Name", LookupServiceData = true, IsVisibleWhenAdd = false, IsVisibleWhenEdit = false }
584+
new(nameof(Cat.Name), typeof(string)) { Text = "test-Name", LookupServiceData = true, IsVisibleWhenAdd = false, IsVisibleWhenEdit = false, IgnoreWhenExport = true }
585585
});
586586
Assert.Equal(2, cols.Count());
587587
Assert.Equal(true, cols.First().LookupServiceData);
588588
Assert.False(cols.First().IsVisibleWhenAdd);
589589
Assert.False(cols.First().IsVisibleWhenEdit);
590+
Assert.True(cols.First().IgnoreWhenExport);
590591
}
591592

592593
[Fact]

0 commit comments

Comments
 (0)