Skip to content

Commit ef8f70f

Browse files
authored
refactor(Table): update calc cell width logic (#8013)
* refactor: 更新计算单元格宽度逻辑 * chore: bump version 10.6.1-beta23 * doc: 更新版本信息
1 parent 38985b7 commit ef8f70f

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.6.1-beta22</Version>
4+
<Version>10.6.1-beta23</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Table/Table.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private string GetSortTooltip(ITableColumn col) => SortName != col.GetFieldName(
446446
/// <summary>
447447
/// <para lang="zh">获得/设置 滚动行为,默认值为 <see cref="ScrollIntoViewBehavior.Smooth"/></para>
448448
/// <para lang="en">Gets or sets the scroll behavior. The default is <see cref="ScrollIntoViewBehavior.Smooth"/></para>
449-
/// <para>v<version>10.6.2</version></para>
449+
/// <para>v<version>10.6.1</version></para>
450450
/// </summary>
451451
[Parameter]
452452
public ScrollIntoViewBehavior ScrollIntoViewBehavior { get; set; } = ScrollIntoViewBehavior.Smooth;

src/BootstrapBlazor/Components/Table/Table.razor.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,53 @@ const autoFitColumnWidth = async (table, col) => {
627627
}
628628
}
629629

630+
const formControlSelector = 'input.form-control:not([type="hidden"]), textarea.form-control';
631+
632+
const getHorizontalWidth = style => {
633+
return (parseFloat(style.getPropertyValue('padding-left')) || 0)
634+
+ (parseFloat(style.getPropertyValue('padding-right')) || 0)
635+
+ (parseFloat(style.getPropertyValue('border-left-width')) || 0)
636+
+ (parseFloat(style.getPropertyValue('border-right-width')) || 0);
637+
}
638+
639+
const getFormControlTextWidth = control => {
640+
const style = getComputedStyle(control);
641+
const span = document.createElement('span');
642+
span.textContent = control.value || control.getAttribute('placeholder') || ' ';
643+
span.style.setProperty('visibility', 'hidden');
644+
span.style.setProperty('white-space', 'pre');
645+
span.style.setProperty('display', 'inline-block');
646+
span.style.setProperty('position', 'absolute');
647+
span.style.setProperty('top', '0');
648+
span.style.setProperty('font-family', style.getPropertyValue('font-family'));
649+
span.style.setProperty('font-size', style.getPropertyValue('font-size'));
650+
span.style.setProperty('font-style', style.getPropertyValue('font-style'));
651+
span.style.setProperty('font-weight', style.getPropertyValue('font-weight'));
652+
span.style.setProperty('letter-spacing', style.getPropertyValue('letter-spacing'));
653+
span.style.setProperty('text-transform', style.getPropertyValue('text-transform'));
654+
document.body.appendChild(span);
655+
656+
const width = getWidth(span) + getHorizontalWidth(style);
657+
span.remove();
658+
659+
return width;
660+
}
661+
662+
const resetFormControlWidth = (sourceCell, cloneCell) => {
663+
const controls = [...sourceCell.querySelectorAll(formControlSelector)];
664+
const cloneControls = [...cloneCell.querySelectorAll(formControlSelector)];
665+
controls.forEach((control, index) => {
666+
const cloneControl = cloneControls[index];
667+
if (cloneControl) {
668+
const width = getFormControlTextWidth(control);
669+
cloneControl.value = control.value;
670+
cloneControl.style.setProperty('width', `${width}px`, 'important');
671+
cloneControl.style.setProperty('min-width', '0', 'important');
672+
cloneControl.style.setProperty('flex', '0 0 auto', 'important');
673+
}
674+
});
675+
}
676+
630677
const calcCellWidth = cell => {
631678
const div = document.createElement('div');
632679
[...cell.children].forEach(c => {
@@ -638,9 +685,13 @@ const calcCellWidth = cell => {
638685
div.style.setProperty('position', 'absolute');
639686
div.style.setProperty('top', '0');
640687
document.body.appendChild(div);
688+
resetFormControlWidth(cell, div);
641689

642690
const cellStyle = getComputedStyle(cell);
643-
return getWidth(div) + parseFloat(cellStyle.getPropertyValue('padding-left')) + parseFloat(cellStyle.getPropertyValue('padding-right')) + parseFloat(cellStyle.getPropertyValue('border-left-width')) + parseFloat(cellStyle.getPropertyValue('border-right-width')) + 1 | 0;
691+
const width = getWidth(div) + getHorizontalWidth(cellStyle) + 1 | 0;
692+
div.remove();
693+
694+
return width;
644695
}
645696

646697
const closeAllTips = (columns, self) => {

0 commit comments

Comments
 (0)