Skip to content

Table组件新增动态设置固定列的功能,同时固定列支持持久化#8110

Open
Tony-ST0754 wants to merge 5 commits into
mainfrom
feat-table-DynamicFixedColumn
Open

Table组件新增动态设置固定列的功能,同时固定列支持持久化#8110
Tony-ST0754 wants to merge 5 commits into
mainfrom
feat-table-DynamicFixedColumn

Conversation

@Tony-ST0754

@Tony-ST0754 Tony-ST0754 commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

refactor: 可以动态设置固定列,同时固定列支持表格持久化

Link issues

fixes #8094

功能描述 / Problem Description

  • TableColumnState增加Fixed属性,用以持久化用
  • 增加表格实例方法UpdateTableColumnClientStatus,用以在用户自定义前n列固定时,回调生效,并同步持久化

代码示例

BootstrapBlazorApp11.zip

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

Enable dynamic configuration and persistence of fixed table columns and expose an API to update client-side column state.

New Features:

  • Add support for persisting whether a table column is fixed as part of the client column state.
  • Expose a table instance method to update client-side column state after columns are modified externally.

Enhancements:

  • Propagate fixed-column metadata between server and client so fixed status is restored from persisted state.
  • Extend the JavaScript table column state handling to include fixed status and to save updated column states to local storage.

Tests:

  • Add a unit test covering the new table column client state update behavior, including fixed-column persistence.

refactor: 可以动态设置固定列,同时固定列支持表格持久化
@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #8110   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          766       766           
  Lines        34204     34224   +20     
  Branches      4696      4701    +5     
=========================================
+ Hits         34204     34224   +20     
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 Harness.
📢 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.

@bb-auto

bb-auto Bot commented Jun 12, 2026

Copy link
Copy Markdown

Thanks for your PR, @Tony-ST0754. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@bb-auto bb-auto Bot requested a review from ArgoZhang June 12, 2026 08:33
@bb-auto bb-auto Bot added the enhancement New feature or request label Jun 12, 2026
@bb-auto bb-auto Bot added this to the v10.7.0 milestone Jun 12, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds persistence and dynamic updating of fixed columns in the Table component by extending the column state model, wiring fixed state through C# and JS, and introducing a new instance method and JS helper to sync client-side fixed-column changes into persistent storage.

Sequence diagram for updating and persisting fixed table columns

sequenceDiagram
    actor User
    participant Table as Table
    participant JSRuntime as JSRuntime
    participant TableJs as TableJs
    participant LocalStorage as LocalStorage

    User->>Table: UpdateTableColumnClientStatus()
    loop for each item in Columns
        Table->>Table: _tableColumnStates.Find(...).Fixed = item.Fixed
    end
    Table->>Table: StateHasChanged()
    Table->>JSRuntime: InvokeVoidAsync(updateColumnStates, Id)
    JSRuntime->>TableJs: updateColumnStates(id)
    TableJs->>TableJs: getColumnStateObject(table)
    TableJs->>LocalStorage: saveColumnStateToLocalstorage(table, state)
Loading

File-Level Changes

Change Details Files
Persist and restore per-column fixed state alongside width and visibility.
  • Extend TableColumnState with a Fixed flag for client state persistence.
  • When rebuilding columns from persisted state, map the stored Fixed value back onto the corresponding TableColumn instance.
  • Include Fixed in the generated TableColumnState objects so that fixed state is saved and restored together with width and visibility.
src/BootstrapBlazor/Components/Table/Table.razor.cs
src/BootstrapBlazor/Components/Table/TableColumnState.cs
src/BootstrapBlazor/Components/Table/Table.razor.js
Add API surface to update and persist client-side column state (including fixed) after external changes.
  • Introduce UpdateTableColumnClientStatus instance method on Table to push current Columns.Fixed values into the internal _tableColumnStates cache, trigger re-render, and call a JS interop update function.
  • Add a JS-side updateColumnStates helper that recomputes column state and writes it to localStorage, including fixed property.
  • Update getColumnStateObject to capture fixed state for both persisted and resizable columns by reading from column metadata or header CSS classes.
src/BootstrapBlazor/Components/Table/Table.razor.cs
src/BootstrapBlazor/Components/Table/Table.razor.js
Cover new behavior with unit tests exercising client-state loading and UpdateTableColumnClientStatus.
  • Add a unit test that seeds TableColumnClientStatus (including Fixed flags), sets up JS interop for getColumnStates/clearColumnStates, renders a Table with client persistence, and verifies that persisted widths are applied.
  • In the same test, invoke UpdateTableColumnClientStatus on the table instance and assert that the returned status reflects the fixed flag and column count from the seeded state.
