Skip to content

feat(Table): add ClearTableColumnClientStatus instance method#8005

Merged
ArgoZhang merged 10 commits into
mainfrom
feat-table
May 17, 2026
Merged

feat(Table): add ClearTableColumnClientStatus instance method#8005
ArgoZhang merged 10 commits into
mainfrom
feat-table

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented May 17, 2026

Link issues

fixes #8004

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

Add support for clearing persisted table column client status and propagating that reset through the table component.

New Features:

  • Introduce a ClearTableColumnClientStatus instance method on the Table component to clear cached and browser-persisted column state for a named table.
  • Provide a JavaScript helper to remove stored table column state from localStorage based on the table name.

Enhancements:

  • Adjust table column state handling so display names are reapplied when restoring visible columns from cached state and simplify row key retrieval logic.
  • Remove unnecessary key usage on generated elements in the table layout and refine how column display names are managed when creating column state objects.

Tests:

  • Add a unit test verifying that ClearTableColumnClientStatus clears client-side column state, invokes the corresponding JS interop, and restores default widths.

Copilot AI review requested due to automatic review settings May 17, 2026 03:03
@bb-auto bb-auto Bot added the enhancement New feature or request label May 17, 2026
@bb-auto bb-auto Bot added this to the v10.6.0 milestone May 17, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 17, 2026

Reviewer's Guide

Adds an instance method on Table to clear client-side column state, wires it to a new JS interop function that removes the table’s persisted state from localStorage, adjusts how column display names and widths are restored from cached state, and covers the behavior with a new unit test while doing minor cleanup in table rendering and samples.

Sequence diagram for clearing table column client status

sequenceDiagram
    actor User
    participant Table
    participant JSRuntime
    participant Browser

    User->>Table: ClearTableColumnClientStatus()
    alt ClientTableName not null or empty
        Table->>JSRuntime: InvokeVoidAsync(clearColumnStates, ClientTableName)
        JSRuntime->>Browser: clearColumnStates(tableName)
        Browser->>Browser: localStorage.removeItem(bb-table-{tableName})
    end
    Table->>Table: _tableColumnStateCache.Clear()
    Table->>Table: _resetColumns = true
    Table->>Table: _invoke = true
    Table->>Table: StateHasChanged()
Loading

File-Level Changes

