dotnet / dotnet/aspnetcore

Make QuickGrid more inheritance friendly (second try)

Open
#63,326 6 comments 0 reactions 0 assignees View on GitHub
area-blazor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I am try to Inherited a component from QickGrid to archive classic RowSelect (blue TR background after Row click).

I suggest this feature some years ago , see https://github.com/issues/created?q=is%3Aissue+author%3A%40me+sort%3Aupdated-desc&issue=dotnet%7Caspnetcore%7C45182

### Describe the solution you'd like

Change some private Modifier to protected to help people to Inherit there own Grid from QickGrid. Or find some other way to override RenderRow and so on.

### Additional context

currently I use the (brutal) UnsafeAccessor to archive my goal

```
public static class QuckGridUnsafeAccess where TGridItem : class
{
//Access Fields
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_currentNonVirtualizedViewItems")]
public static extern ref ICollection Get_currentNonVirtualizedViewItems(QuickGrid instance);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_renderNonVirtualizedRows")]
public static extern ref RenderFragment Get_renderNonVirtualizedRows(QuickGrid instance);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_columns")]
public static extern ref List> Get_columns(QuickGrid instance);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_virtualizeComponent")]
public static extern ref Virtualize<(int, TGridItem)>? Get_virtualizeComponent(QuickGrid instance);

//Access Methods
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "RenderPlaceholderRow")]
public static extern void Call_RenderPlaceholderRow(QuickGrid instance,RenderTreeBuilder builder,PlaceholderContext placeholderContext);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "CellContent")]
public static extern void Call_CellContent(ColumnBase instance, RenderTreeBuilder builder, TGridItem item);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ProvideVirtualizedItems")]
public extern static ValueTask> Call_ProvideVirtualizedItemsAsync(QuickGrid instance, ItemsProviderRequest request);
public static ItemsProviderDelegate<(int, TGridItem)> Get_UnsafeItemsProviderDelegate(EBQuickGrid gridInstance)
{
return async request =>
{

var ret = await Call_ProvideVirtualizedItemsAsync(gridInstance as QuickGrid, request);
return ret;
};
}

//Access Static Methods
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "ColumnClass")]
public extern static string? Call_ColumnClass(QuickGrid? unused, ColumnBase column);

}

```

now I can create my own EBQickGrid :QuickGrid

to bend the _renderNonVirtualizedRows RenderFragment

```
ref RenderFragment _renderNonVirtualizedRows = ref QuckGridUnsafeAccess.Get_renderNonVirtualizedRows(this);
_renderNonVirtualizedRows = RenderRowsEBQickGrid;

```
And render the row with an OnClick eventhandler and optional with select attribute

```
private void RenderRowsEBQickGrid(RenderTreeBuilder __builder)
{
@if (VirtualizeEBGrid)
{

RenderVirtualizedRowsEBQickGrid(__builder);
}
else
{
RenderNonVirtualizedRowsEBQickGrid(__builder);
}
}
private void RenderVirtualizedRowsEBQickGrid(RenderTreeBuilder __builder)
{

}

private void RenderNonVirtualizedRowsEBQickGrid(RenderTreeBuilder __builder)
{
var initialRowIndex = 2; // aria-rowindex is 1-based, plus the first row is the header
var rowIndex = initialRowIndex;

var _currentNonVirtualizedViewItems = QuckGridUnsafeAccess.Get_currentNonVirtualizedViewItems(this );

foreach (var item in _currentNonVirtualizedViewItems)
{
RenderRowEBQuckGrid(__builder, rowIndex++, item);
}

// When pagination is enabled, by default ensure we render the exact number of expected rows per page,
// even if there aren't enough data items. This avoids the layout jumping on the last page.
// Consider making this optional.
if (Pagination is not null)
{
var _columns = QuckGridUnsafeAccess.Get_columns(this);
while (rowIndex++ < initialRowIndex + Pagination.ItemsPerPage)
{

@foreach (var col in _columns)
{
var cssClass = QuckGridUnsafeAccess.Call_ColumnClass(null, col);

}

}
}
}

protected void RenderRowEBQuckGrid(RenderTreeBuilder __builder, int rowIndex, TGridItem item)
{
bool selected = ShouldSelect(item);
var rowClass = RowClass?.Invoke(item);
var _columns = QuckGridUnsafeAccess.Get_columns(this );


@foreach (var col in _columns)
{
var cssClass = QuckGridUnsafeAccess.Call_ColumnClass(null, col);


@{
QuckGridUnsafeAccess.Call_CellContent(col , __builder, item);
}

}

}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.