How to prevent Mud Blazor MudTable columns from expanding to fit text
P粉212114661
P粉212114661 2023-12-27 11:05:27
0
1
455

I have a table with 5 columns, one of which contains some very long user IDs (without spaces). Instead of cutting the text off, it expands the column to accommodate all the content, pushing the other columns to the right and causing scrollbars to appear.

I've been researching for hours trying to figure out how to fix the width and stop it from overflowing. I tried setting the table-layout attribute on the MudTable element to fixed and tried using a variation of width:20%;; word wrapping :newline; whitespace: nowrap; overflow: hidden; text-overflow: ellipsis; on anything I can think of, like col MudTd etc., but nothing works , doesn't even have any effect.

The documentation doesn't seem to cover overflow at all, which is frustrating. Setting the col width works great until the data gets too long, and setting the max-width doesn't help either.

This seems to be an issue in MudTable that should be met, can anyone tell me what I'm doing wrong, or suggest a fix?

This is my table:

<MudTable id="results-table" ServerData="@(new Func<TableState, Task<TableData<HiHi1User>>>(LoadUsers))" RowsPerPage="50" FixedHeader=@_tableFixedHeader FixedFooter=@_tableFixedFooter
         Style="table-layout:fixed" Height=@_tableFixedHeight Hover="true" Striped="true" Breakpoint="Breakpoint.Sm" Loading="@_loading" LoadingProgressColor="Color.Primary">
        <ColGroup>
            <col style="width:20%;" />
            <col style="width:20%;" />
            <col style="width:20%;" />
            <col style="width:20%;" />
            <col style="width:20%;" />
        </ColGroup>
        <HeaderContent>
            <MudTh id="user-id-hdr">User ID</MudTh>
            <MudTh id="group-id-hdr">Group ID</MudTh>
            <MudTh id="current-versions-hdr">Current Version</MudTh>
            <MudTh id="max-versions-hdr">Max Version</MudTh>
            <MudTh id="can-alter-gnf-hdr">Can Alter Night Forwarding Status</MudTh>
        </HeaderContent>
        <RowTemplate>
            <MudTd id="user-id-val" DataLabel=@nameof(context.UserId)>@context.UserId</MudTd>
            <MudTd id="group-id-val" DataLabel=@nameof(context.GroupId)>@context.GroupId</MudTd>
            <MudTd id="current-versions-val" DataLabel=@nameof(context.CurrentVersion)>@context.CurrentVersion</MudTd>
            <MudTd id="max-versions-val" DataLabel=@nameof(context.MaxVersion)>@context.MaxVersion</MudTd>
            <MudTd id="can-alter-gnf-val" DataLabel=@nameof(context.CanAlterNightForwardingStatus)>@context.CanAlterNightForwardingStatus</MudTd>
        </RowTemplate>
        <PagerContent>
            <MudTablePager HorizontalAlignment="HorizontalAlignment.Left" HidePagination="false" />
        </PagerContent>
    </MudTable>

Thank you so much.

P粉212114661
P粉212114661

reply all(1)
P粉440453689

Try this. overflow-wrap:break-word;max-width:200px; on your line.

For some reason, max-width needs to be set to a > 0 value, but you can control the column width in the ColGroup style.

<RowTemplate>
    <MudTd DataLabel="Nr">@context.Number</MudTd>
    <MudTd DataLabel="Sign">@context.Sign</MudTd>
    <MudTd DataLabel="Name" Style="overflow-wrap:break-word;max-width:10px;">@context.Name</MudTd>
    <MudTd DataLabel="Position">@context.Position</MudTd>
    <MudTd DataLabel="Molar mass" Style="text-align:right">@context.Molar</MudTd>
</RowTemplate>

mudblazor code snippet

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template