Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Nov 26, 2025

Link issues

fixes #7176

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Ensure table column drag behavior is correctly reset after toggling column visibility.

Bug Fixes:

  • Reset the table column drag event listeners when columns are shown/hidden so drag-and-drop continues to work after visibility changes.

Tests:

  • Extend the table drag-column unit test to cover scenarios with the column chooser toolbar and verify drag behavior after toggling column visibility.

* fix(Table): 列被隐藏再取消隐藏后,拖动列改变列顺序的功能不正常 #7176

* refactor: 复用脚本

* test: 更新单元测试

* test: 重构单元测试

---------

Co-authored-by: Argo Zhang <argo@live.ca>
Copilot AI review requested due to automatic review settings November 26, 2025 08:28
@bb-auto bb-auto bot added the bug Something isn't working label Nov 26, 2025
@bb-auto bb-auto bot added this to the 10.0.0 milestone Nov 26, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Nov 26, 2025

Reviewer's Guide

Ensures table column drag-and-drop handlers are correctly reset when toggling column visibility, and extends unit tests to cover toolbar, column list visibility toggling, and column drag behavior regression.

Sequence diagram for resetting column drag handlers after visibility toggle

sequenceDiagram
    actor User
    participant Toolbar as ColumnToolbar
    participant Table as TableGenericTItem
    participant JS as TableJsModule
    participant DOM as TableDom

    User->>Toolbar: click toggle visibility for column
    Toolbar->>Table: OnToggleColumnVisible(columnName, visible)
    alt column visibility changed
        Table->>Table: _resetColumns = true
    end
    alt AllowDragColumn and visible is true
        Table->>Table: _resetColDragListener = true
    end
    alt ClientTableName is not empty
        Table->>JS: InvokeVoidAsync saveColumnList(ClientTableName, _visibleColumns)
    end

    Table->>Table: OnAfterRenderAsync(firstRender)
    alt _resetColumns is true
        Table->>JS: InvokeVoidAsync resetColumn(Id)
        Table->>Table: _resetColumns = false
    end
    alt _resetColDragListener is true
        Table->>JS: InvokeVoidAsync resetColDragListener(Id)
        Table->>Table: _resetColDragListener = false
    end

    JS->>DOM: resetColDragListener(id)
    JS->>DOM: setDraggable(table)
    loop for each draggable header cell
        JS->>DOM: disposeDragColumns(col)
        JS->>DOM: attach dragstart, dragenter, dragover, dragend handlers
    end
Loading

Class diagram for updated Table column visibility and drag reset logic

classDiagram
    class TableGenericTItem {
        - bool _resetColumns
        - bool _resetColDragListener
        - bool AllowDragColumn
        - string ClientTableName
        - string Id
        - IEnumerable~string~ _visibleColumns
        + Task OnToggleColumnVisible(string columnName, bool visible)
        + Task OnAfterRenderAsync(bool firstRender)
        + Task InvokeVoidAsync(string identifier, object arg1)
        + Task InvokeVoidAsync(string identifier, object arg1, object arg2)
    }

    class TableJsModule {
        + void resetColumn(string id)
        + void resetColDragListener(string id)
        + void bindResizeColumn(string id)
        + void setDraggable(TableDom table)
        + void disposeDragColumns(TableHeaderCell col)
    }

    class TableDom {
        + TableHeaderCollection tables
        + TableHeaderCollection dragColumns
    }

    class TableHeaderCell {
        + bool draggable
    }

    class TableHeaderCollection {
        + TableHeaderCell getItem(int index)
        + int length
    }

    TableGenericTItem --> TableJsModule : uses_JSInterop
    TableJsModule --> TableDom : manipulates
    TableDom o-- TableHeaderCollection : contains
    TableHeaderCollection o-- TableHeaderCell : contains
    TableJsModule --> TableHeaderCell : attaches_drag_handlers
Loading

File-Level Changes

Change Details Files
Reset column drag handlers when toggling visible columns so drag-and-drop continues to work after visibility changes.
  • Introduced a _resetColDragListener flag in the Table component that is set when a column becomes visible and drag is allowed.
  • In OnAfterRenderAsync, when _resetColDragListener is true, reset the JS-side drag listeners by invoking the new resetColDragListener JS interop and then clear the flag.
  • Extended OnToggleColumnVisible to set _resetColDragListener for visible columns when AllowDragColumn is enabled.
src/BootstrapBlazor/Components/Table/Table.razor.Checkbox.cs
src/BootstrapBlazor/Components/Table/Table.razor.cs
Provide a JavaScript API to re-bind drag listeners and avoid duplicate drag event handlers when resetting draggability.
  • Added resetColDragListener(id) in the table JS module to call setDraggable for the specified table instance.
  • Updated setDraggable to call disposeDragColumns on each draggable column before attaching new drag event handlers to prevent duplicates.
src/BootstrapBlazor/Components/Table/Table.razor.js
Extend the table drag-column unit test to cover toolbar/column list usage, extra column, and regression around visibility toggling plus drag operations.
  • Enabled ShowToolbar and ShowColumnList in the AllowDragColumn_Ok test setup and added a third Count column to the table.
  • Adjusted the column component indices/attribute order so the test column definitions align with the rendered DOM.
  • Reordered assertions so the drag callback result is validated after DragColumnCallback completes and added assertions on header text order before and after drag operations.
  • Simulated toggling a visible column via the column list dropdown checkbox and then performed an additional DragColumnCallback to verify drag behavior after visibility changes.
