MudBlazor DataGrid在FireFox中单元格文本溢出问题求助
解决MudBlazor DataGrid在FireFox中单元格文本溢出相邻列的问题
针对你遇到的FireFox中单元格超长文本溢出相邻列的问题,提供两个可行的修复方案:
方案1:给列添加MinWidth属性并优化单元格样式
FireFox对表格列宽的计算逻辑和Chrome不同,仅设置max-width不足以约束超长无空格文本的列宽,给PropertyColumn添加MinWidth可以明确列的最小宽度,配合overflow属性就能让FireFox正确截断溢出内容。同时可以加上text-overflow: ellipsis让溢出部分显示省略号(可选)。
修改后的代码:
<div style="width: 50vw;"> <MudDataGrid T="Person" Items="@People" ColumnResizeMode="ResizeMode.Container"> <Columns> <PropertyColumn Property="x => x.Name" MinWidth="100" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap; text-overflow: ellipsis;" /> <PropertyColumn Property="x => x.State" MinWidth="100" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap; text-overflow: ellipsis;" /> <PropertyColumn Property="x => x.Age" MinWidth="100" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap; text-overflow: ellipsis;" /> </Columns> </MudDataGrid> </div> @code { private IEnumerable<Person> People = new List<Person> { new Person("Brady", 25, "Florida"), new Person("Tim", 32, "Minnesota"), new Person("Derek", 51, "Utahqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"), new Person("Ben", 27, "Wisconsin"), new Person("Sam", 34, "Alaska") }; public class Person { public string Name { get; set; } public int Age { get; set; } public string State { get; set; } public Person(string name, int age, string state) { Name = name; Age = age; State = state; } } }
方案2:给DataGrid添加table-layout: fixed样式
通过设置表格的table-layout: fixed属性,强制表格采用固定布局模式,此时所有列的宽度会严格遵循你设置的max-width约束,超长内容不会撑开列宽,直接被截断。
修改后的代码:
<div style="width: 50vw;"> <MudDataGrid T="Person" Items="@People" ColumnResizeMode="ResizeMode.Container" Style="table-layout: fixed; width: 100%;"> <Columns> <PropertyColumn Property="x => x.Name" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap; text-overflow: ellipsis;" /> <PropertyColumn Property="x => x.State" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap; text-overflow: ellipsis;" /> <PropertyColumn Property="x => x.Age" CellStyle="max-width: 100px; overflow-x: hidden; white-space: nowrap; text-overflow: ellipsis;" /> </Columns> </MudDataGrid> </div> @code { private IEnumerable<Person> People = new List<Person> { new Person("Brady", 25, "Florida"), new Person("Tim", 32, "Minnesota"), new Person("Derek", 51, "Utahqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"), new Person("Ben", 27, "Wisconsin"), new Person("Sam", 34, "Alaska") }; public class Person { public string Name { get; set; } public int Age { get; set; } public string State { get; set; } public Person(string name, int age, string state) { Name = name; Age = age; State = state; } } }
方案对比
- 方案1保留了MudBlazor列的自适应特性,同时通过
MinWidth约束FireFox的列宽计算,列拖拽调整功能不受影响。 - 方案2更直接,固定表格布局后所有列宽严格遵循设置,适合列宽固定的场景,同样支持列拖拽调整。
内容的提问来源于stack exchange,提问作者LongNeckTimmy
相关产品推荐
相关产品推荐