test/UnitTest/Components/TableTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#8094 Enable the Table component to dynamically set and update fixed columns at runtime (e.g., allowing users to set the first n columns as fixed after the table is rendered).
#8094 Persist and restore the dynamically configured fixed-column state via the existing client-side column state mechanism so that fixed columns remain consistent across reloads.

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

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:

  • In UpdateTableColumnClientStatus, using Find(... )! assumes every column has a corresponding _tableColumnStates entry; consider handling the case where no match is found to avoid a potential NullReferenceException when columns are dynamically added or renamed.
  • In updateColumnStates (Table.razor.js), Data.get(id) is used without checking for an undefined result; adding a guard (and early return/log) would prevent runtime errors if the table instance has not been registered or has been cleaned up when this function is called.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `UpdateTableColumnClientStatus`, using `Find(... )!` assumes every column has a corresponding `_tableColumnStates` entry; consider handling the case where no match is found to avoid a potential `NullReferenceException` when columns are dynamically added or renamed.
- In `updateColumnStates` (Table.razor.js), `Data.get(id)` is used without checking for an undefined result; adding a guard (and early return/log) would prevent runtime errors if the table instance has not been registered or has been cleaned up when this function is called.

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Components/Table/Table.razor.cs" line_range="1955-1957" />
<code_context>
+        if (Columns.Count != 0)
+        {
+            // 用户在外面变更了列状态后,为避免用户变更状态丢失,须将变更后的状态同步到缓存中
+            foreach (var item in Columns)
+            {
+                _tableColumnStates.Find(x => x.Name == item.GetFieldName())!.Fixed = item.Fixed;
+            }
+            StateHasChanged();
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid assuming `_tableColumnStates` always contains a matching entry for each column.

Using `!` on the `Find` result will cause a runtime exception if `Columns` and `_tableColumnStates` ever diverge (e.g., column added/removed/renamed without a matching state). Consider handling `null` and skipping or logging missing entries to keep this resilient to mismatched state.
</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/Components/Table/Table.razor.cs Outdated
refactor:清除空行,增加缓存列可能为判断
@Tony-ST0754 Tony-ST0754 changed the title Table组件能否新增动态设置固定列的功能 Table组件新增动态设置固定列的功能,同时固定列支持持久化 Jun 12, 2026
refactor: 补充动态固定列时,没有列宽给默认列宽,以防止无列宽时固定后,横向滑动出现错位
refactor: 调整指定固定列时,列宽必须有值

test:补充单元测试
test:清除多余空行

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 support for dynamically updating a Table’s fixed-column configuration at runtime and persisting/restoring that fixed state alongside existing client column state (width/visibility), addressing issue #8094.

Changes:

  • Add Fixed to TableColumnState so fixed-column state can be persisted.
  • Extend the Table JS column-state serialization to include fixed, and add a JS API to update stored column states.
  • Add Table.UpdateTableColumnClientStatus() plus a unit test for the new behavior.

Reviewed changes

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

File Description
test/UnitTest/Components/TableTest.cs Adds a unit test covering UpdateTableColumnClientStatus and fixed-column persistence behavior.
src/BootstrapBlazor/Components/Table/TableColumnState.cs Introduces Fixed property to enable persistence of fixed-column state.
src/BootstrapBlazor/Components/Table/Table.razor.js Persists fixed in column state and adds updateColumnStates export.
src/BootstrapBlazor/Components/Table/Table.razor.cs Restores fixed status from cached state and adds UpdateTableColumnClientStatus() instance method.

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

Comment on lines +939 to +949
export function updateColumnStates(id) {
const el = document.getElementById(id)
if (el === null) {
return
}
let table = Data.get(id)
table.el = el;
Data.set(id, table)
const state = getColumnStateObject(table);
saveColumnStateToLocalstorage(table, state);
}
Comment on lines +9036 to +9041
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.ClientTableName, "test_update");
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.AllowResizing, true);
pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer));
Comment on lines 1436 to 1439
var item = _tableColumnStates[index];
var col = columnMap[item.Name];
col.Fixed = !string.IsNullOrEmpty(item.Name) && stateMap.TryGetValue(item.Name, out var state) ? stateMap[item.Name].Fixed : false;
if (item.Visible)
Comment on lines +1950 to +1970
public async Task<TableColumnClientStatus> UpdateTableColumnClientStatus()
{
if (Columns.Count != 0)
{
// 用户在外面变更了列状态后,为避免用户变更状态丢失,须将变更后的状态同步到缓存中
foreach (var item in Columns)
{
var columnState = _tableColumnStates.Find(x => x.Name == item.GetFieldName());
if (columnState != null)
{
columnState.Fixed = item.Fixed;
columnState.Width = item.Fixed && !item.Width.HasValue ? DefaultFixedColumnWidth : item.Width;
}
}
StateHasChanged();
}
// 如果启用了 ClientTableName 则更新浏览器持久化列状态
await InvokeVoidAsync("updateColumnStates", Id);

return _tableColumnStateCache;
}
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): Table组件能否新增动态设置固定列的功能

3 participants