Skip to content

Commit 175dac6

Browse files
authored
feat(Table): can resize the fixed column (#5223)
* style: 增加 overflow: hidden 样式 * refactor: 固定列也可以调整列宽 * style: 微调 overflow 样式 * refactor: 增加固定列调整逻辑 * doc: 更新示例
1 parent 2e132a9 commit 175dac6

File tree

6 files changed

+50
-13
lines changed

6 files changed

+50
-13
lines changed

src/BootstrapBlazor.Server/Components/Samples/Table/TablesFixedColumn.razor

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
<section ignore>@((MarkupString)Localizer["TablesFixedColHeaderDescription"].Value)</section>
1616
<Table TItem="Foo"
1717
Items="@Items.Take(4)"
18-
RenderMode="TableRenderMode.Table"
18+
RenderMode="TableRenderMode.Table" AllowResizing="true"
1919
IsBordered="true"
2020
IsStriped="true">
2121
<TableColumns>
2222
<TableColumn @bind-Field="@context.Name" Width="120" Fixed="true" />
23-
<TableColumn @bind-Field="@context.DateTime" Width="180" Fixed="true" Filterable="true" />
23+
<TableColumn @bind-Field="@context.DateTime" Width="180" Filterable="true" />
2424
<TableColumn @bind-Field="@context.Address" Width="1400" Filterable="true" />
25-
<TableColumn @bind-Field="@context.Education" Width="100" Filterable="true" />
26-
<TableColumn @bind-Field="@context.Count" Width="100" Filterable="true" />
25+
<TableColumn @bind-Field="@context.Hobby" Width="140" />
26+
<TableColumn @bind-Field="@context.Education" Width="100" Filterable="true" Fixed="true" />
27+
<TableColumn @bind-Field="@context.Count" Width="100" Filterable="true" Fixed="true" />
2728
<TableColumn @bind-Field="@context.Complete" Width="100" Fixed="true" Filterable="true" />
2829
</TableColumns>
2930
</Table>
@@ -34,7 +35,7 @@
3435
Name="FixedColTableHeader">
3536
<Table TItem="Foo"
3637
Items="@Items.Take(10)"
37-
RenderMode="TableRenderMode.Table"
38+
RenderMode="TableRenderMode.Table" AllowResizing="true"
3839
IsBordered="true" IsStriped="true"
3940
IsMultipleSelect="true"
4041
Height="200"

src/BootstrapBlazor.Server/Locales/en-US.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"TablesFixedColumnNote": "For fixed column usage, please set the fixed column width as much as possible",
8484
"TablesFixedColHeaderTitle": "Fixed column header and column footer",
8585
"TablesFixedColHeaderIntro": "Set the <code>Fixed</code> attribute to fix the header",
86-
"TablesFixedColHeaderDescription": "In this example, set <code>Name</code> <code>DateTime</code> <code>Complete</code> The first two columns and the last column are fixed columns, and the middle columns are scrolled horizontally",
86+
"TablesFixedColHeaderDescription": "In this example, set <code>Name</code> <code>Education</code> <code>Count</code> <code>Complete</code> The first two columns and the last column are fixed columns, and the middle columns are scrolled horizontally",
8787
"TablesFixedColTableHeaderTitle": "Fixed headers and columns",
8888
"TablesFixedColTableHeaderIntro": "Set <code>IsFixedHeader=\"true\"</code> to fix the header, set <code>Fixed</code> to fix the column",
8989
"TablesFixedExtendButtonsColumnTitle": "Fixed button action column",

src/BootstrapBlazor.Server/Locales/zh-CN.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"TablesFixedColumnNote": "固定列用法请尽可能的设置固定列宽度,本页面如果宽度过长,请 <kbd>F12</kbd> 人为减小可视页面宽度",
8484
"TablesFixedColHeaderTitle": "固定列头与列尾",
8585
"TablesFixedColHeaderIntro": "设置 <code>Fixed</code> 属性固定表头",
86-
"TablesFixedColHeaderDescription": "本例中设置 <code>Name</code> <code>DateTime</code> <code>Complete</code> 前两列和最后一列为固定列,中间各列进行水平滚动",
86+
"TablesFixedColHeaderDescription": "本例中设置 <code>Name</code> <code>Education</code> <code>Count</code> <code>Complete</code> 前两列和最后一列为固定列,中间各列进行水平滚动,本例中设置 <code>AllowResize=\"true\"</code> 固定列也可以调整宽度",
8787
"TablesFixedColTableHeaderTitle": "固定表头与列",
8888
"TablesFixedColTableHeaderIntro": "设置 <code>IsFixedHeader=\"true\"</code> 固定表头,设置 <code>Fixed</code> 对列进行固定",
8989
"TablesFixedExtendButtonsColumnTitle": "固定按钮操作列",

src/BootstrapBlazor/Components/Table/Table.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@
602602
</Popover>
603603
}
604604
</div>
605-
@if (!col.Fixed && AllowResizing)
605+
@if (AllowResizing)
606606
{
607607
<span class="col-resizer" data-bb-field="@col.GetFieldName()"></span>
608608
}

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

