Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid : Workaround for DatePicker being set to null no matter what when filter is set through Parameter FilterSearchValue #5838

Merged
merged 3 commits into from
Nov 13, 2024
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
2 changes: 1 addition & 1 deletion Source/Extensions/Blazorise.DataGrid/DataGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
}
else
{
<_DataGridCellFilter Column="column" FilterSearchValue="@column.Filter.SearchValue" OnFilterChanged="@((newValue) => OnFilterChanged(column, newValue))" />
<_DataGridCellFilter Column="column" OnFilterChanged="@((newValue) => OnFilterChanged(column, newValue))" />
}
</TableHeaderCell>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Blazorise.DataGrid;

/// <summary>
/// Defines the DataGrid cell filter type.
/// </summary>
public enum DataGridCellFilterType
{
/// <summary>
/// Represents a single filter, allowing only one value.
/// </summary>
Single,

/// <summary>
/// Represents the starting value of a range filter.
/// </summary>
RangeFrom,

/// <summary>
/// Represents the ending value of a range filter.
/// </summary>
RangeTo,
}
65 changes: 59 additions & 6 deletions Source/Extensions/Blazorise.DataGrid/_DataGridCellFilter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,86 @@
@{
if ( Column is DataGridNumericColumn<TItem> numericColumn )
{
<_DataGridCellNumericEdit Column="@numericColumn" CellValue="@FilterSearchValue" CellValueChanged="@OnFilterChanged"></_DataGridCellNumericEdit>
<_DataGridCellNumericEdit Column="@numericColumn" CellValue="@GetSearchValue()" CellValueChanged="@FilterChanged"></_DataGridCellNumericEdit>

}
else if ( Column is DataGridSelectColumn<TItem> selectColumn )
{
<_DataGridCellSelectEdit Column="@selectColumn" CellValue="@FilterSearchValue" CellValueChanged="@OnFilterChanged"></_DataGridCellSelectEdit>
<_DataGridCellSelectEdit Column="@selectColumn" CellValue="@GetSearchValue()" CellValueChanged="@FilterChanged"></_DataGridCellSelectEdit>

}
else if ( Column is DataGridCheckColumn<TItem> checkColumn )
{
<_DataGridCellCheckEdit Column="@checkColumn" CellValue="@FilterSearchValue" CellValueChanged="@OnFilterChanged"></_DataGridCellCheckEdit>
<_DataGridCellCheckEdit Column="@checkColumn" CellValue="@GetSearchValue()" CellValueChanged="@FilterChanged"></_DataGridCellCheckEdit>

}
else if ( Column is DataGridDateColumn<TItem> dateColumn )
{
<_DataGridCellDatePicker Column="@dateColumn" CellValue="@FilterSearchValue" CellValueChanged="@OnFilterChanged"></_DataGridCellDatePicker>
<_DataGridCellDatePicker Column="@dateColumn" CellValue="@GetSearchValue()" CellValueChanged="@FilterChanged"></_DataGridCellDatePicker>

}
else
{
<TextEdit Text="@FilterSearchValue?.ToString()" TextChanged="@((value) => OnFilterChanged.InvokeAsync(value))" />
<TextEdit Text="@GetSearchValueAsString()" TextChanged="@((newValue) => FilterChanged(newValue))" />
}
}
@code {
[Parameter] public DataGridColumn<TItem> Column { get; set; }

[Parameter] public EventCallback<object> OnFilterChanged { get; set; }

[Parameter] public object FilterSearchValue { get; set; }
/// <summary>
/// Workaround for an issue with the DatePicker setting the value to null if we try to set FilterValue as a Parameter.
/// </summary>
[Parameter] public DataGridCellFilterType CellFilterType { get; set; } = DataGridCellFilterType.Single;

private Task FilterChanged( object newValue )
{
return OnFilterChanged.InvokeAsync( newValue );
}

private string GetSearchValueAsString()
{
if ( CellFilterType == DataGridCellFilterType.Single )
{
return Column.Filter.SearchValue?.ToString();
}
else if ( CellFilterType == DataGridCellFilterType.RangeFrom )
{
return GetFilterValue1()?.ToString();
}
else if ( CellFilterType == DataGridCellFilterType.RangeTo )
{
return GetFilterValue2()?.ToString();
}
return default;
}

private object GetSearchValue()
{
if ( CellFilterType == DataGridCellFilterType.Single )
{
return Column.Filter.SearchValue;
}
else if ( CellFilterType == DataGridCellFilterType.RangeFrom )
{
return GetFilterValue1();
}
else if ( CellFilterType == DataGridCellFilterType.RangeTo )
{
return GetFilterValue2();
}
return default;
}

private object GetFilterValue1()
{
return ( Column.Filter.SearchValue as object[] )?[0];
}

private object GetFilterValue2()
{
return ( Column.Filter.SearchValue as object[] )?[1];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
<Field @key=filterMethod>
@if ( filterMethod == DataGridColumnFilterMethod.Between )
{
<_DataGridCellFilter Column="Column" FilterSearchValue="@GetFilterValue1()" OnFilterChanged="@SetFilterValue1" />
<_DataGridCellFilter Column="Column" FilterSearchValue="@GetFilterValue2()" OnFilterChanged="@SetFilterValue2" />
<_DataGridCellFilter Column="Column" CellFilterType="DataGridCellFilterType.RangeFrom" OnFilterChanged="@SetFilterValue1" />
<_DataGridCellFilter Column="Column" CellFilterType="DataGridCellFilterType.RangeTo" OnFilterChanged="@SetFilterValue2" />
}
else
{
<_DataGridCellFilter Column="Column" FilterSearchValue="Column.Filter.SearchValue" OnFilterChanged="@((newValue) => Column.Filter.SearchValue = newValue)" />
<_DataGridCellFilter Column="Column" OnFilterChanged="@((newValue) => Column.Filter.SearchValue = newValue)" />
}
</Field>
</Column>
Expand Down
Loading