Skip to content

Commit bcf3715

Browse files
author
Raj Kumar Sri Ramulu
committed
Added IsVisible Parameter to Grid Column
1 parent 4d67533 commit bcf3715

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

blazorbootstrap/Components/Grid/Grid.razor

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
}
2323
@foreach (var column in columns)
2424
{
25+
@if (!column.IsVisible)
26+
{
27+
continue;
28+
}
2529
@column.HeaderTemplate
2630
}
2731
</tr>
@@ -38,6 +42,10 @@
3842
}
3943
@foreach (var column in columns)
4044
{
45+
@if (!column.IsVisible)
46+
{
47+
continue;
48+
}
4149
var columnClassList = new HashSet<string>();
4250
var columnStyleList = new List<string>();
4351

@@ -78,7 +86,7 @@
7886
</thead>
7987
<tbody>
8088
@{
81-
var columnCount = columns.Count;
89+
var columnCount = columns.Where(c => c.IsVisible).Count();
8290
if (AllowSelection) columnCount += 1;
8391
if (AllowDetailView) columnCount += 1;
8492
}
@@ -158,6 +166,10 @@
158166

159167
@foreach (var column in columns)
160168
{
169+
@if (!column.IsVisible)
170+
{
171+
continue;
172+
}
161173
@column.CellTemplate(item)
162174
}
163175
</tr>

blazorbootstrap/Components/Grid/Grid.razor.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public partial class Grid<TItem> : BlazorBootstrapComponentBase
2525

2626
private RenderFragment? headerSelectionTemplate;
2727

28+
private bool isColumnVisibilityChanged = false;
29+
2830
private bool isFirstRenderComplete = false;
2931

3032
private List<TItem>? items = null;
@@ -53,6 +55,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5355
isFirstRenderComplete = true;
5456
}
5557

58+
// As Rendering now complete we can reset the column visibility change to false
59+
isColumnVisibilityChanged = false;
60+
5661
await base.OnAfterRenderAsync(firstRender);
5762
}
5863

@@ -135,6 +140,15 @@ protected override Task OnParametersSetAsync()
135140

136141
internal void AddColumn(GridColumn<TItem> column) => columns.Add(column);
137142

143+
internal void ColumnVisibilityUpdated()
144+
{
145+
if (!isColumnVisibilityChanged)
146+
{
147+
isColumnVisibilityChanged = true;
148+
StateHasChanged();
149+
}
150+
}
151+
138152
internal async Task FilterChangedAsync()
139153
{
140154
if (cancellationTokenSource is not null

blazorbootstrap/Components/Grid/GridColumn.razor.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public partial class GridColumn<TItem> : BlazorBootstrapComponentBase
1414

1515
private string filterValue = default!;
1616

17+
private bool isVisible = true;
18+
1719
private RenderFragment? headerTemplate;
1820

1921
#endregion
@@ -109,6 +111,11 @@ or StringConstants.PropertyTypeNameDecimal
109111
if (filterOperator == FilterOperator.None)
110112
FilterOperator = filterOperator = FilterOperator.Equals;
111113
}
114+
if (isVisible != IsVisible)
115+
{
116+
isVisible = IsVisible;
117+
Parent?.ColumnVisibilityUpdated();
118+
}
112119
}
113120

114121
internal void SetFilterOperator(FilterOperator filterOperator) => FilterOperator = this.filterOperator = filterOperator;
@@ -420,6 +427,15 @@ private async Task OnSortClickAsync()
420427
[Parameter]
421428
public bool IsDefaultSortColumn { get; set; } = false;
422429

430+
/// <summary>
431+
/// Gets or sets visibility of the Grid column.
432+
/// </summary>
433+
/// <remarks>
434+
/// Default value is true.
435+
/// </remarks>
436+
[Parameter]
437+
public bool IsVisible { get; set; } = true;
438+
423439
[CascadingParameter(Name = "Parent")]
424440
public Grid<TItem> Parent { get; set; } = default!;
425441

0 commit comments

Comments
 (0)