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

🐛 (DataTable): ItemColContent and CellRender cannot be used together #2026

Merged
merged 2 commits into from
Jul 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ You can use the dynamic slots **HeaderColContent** to customize only certain col

<masa-example file="Examples.components.data_tables.Header"></masa-example>

#### Item
#### Cell content

You can use the dynamic slots **ItemColContent** to customize only certain columns.

Expand Down
38 changes: 30 additions & 8 deletions src/Masa.Blazor/Components/DataTable/Row/ItemColProps.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
namespace Masa.Blazor;

public class ItemColProps<TItem>
public class ItemColProps<TItem>(DataTableHeader<TItem> header, TItem item)
{
public DataTableHeader<TItem> Header { get; set; }
public DataTableHeader<TItem> Header { get; } = header;

public object? Value => Header.ItemValue.Invoke(Item);
public TItem Item { get; } = item;

public TItem Item { get; set; }

public ItemColProps(DataTableHeader<TItem> header, TItem item)
/// <summary>
/// The value of current cell.
/// It's recommended to use <see cref="ValueContent"/>
/// to render cell content as it has better compatibility.
/// </summary>
public object? Value
{
Header = header;
Item = item;
get
{
if (Header.CellRender is not null)
{
var render = Header.CellRender.Invoke(Item);
if (render.IsT0)
{
return render.AsT0;
}

return render.AsT1;
}

return Header.ItemValue.Invoke(Item);
}
}

/// <summary>
/// The render fragment of current cell.
/// </summary>
public RenderFragment ValueContent
=> Value as RenderFragment ?? (RenderFragment)(builder => builder.AddContent(0, Value));
}
28 changes: 7 additions & 21 deletions src/Masa.Blazor/Components/DataTable/Row/MDataTableRow.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,13 @@
<td class="@GetCellClass(header)"
style="@GetCellStyle(header)"
title="@title">
@{
if (HasSlot(props))
{
@SlotContent(props)
}
else if (header.CellRender is not null)
{
var cell = header.CellRender(Item);
if (cell.IsT0)
{
@cell.AsT0
}
else
{
@cell.AsT1
}
}
else
{
@props.Value
}
@if (HasSlot(props))
{
@SlotContent(props)
}
else
{
@props.ValueContent
}
</td>
}
Expand Down
Loading