+37-1
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,10 @@ const setResizeListener = table => {
586586
const marginX = eventX - originalX
587587
table.tables.forEach(t => {
588588
const group = [...t.children].find(i => i.nodeName === 'COLGROUP')
589+
const calcColWidth = colWidth + marginX;
589590
if (group) {
590591
const curCol = group.children.item(colIndex)
591-
curCol.style.width = `${colWidth + marginX}px`
592+
curCol.style.setProperty('width', `${calcColWidth}px`);
592593
const tableEl = curCol.closest('table')
593594
let width = tableWidth + marginX
594595
if (t.closest('.table-fixed-body')) {
@@ -606,6 +607,22 @@ const setResizeListener = table => {
606607
tip.update();
607608
}
608609
}
610+
611+
const header = col.parentElement;
612+
if (header.classList.contains('fixed')) {
613+
resizeNextFixedColumnWidth(header, calcColWidth);
614+
}
615+
}
616+
617+
const tbody = [...t.children].find(i => i.nodeName === 'TBODY');
618+
if (tbody) {
619+
const rows = [...tbody.children].filter(i => i.nodeName === 'TR');
620+
rows.forEach(row => {
621+
const header = row.children.item(colIndex);
622+
if (header.classList.contains('fixed')) {
623+
resizeNextFixedColumnWidth(header, calcColWidth);
624+
}
625+
});
609626
}
610627
})
611628
},
@@ -624,6 +641,25 @@ const setResizeListener = table => {
624641
})
625642
}
626643

644+
const resizeNextFixedColumnWidth = (col, width) => {
645+
if (col.classList.contains('fixed-right')) {
646+
const nextColumn = col.previousElementSibling;
647+
if (nextColumn.classList.contains('fixed')) {
648+
const right = parseFloat(col.style.getPropertyValue('right'));
649+
nextColumn.style.setProperty('right', `${right + width}px`);
650+
resizeNextFixedColumnWidth(nextColumn, nextColumn.offsetWidth);
651+
}
652+
}
653+
else if (col.classList.contains('fixed')) {
654+
const nextColumn = col.nextElementSibling;
655+
if (nextColumn.classList.contains('fixed')) {
656+
const left = parseFloat(col.style.getPropertyValue('left'));
657+
nextColumn.style.setProperty('left', `${left + width}px`);
658+
resizeNextFixedColumnWidth(nextColumn, nextColumn.offsetWidth);
659+
}
660+
}
661+
}
662+
627663
const setColumnResizingListen = (table, col) => {
628664
if (table.options.showColumnWidthTooltip) {
629665
EventHandler.on(col, 'mouseenter', e => {

src/BootstrapBlazor/Components/Table/Table.razor.scss

+4-4
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@
384384
overflow: hidden;
385385
}
386386

387+
td > .table-cell {
388+
overflow: hidden;
389+
}
390+
387391
.table tbody td .table-cell:not(.is-wrap) {
388392
white-space: nowrap;
389393
}
@@ -555,10 +559,6 @@ tr.active:not(.is-edit):hover {
555559
pointer-events: none;
556560
}
557561

558-
.table-fixed-column .table .fixed-right {
559-
border-right: inherit;
560-
}
561-
562562
.table-fixed-column .table .fixed-right.fl {
563563
border-left: 1px solid var(--bb-table-column-fixed-border-color);
564564
}

0 commit comments

Comments
 (0)