Skip to content

Commit 5dacb93

Browse files
ArgoZhangljmay168
andauthored
fix(Table): reset columns after toggle visible #7176 (#7180) (#7186)
* fix(Table): 列被隐藏再取消隐藏后,拖动列改变列顺序的功能不正常 #7176 * refactor: 复用脚本 * test: 更新单元测试 * test: 重构单元测试 --------- Co-Authored-By: ljmay168 <209402714+ljmay168@users.noreply.github.com>
1 parent b161e57 commit 5dacb93

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ protected async Task OnCheck(CheckboxState state, TItem val)
143143
/// </summary>
144144
private bool _resetColumns;
145145

146+
/// <summary>
147+
/// 是否重置列拖拽事件 <see cref="OnAfterRenderAsync(bool)"/> 方法中重置为 false
148+
/// </summary>
149+
private bool _resetColDragListener;
150+
146151
/// <summary>
147152
/// 获得/设置 列改变显示状态回调方法
148153
/// </summary>
@@ -155,6 +160,10 @@ private async Task OnToggleColumnVisible(string columnName, bool visible)
155160
{
156161
_resetColumns = true;
157162
}
163+
if (AllowDragColumn && visible)
164+
{
165+
_resetColDragListener = true;
166+
}
158167
if (!string.IsNullOrEmpty(ClientTableName))
159168
{
160169
await InvokeVoidAsync("saveColumnList", ClientTableName, _visibleColumns);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
10421042
await InvokeVoidAsync("resetColumn", Id);
10431043
}
10441044

1045+
if (_resetColDragListener)
1046+
{
1047+
_resetColDragListener= false;
1048+
await InvokeVoidAsync("resetColDragListener", Id);
1049+
}
1050+
10451051
if (_bindResizeColumn)
10461052
{
10471053
_bindResizeColumn = false;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { getResponsive } from '../../modules/responsive.js'
1+
export { getResponsive } from '../../modules/responsive.js'
22
import { copy, drag, getDescribedElement, getOuterHeight, getWidth, isVisible } from '../../modules/utility.js'
33
import '../../modules/browser.js'
44
import Data from '../../modules/data.js'
@@ -180,6 +180,13 @@ export function resetColumn(id) {
180180
}
181181
}
182182

183+
export function resetColDragListener(id) {
184+
const table = Data.get(id)
185+
if (table) {
186+
setDraggable(table)
187+
}
188+
}
189+
183190
export function bindResizeColumn(id) {
184191
const table = Data.get(id)
185192
if (table) {
@@ -924,6 +931,8 @@ const setDraggable = table => {
924931
let index = 0
925932
table.dragColumns = [...table.tables[0].querySelectorAll('thead > tr > th')].filter(i => i.draggable)
926933
table.dragColumns.forEach(col => {
934+
disposeDragColumns(col);
935+
927936
EventHandler.on(col, 'dragstart', e => {
928937
col.parentNode.classList.add('table-dragging')
929938
col.classList.add('table-drag')

test/UnitTest/Components/TableTest.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8476,6 +8476,8 @@ public async Task AllowDragColumn_Ok()
84768476
{
84778477
pb.Add(a => a.RenderMode, TableRenderMode.Table);
84788478
pb.Add(a => a.AllowDragColumn, true);
8479+
pb.Add(a => a.ShowToolbar, true);
8480+
pb.Add(a => a.ShowColumnList, true);
84798481
pb.Add(a => a.ClientTableName, "table-unit-test");
84808482
pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer));
84818483
pb.Add(a => a.OnDragColumnEndAsync, (fieldName, columns) =>
@@ -8490,9 +8492,14 @@ public async Task AllowDragColumn_Ok()
84908492
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
84918493
builder.CloseComponent();
84928494

8493-
builder.OpenComponent<TableColumn<Foo, string>>(0);
8494-
builder.AddAttribute(3, "Field", "Address");
8495-
builder.AddAttribute(4, "FieldExpression", Utility.GenerateValueExpression(foo, "Address", typeof(string)));
8495+
builder.OpenComponent<TableColumn<Foo, string>>(3);
8496+
builder.AddAttribute(4, "Field", "Address");
8497+
builder.AddAttribute(5, "FieldExpression", Utility.GenerateValueExpression(foo, "Address", typeof(string)));
8498+
builder.CloseComponent();
8499+
8500+
builder.OpenComponent<TableColumn<Foo, int>>(6);
8501+
builder.AddAttribute(7, "Field", foo.Count);
8502+
builder.AddAttribute(8, "FieldExpression", Utility.GenerateValueExpression(foo, "Count", typeof(int)));
84968503
builder.CloseComponent();
84978504
});
84988505
});
@@ -8502,17 +8509,28 @@ public async Task AllowDragColumn_Ok()
85028509
await cut.InvokeAsync(async () =>
85038510
{
85048511
await table.Instance.DragColumnCallback(1, 0);
8505-
Assert.Equal("Address", name);
85068512
});
8513+
Assert.Equal("Address", name);
8514+
8515+
var columns = cut.FindAll("th");
8516+
Assert.Contains("地址", columns[0].InnerHtml);
8517+
Assert.Contains("姓名", columns[1].InnerHtml);
85078518

85088519
await cut.InvokeAsync(async () =>
85098520
{
8510-
var columns = cut.FindAll("th");
8511-
Assert.Contains("地址", columns[0].InnerHtml);
8512-
Assert.Contains("姓名", columns[1].InnerHtml);
8513-
85148521
await table.Instance.DragColumnCallback(2, 3);
85158522
});
8523+
8524+
// 更改可见列
8525+
var checkbox = cut.Find(".dropdown-item .form-check-input");
8526+
8527+
await cut.InvokeAsync(() => checkbox.Click());
8528+
await cut.InvokeAsync(() => checkbox.Click());
8529+
8530+
await cut.InvokeAsync(async () =>
8531+
{
8532+
await table.Instance.DragColumnCallback(3, 4);
8533+
});
85168534
}
85178535

85188536
[Fact]

0 commit comments

Comments
 (0)