elements in Table.razor to avoid using the column object itself as a key
  • Simplify GetKeyByITem to drop obsolete commented row-key path
  • Change Details Files
    Add Table instance API to clear client-side column state and reset columns
    • Introduce public async ClearTableColumnClientStatus instance method on Table that, when ClientTableName is set, invokes JS interop to clear persisted state and then clears the internal _tableColumnStateCache
    • After clearing state, force a columns reset and re-render by toggling _resetColumns and _invoke and calling StateHasChanged
    • Document the new API with XML comments (Chinese/English) including version tag v10.6.1
    src/BootstrapBlazor/Components/Table/Table.razor.cs
    Implement JavaScript support for clearing table column state from localStorage
    • Add clearColumnStates(tableName) export in Table.razor.js that removes the bb-table-{tableName} key from localStorage
    • Keep existing width-specific localStorage handling intact while separating full column state removal into its own function
    src/BootstrapBlazor/Components/Table/Table.razor.js
    Adjust column state handling and rendering behavior
    • Stop setting DisplayName on TableColumnState during creation and instead populate DisplayName only when restoring from cached state so it reflects current column metadata
    • Remove the @key directive from
    src/BootstrapBlazor/Components/Table/Table.razor.cs
    src/BootstrapBlazor/Components/Table/Table.razor
    src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs
    Cover ClearTableColumnClientStatus behavior with unit tests and sample wiring
    • Add ClearTableColumnClientStatus_Ok unit test that sets up JSInterop for getColumnStates and clearColumnStates, verifies initial width comes from client state, calls ClearTableColumnClientStatus, verifies JS was invoked, and ensures column width is reset to the configured value
    • Comment in sample TablesColumnDrag.razor showing how a Reset button could call Table.ClearTableColumnClientStatus via a component ref, but keep it disabled/commented for now
    test/UnitTest/Components/TableTest.cs
    src/BootstrapBlazor.Server/Components/Samples/Table/TablesColumnDrag.razor
    Introduce extension helper to clear TableColumnClientStatus in code
    • Add TableColumnClientStatusExtensions static class providing a Clear() helper that zeroes TableWidth and clears Columns on a TableColumnClientStatus instance
    src/BootstrapBlazor/Extensions/TableColumnClientStatusExtensions.cs

    Assessment against linked issues

    Issue Objective Addressed Explanation
    #8004 Add a ClearTableColumnClientStatus instance method on the Table component that clears client-side persisted column state and resets the table columns accordingly.
    #8004 Add supporting implementation (e.g., JS interop and tests) so that ClearTableColumnClientStatus actually clears browser-persisted state and is verified by automated tests.

    Possibly linked issues


    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
    Copy Markdown
    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 - I've found 1 issue, and left some high level feedback:

    • The new TableColumnClientStatusExtensions class does not use valid C# extension syntax (extension(TableColumnClientStatus status)); this should be rewritten as a standard static extension method (e.g., public static void Clear(this TableColumnClientStatus status)) or removed if not needed.
    • The new clearTableColumnClientStatus instance method only calls the JS clearColumnStates function, which removes bb-table-{tableName} but not the existing column width key handled by removeColumnWidthState; consider also clearing the width state (or reusing removeColumnWidthState) so the client state is fully reset.
    Prompt for AI Agents
    Please address the comments from this code review:
    
    ## Overall Comments
    - The new TableColumnClientStatusExtensions class does not use valid C# extension syntax (`extension(TableColumnClientStatus status)`); this should be rewritten as a standard static extension method (e.g., `public static void Clear(this TableColumnClientStatus status)`) or removed if not needed.
    - The new clearTableColumnClientStatus instance method only calls the JS `clearColumnStates` function, which removes `bb-table-{tableName}` but not the existing column width key handled by `removeColumnWidthState`; consider also clearing the width state (or reusing `removeColumnWidthState`) so the client state is fully reset.
    
    ## Individual Comments
    
    ### Comment 1
    <location path="src/BootstrapBlazor/Extensions/TableColumnClientStatusExtensions.cs" line_range="12-14" />
    <code_context>
    +/// <para lang="zh">TableColumnClientStatus 扩展方法</para>
    +/// <para lang="en">TableColumnClientStatus extensions</para>
    +/// </summary>
    +public static class TableColumnClientStatusExtensions
    +{
    +    extension(TableColumnClientStatus status)
    +    {
    +        /// <summary>
    </code_context>
    <issue_to_address>
    **issue (bug_risk):** The extension method syntax is invalid and will not compile.
    
    In C#, extension methods must be `public static` methods on a `public static` class, and the first parameter must be marked with `this`. The `extension(TableColumnClientStatus status)` block and nested `public void Clear()` are not valid syntax. Refactor to a proper extension method, e.g.:
    
    ```csharp
    public static class TableColumnClientStatusExtensions
    {
        /// <summary>
        /// <para lang="zh">清除方法</para>
        /// <para lang="en">Clear method</para>
        /// </summary>
        public static void Clear(this TableColumnClientStatus status)
        {
            status.Columns.Clear();
            status.TableWidth = 0;
        }
    }
    ```
    </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.

    Comment thread src/BootstrapBlazor/Extensions/TableColumnClientStatusExtensions.cs
    @ArgoZhang ArgoZhang merged commit 64b5c86 into main May 17, 2026
    4 checks passed
    @ArgoZhang ArgoZhang deleted the feat-table branch May 17, 2026 03:06
    Copy link
    Copy Markdown
    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 adds an instance API to the Table<TItem> component to clear persisted client-side column state (localStorage) and refresh the table’s column layout, addressing #8004.

    Changes:

    • Added Table<TItem>.ClearTableColumnClientStatus() to clear persisted column state and reset the table columns.
    • Added JS interop support (clearColumnStates) to remove the table column-state entry from localStorage.
    • Adjusted column-state/display-name handling and added a unit test covering the new instance method.

    Reviewed changes

    Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

    Show a summary per file
    File Description
    test/UnitTest/Components/TableTest.cs Adds a unit test validating that clearing client column state restores the configured column width.
    src/BootstrapBlazor/Extensions/TableColumnClientStatusExtensions.cs Introduces a TableColumnClientStatus.Clear() helper used when clearing cached state.
    src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs Updates display-name assignment during visible-columns cache rebuild.
    src/BootstrapBlazor/Components/Table/Table.razor.js Adds clearColumnStates(tableName) JS export to delete persisted column state from localStorage.
    src/BootstrapBlazor/Components/Table/Table.razor.cs Adds ClearTableColumnClientStatus() and adjusts column-state construction (notably DisplayName).
    src/BootstrapBlazor/Components/Table/Table.razor Removes @key from the <col> loop in the <colgroup>.
    src/BootstrapBlazor.Server/Components/Samples/Table/TablesColumnDrag.razor Adds (commented-out) sample code intended to demonstrate the new API.
    Comments suppressed due to low confidence (2)

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

    • CreateTableColumnState no longer initializes TableColumnState.DisplayName, but the column list UI renders @item.DisplayName (see Table.razor RenderColumnListItem). This makes display text null/blank for columns (especially those currently not visible) unless it’s set elsewhere. Consider setting DisplayName = col.GetDisplayName() here (or ensuring it’s populated for all _tableColumnStates, not only visible ones).

    This issue also appears on line 1886 of the same file.

    
        private TableColumnState CreateTableColumnState(ITableColumn col) => new TableColumnState()
        {
            Name = col.GetFieldName(),
            Width = col.Width,
            Visible = col.GetVisible(_screenSize)
        };
    

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

    • ClearTableColumnClientStatus clears _tableColumnStateCache, but it doesn’t reset the current Columns model back to its original widths/visibility. For auto-generated/dynamic columns (InternalTableColumn) the Width/Visible values can remain mutated from the previously applied client state, so clearing may not actually restore defaults. Consider rebuilding the column definitions (e.g., re-run the column build pipeline) or keeping an “initial column state” snapshot to restore Columns from when clearing client status.
        public async Task ClearTableColumnClientStatus()
        {
            if (!string.IsNullOrEmpty(ClientTableName))
            {
                // 如果启用了 ClientTableName 则清除浏览器持久化列状态
                await InvokeVoidAsync("clearColumnStates", ClientTableName);
            }
    
            // 清除缓存的列状态
            _tableColumnStateCache.Clear();
    
            _resetColumns = true;
            _invoke = true;
            StateHasChanged();
        }
    

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


    export function clearColumnStates(tableName) {
    const columnStateKey = `bb-table-${tableName}`;
    localStorage.removeItem(columnStateKey);
    Comment on lines 11 to +38
    @@ -26,3 +28,12 @@
    </Table>
    <ConsoleLogger @ref="Logger" />
    </DemoBlock>

    @code {
    // private Table<Foo> Table { get; set; }

    // private async Task Reset()
    // {
    // await Table.ClearTableColumnClientStatus();
    // }
    @codecov
    Copy link
    Copy Markdown

    codecov Bot commented May 17, 2026

    Codecov Report

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

    Additional details and impacted files
    @@            Coverage Diff            @@
    ##              main     #8005   +/-   ##
    =========================================
      Coverage   100.00%   100.00%           
    =========================================
      Files          765       766    +1     
      Lines        34118     34131   +13     
      Branches      4691      4692    +1     
    =========================================
    + Hits         34118     34131   +13     
    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.
    • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    enhancement New feature or request

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    feat(Table): add ClearTableColumnClientStatus instance method

    2 participants