test/UnitTest/Components/TableTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#7176 Ensure that when Table has AllowDragColumn=true and column visibility is toggled via the column list (hide then unhide), subsequent column drag-and-drop operations correctly update column order.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • In the AllowDragColumn_Ok test, the new Count column uses builder.AddAttribute(7, "Field", foo.Count); whereas the other columns pass the property name string (e.g., "Address"); consider passing the property name (e.g., "Count") instead of the runtime value to keep Field usage consistent with the component API.
  • The new _resetColDragListener flag and resetColDragListener JS function partially duplicate the existing column reset flow; consider whether this can be integrated with the existing _resetColumns/resetColumn behavior to avoid having multiple separate reset paths that need to stay in sync.
  • In setDraggable, you now call disposeDragColumns(col) inside the forEach loop before registering listeners; if disposeDragColumns was originally intended to work on the table or a collection, double-check that the per-column call pattern aligns with its contract and doesn’t leave any stale listeners on other elements.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the `AllowDragColumn_Ok` test, the new Count column uses `builder.AddAttribute(7, "Field", foo.Count);` whereas the other columns pass the property name string (e.g., "Address"); consider passing the property name (e.g., "Count") instead of the runtime value to keep `Field` usage consistent with the component API.
- The new `_resetColDragListener` flag and `resetColDragListener` JS function partially duplicate the existing column reset flow; consider whether this can be integrated with the existing `_resetColumns`/`resetColumn` behavior to avoid having multiple separate reset paths that need to stay in sync.
- In `setDraggable`, you now call `disposeDragColumns(col)` inside the `forEach` loop before registering listeners; if `disposeDragColumns` was originally intended to work on the table or a collection, double-check that the per-column call pattern aligns with its contract and doesn’t leave any stale listeners on other elements.

## Individual Comments

### Comment 1
<location> `test/UnitTest/Components/TableTest.cs:8524-8532` </location>
<code_context>
             await table.Instance.DragColumnCallback(2, 3);
         });
+
+        // 更改可见列
+        var checkbox = cut.Find(".dropdown-item .form-check-input");
+
+        await cut.InvokeAsync(() => checkbox.Click());
+        await cut.InvokeAsync(() => checkbox.Click());
+
+        await cut.InvokeAsync(async () =>
+        {
+            await table.Instance.DragColumnCallback(3, 4);
+        });
     }
</code_context>

<issue_to_address>
**issue (testing):** Add assertions after the final DragColumnCallback to prove the regression is fixed.

The final `DragColumnCallback(3, 4)` isn’t validated. To ensure this test actually covers #7176, please add assertions that:

* `OnDragColumnEndAsync` is invoked with the expected field name/index after toggling visibility, and/or
* the `<th>` header order remains correct after the visibility toggle and final drag.

You can reuse or add a `name` variable to capture the callback argument and re-query the header cells to verify their order after the last drag operation.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ArgoZhang ArgoZhang merged commit 5dacb93 into main Nov 26, 2025
11 of 12 checks passed
@ArgoZhang ArgoZhang deleted the dev-table branch November 26, 2025 08:30
Copilot finished reviewing on behalf of ArgoZhang November 26, 2025 08:30
@ArgoZhang ArgoZhang restored the dev-table branch November 26, 2025 08:31
@codecov
Copy link

codecov bot commented Nov 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b161e57) to head (985b612).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #7186   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          745       745           
  Lines        32574     32583    +9     
  Branches      4513      4515    +2     
=========================================
+ Hits         32574     32583    +9     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ArgoZhang ArgoZhang deleted the dev-table branch November 26, 2025 08:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where column drag listeners were not properly reset after toggling column visibility in the Table component. The fix ensures that when a column is made visible and drag functionality is enabled, the drag event listeners are reinitialized.

  • Added a new _resetColDragListener flag to track when drag listeners need to be reset
  • Implemented resetColDragListener JavaScript function to reinitialize drag listeners
  • Extended test coverage to verify column toggling works correctly with drag functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/BootstrapBlazor/Components/Table/Table.razor.cs Adds logic to reset column drag listeners in OnAfterRenderAsync when the _resetColDragListener flag is set
src/BootstrapBlazor/Components/Table/Table.razor.Checkbox.cs Sets _resetColDragListener flag when columns are toggled visible and drag is enabled
src/BootstrapBlazor/Components/Table/Table.razor.js Implements new resetColDragListener function and adds cleanup call in setDraggable to dispose old listeners before reattaching
test/UnitTest/Components/TableTest.cs Extends AllowDragColumn_Ok test to verify drag functionality works after toggling column visibility

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


if (_resetColDragListener)
{
_resetColDragListener= false;
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after assignment operator. Should be _resetColDragListener = false; instead of _resetColDragListener= false;

Suggested change
_resetColDragListener= false;
_resetColDragListener = false;

Copilot uses AI. Check for mistakes.
let index = 0
table.dragColumns = [...table.tables[0].querySelectorAll('thead > tr > th')].filter(i => i.draggable)
table.dragColumns.forEach(col => {
disposeDragColumns(col);
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The disposeDragColumns function expects an array of columns (as seen on line 983 where it uses (columns ?? []).forEach(...)), but here it's being called with a single column element col. This should be wrapped in an array: disposeDragColumns([col]); or the function should be refactored to handle both single elements and arrays.

Suggested change
disposeDragColumns(col);
disposeColumnDrag([col]);

Copilot uses AI. Check for mistakes.
builder.CloseComponent();

builder.OpenComponent<TableColumn<Foo, int>>(6);
builder.AddAttribute(7, "Field", foo.Count);
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Field attribute should be set to the field name as a string "Count", not the field value foo.Count. This is inconsistent with the pattern used on lines 8491 and 8496 where field names are passed as strings ("Name", "Address").

Suggested change
builder.AddAttribute(7, "Field", foo.Count);
builder.AddAttribute(7, "Field", "Count");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(Table): 列被隐藏再取消隐藏后,拖动列改变列顺序的功能不正常

3 participants