Skip to content

Commit

Permalink
🆕 feat(DataTable): add support for displaying index column by ShowSer…
Browse files Browse the repository at this point in the history
…ialNumber (#2200)
  • Loading branch information
capdiem authored Oct 23, 2024
1 parent 476ed25 commit 0941bb5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@
</MSimpleCheckbox>
}
};

protected RenderFragment GenNumber() => __builder =>
{
<span>#</span>
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
{
@GenSelectAll()
}
else if (header.Value == "data-table-no")
{
@GenNumber()
}
else
{
@if (!DisableSort && header.Sortable && header.Align == DataTableHeaderAlign.End)
Expand Down
28 changes: 17 additions & 11 deletions src/Masa.Blazor/Components/DataTable/MDataTable.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@inherits MDataIterator<TItem>
@using Masa.Blazor.Components.DataTable

<CascadingValue Value="@(Headers.Any(h => h.Fixed != DataTableFixed.None))"
<CascadingValue Value="@HasFixedColumn"
Name="DataTableHasFixed">
<MSimpleTable Height="@Height"
Width="@Width"
Expand Down Expand Up @@ -49,7 +49,7 @@

private RenderFragment GenColgroup() => __builder =>
{
<MDataTableColgroup Headers="@ComputedHeaders.ToList()" />
<MDataTableColgroup Headers="@ComputedHeaders.ToList()"/>
};

private RenderFragment GenHeaders() => __builder =>
Expand Down Expand Up @@ -77,7 +77,7 @@
OnToggleSelectAll="@ToggleSelectAll"
OnSort="@Sort"
OnGroup="@Group"
@attributes="@HeaderProps" />
@attributes="@HeaderProps"/>
}

@if (Loading == true)
Expand Down Expand Up @@ -176,14 +176,14 @@
Icon
Small
OnClick="@ToggleFn">
<MIcon Icon="@(isOpen ? "$minus" : "$plus")" />
<MIcon Icon="@(isOpen ? "$minus" : "$plus")"/>
</MButton>
@($"{GetText(Options.GroupBy[0])}: {group.Key}")
<MButton Class="ma-0"
Icon
Small
OnClick="@RemoveGroup">
<MIcon Icon="@("$close")" />
<MIcon Icon="@("$close")"/>
</MButton>
</td>
}
Expand Down Expand Up @@ -253,13 +253,13 @@
var disabled = IsSelectable(item) == false;
var isSelected = IsSelected(item);
var isExpanded = IsExpanded(item);

Dictionary<string, object?> parameters = new()
{
{ nameof(MDataTableRow<TItem>.Headers), ComputedHeaders },
{ nameof(MDataTableRow<TItem>.IsSelected), isSelected },
{ nameof(MDataTableRow<TItem>.IsExpanded), isExpanded },
{ nameof(MDataTableRow<TItem>.Disabled), disabled},
{ nameof(MDataTableRow<TItem>.Disabled), disabled },
{ nameof(MDataTableRow<TItem>.Stripe), Stripe },
{ nameof(MDataTableRow<TItem>.ItemClass), ItemClass },
{ nameof(MDataTableRow<TItem>.Index), index },
Expand Down Expand Up @@ -287,11 +287,13 @@

bool HasSelect(ItemColProps<TItem> props) => props.Header.Value == "data-table-select" && ShowSelect;

bool HasNumber(ItemColProps<TItem> props) => props.Header.Value == "data-table-no" && ShowSerialNumber;

bool HasItemColContent() => ItemColContent is not null;

bool HasSlot(ItemColProps<TItem> props)
{
return HasExpand(props) || HasSelect(props) || HasItemColContent();
return HasExpand(props) || HasSelect(props) || HasNumber(props) || HasItemColContent();
}

RenderFragment<ItemColProps<TItem>> GenRowSlot() => context => __builder =>
Expand Down Expand Up @@ -327,9 +329,9 @@
</MIcon>
}
}
else if (HasSelect((context)))
else if (HasSelect(context))
{
if (ItemDataTableSelectContent != null)
if (ItemDataTableSelectContent != null)
{
@ItemDataTableSelectContent(CreateExpandOrSelectContext())
}
Expand All @@ -343,6 +345,10 @@
</MSimpleCheckbox>
}
}
else if (HasNumber(context))
{
<span>@(index + 1 + (Pagination.Page - 1) * Pagination.ItemsPerPage)</span>
}
else if (HasItemColContent())
{
@ItemColContent!(context)
Expand Down Expand Up @@ -385,4 +391,4 @@
</MProgressLinear>
};

}
}
39 changes: 36 additions & 3 deletions src/Masa.Blazor/Components/DataTable/MDataTable.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public partial class MDataTable<TItem> : MDataIterator<TItem>
[Parameter]
public bool ShowExpand { get; set; }

[Parameter]
[MasaApiParameter(ReleasedOn = "v1.8.0")]
public bool ShowSerialNumber { get; set; }

[Parameter] public RenderFragment<DataTableHeaderSelectContext>? HeaderDataTableSelectContent { get; set; }

[Parameter]
Expand Down Expand Up @@ -131,6 +135,10 @@ public DataTableResizeMode ResizeMode
}

private bool _prevIsMobile;

private bool HasFixedColumn => Headers.Any(h => h.Fixed != DataTableFixed.None);

private bool HasLeftFixedColumn => Headers.Any(h => h.Fixed == DataTableFixed.Left);

public IEnumerable<DataTableHeader<TItem>> ComputedHeaders
{
Expand All @@ -145,15 +153,40 @@ public IEnumerable<DataTableHeader<TItem>> ComputedHeaders
.Where(header => header.Value == null || InternalOptions.GroupBy.FirstOrDefault(by => by == header.Value) == null)
.ToList();

if (ShowSerialNumber)
{
var index = headers.FindIndex(r => r.Value == "data-table-no");
if (index == -1)
{
headers.Insert(0, new DataTableHeader<TItem>()
{
Width = "56px",
Value = "data-table-no",
Fixed = HasLeftFixedColumn ? DataTableFixed.Left : DataTableFixed.None,
Sortable = false
});
}
else
{
var header = headers[index];
header.Sortable = false;
if (header.Width == null)
{
header.Width = "56px";
}
}
}

if (ShowSelect)
{
var fixedSelect = FixedSelect || HasLeftFixedColumn;
var index = headers.FindIndex(r => r.Value == "data-table-select");
if (index < 0)
{
headers.Insert(0, new DataTableHeader<TItem>
{
Fixed = FixedSelect ? DataTableFixed.Left : DataTableFixed.None,
Width = "56px",
Fixed = fixedSelect ? DataTableFixed.Left : DataTableFixed.None,
Width = "64px",
Value = "data-table-select",
Sortable = false
});
Expand All @@ -164,7 +197,7 @@ public IEnumerable<DataTableHeader<TItem>> ComputedHeaders
header.Sortable = false;
if (header.Width == null)
{
header.Width = "1px";
header.Width = "64px";
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<tr class="@GetClass()" @attributes="@Attributes">
@foreach (var header in Headers)
{
if (header.Value == "data-table-no")
{
continue;
}

<td class="@_block.Element("mobile-row").Name">
@{
if (header.Value != "data-table-select" && !HideDefaultHeader)
Expand Down Expand Up @@ -43,8 +48,7 @@
{
@props.Value
}
</div>
}
</div>}
</td>
}
</tr>
</tr>

0 comments on commit 0941bb5

Please sign in to comment.