Skip to content

feat(Table): support Cancel use DataTableDynamicObject model#7917

Merged
ArgoZhang merged 5 commits intomainfrom
feat-dy
Apr 26, 2026
Merged

feat(Table): support Cancel use DataTableDynamicObject model#7917
ArgoZhang merged 5 commits intomainfrom
feat-dy

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Apr 26, 2026

Link issues

fixes #7916

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

Support canceling edits when using the DataTableDynamicObject model by correctly cloning and syncing dynamic table data.

Bug Fixes:

  • Fix editing behavior so dynamic table rows backed by DataTableDynamicContext are cloned instead of edited in place, enabling cancel operations to revert changes.
  • Correct object cloning to use the runtime type of the instance, ensuring fields and read-only properties are copied properly.
  • Ensure dynamic table cell changes are propagated back to the underlying cached data and DataRow when values are edited.

Copilot AI review requested due to automatic review settings April 26, 2026 06:49
@bb-auto bb-auto Bot added the enhancement New feature or request label Apr 26, 2026
@bb-auto bb-auto Bot added this to the v10.5.0 milestone Apr 26, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 26, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts table editing behavior to correctly clone models only for DataTableDynamicContext scenarios, fixes object cloning to use the runtime type, and ensures dynamic table edits update the underlying cached and DataTable-backed data objects.

Sequence diagram for table row edit with DataTableDynamicContext cloning

sequenceDiagram
    actor User
    participant Table
    participant Utility
    participant DynamicContext

    User->>Table: EditAsync()
    alt SelectedRows empty
        Table-->>User: return
    else SelectedRows not empty
        Table->>Table: ToggleLoading(true)
        alt IsTracking true or DynamicContext not DataTableDynamicContext
            Table->>Table: EditModel = SelectedRows[0]
        else DynamicContext is DataTableDynamicContext
            Table->>Utility: Clone(SelectedRows[0])
            Utility-->>Table: clonedModel
            Table->>Table: EditModel = clonedModel
        end
        alt OnEditAsync not null
            Table->>Table: OnEditAsync(EditModel)
        end
        Table->>Table: ToggleLoading(false)
        Table-->>User: open edit UI
    end
Loading

Sequence diagram for dynamic cell value change updating cache and DataTable

sequenceDiagram
    participant DynamicTable as DataTableDynamicContext
    participant DynamicItem as IDynamicObject
    participant Column as ITableColumn
    participant Cache as DataCache
    participant CacheItem
    participant DataRow

    DynamicTable->>DynamicTable: OnCellValueChanged(DynamicItem, Column, val)
    DynamicTable->>Cache: TryGetValue(DynamicItem.DynamicObjectPrimaryKey)
    alt Cache hit
        Cache-->>DynamicTable: CacheItem
        DynamicTable->>CacheItem: Utility.SetPropertyValue(CacheItem, Column.GetFieldName(), val)
        alt CacheItem.Row not null
            DynamicTable->>DataRow: update cell value
        end
    else Cache miss
        Cache-->>DynamicTable: not found
    end
Loading

Updated class diagram for Table editing and dynamic data handling

classDiagram
    class TableComponent {
        bool IsTracking
        object? DynamicContext
        IList~object~ SelectedRows
        object? EditModel
        Func~object,Task~? OnEditAsync
        Task EditAsync()
        Task ToggleLoading(bool isLoading)
    }

    class DataTableDynamicContext {
        Dictionary~string,DataCacheItem~ _dataCache
        Task OnCellValueChanged(IDynamicObject item, ITableColumn column, object val)
    }

    class DataCacheItem {
        string DynamicObjectPrimaryKey
        object CacheObject
        DataRow? Row
    }

    class IDynamicObject {
        string DynamicObjectPrimaryKey
    }

    class ITableColumn {
        string GetFieldName()
    }

    class ObjectExtensions {
        static void Clone~TModel~(TModel source, TModel item)
    }

    class Utility {
        static TModel Clone~TModel~(TModel source)
        static void SetPropertyValue~TTarget,TValue~(TTarget target, string propertyName, TValue value)
    }

    class DataRow {
        object this[string columnName]
    }

    TableComponent --> DataTableDynamicContext : uses DynamicContext
    TableComponent --> Utility : calls Clone
    TableComponent --> ObjectExtensions : uses Clone extension
    TableComponent --> IDynamicObject : SelectedRows items may implement

    DataTableDynamicContext --> IDynamicObject : parameter item
    DataTableDynamicContext --> ITableColumn : parameter column
    DataTableDynamicContext --> DataCacheItem : uses _dataCache
    DataTableDynamicContext --> Utility : calls SetPropertyValue

    DataCacheItem --> DataRow : contains Row

    ObjectExtensions ..> Utility : may delegate to Clone

    IDynamicObject <|.. DataCacheItem
    IDynamicObject <|.. DynamicRuntimeItem

    class DynamicRuntimeItem {
        string DynamicObjectPrimaryKey
    }
Loading

File-Level Changes

Change Details Files
Refine EditModel assignment so that rows are cloned only when using DataTableDynamicContext, otherwise editing operates directly on the selected row instance.
  • Wrap EditModel assignment in EditAsync with a more specific condition that checks whether DynamicContext is a DataTableDynamicContext instance
  • Keep direct reference editing when tracking is enabled or when not using DataTableDynamicContext
  • Retain existing loading toggle and OnEditAsync invocation flow
src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs
Fix generic Clone extension method to use the runtime type of the target object when copying fields and properties.
  • Change Clone to get the type from the target instance via item.GetType() instead of typeof(TModel)
  • Preserve support for cloning fields and read-only properties on the runtime type
src/BootstrapBlazor/Extensions/ObjectExtensions.cs
Ensure dynamic table cell edits propagate to the internal dynamic object cache and the backing DataTable row.
  • When a cell value changes, update the cached dynamic item via Utility.SetPropertyValue using the column field name and new value
  • Keep existing logic that updates the original DataRow when present
src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#7916 Update Table editing logic so that when using DataTableDynamicContext/DataTableDynamicObject models, the edit operation uses a cloned model instance (rather than the original) to allow proper Cancel behavior.
#7916 Fix/adjust the object cloning helper so that it correctly clones instances of dynamic or runtime-generated model types used by the table.
#7916 Ensure DataTableDynamicContext updates the underlying dynamic data object when a cell value changes, keeping the dynamic model and the DataTable in sync.

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 ternary in EditAsync combining IsTracking and DynamicContext is not DataTableDynamicContext is a bit hard to scan; consider extracting this into a clearly named local like bool shouldCloneForDynamicContext to make the edit behavior easier to understand and maintain.
  • In OnCellValueChanged, Utility.SetPropertyValue<object, object?> assumes the target property exists and is settable; consider handling or short‑circuiting when the property is missing/readonly to avoid runtime exceptions when dynamic column definitions and cached items diverge.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The ternary in `EditAsync` combining `IsTracking` and `DynamicContext is not DataTableDynamicContext` is a bit hard to scan; consider extracting this into a clearly named local like `bool shouldCloneForDynamicContext` to make the edit behavior easier to understand and maintain.
- In `OnCellValueChanged`, `Utility.SetPropertyValue<object, object?>` assumes the target property exists and is settable; consider handling or short‑circuiting when the property is missing/readonly to avoid runtime exceptions when dynamic column definitions and cached items diverge.

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs" line_range="655-656" />
<code_context>
                 await ToggleLoading(true);
-                EditModel = (IsTracking || DynamicContext != null) ? SelectedRows[0] : Utility.Clone(SelectedRows[0]);
+
+                // 复制对象给编辑模型
+                EditModel = (IsTracking || DynamicContext is not DataTableDynamicContext)
+                    ? SelectedRows[0]
+                    : Utility.Clone(SelectedRows[0]);
</code_context>
<issue_to_address>
**issue (bug_risk):** Double-check the behavior change when DynamicContext is null compared to the previous condition.

With the new condition `(IsTracking || DynamicContext is not DataTableDynamicContext)`, the `DynamicContext == null` case now returns `SelectedRows[0]` instead of a clone. This changes previous copy-vs-reference behavior and may introduce in-place mutations where a safe copy was used before. Consider explicitly handling `DynamicContext == null` or clarifying in code/comments that this behavioral change is intentional.
</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.Toolbar.cs Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 26, 2026

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 99.99%. Comparing base (d22ede2) to head (422ea11).

Files with missing lines Patch % Lines
...trapBlazor/Components/Table/Table.razor.Toolbar.cs 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              main    #7917      +/-   ##
===========================================
- Coverage   100.00%   99.99%   -0.01%     
===========================================
  Files          765      765              
  Lines        34325    34332       +7     
  Branches      4710     4711       +1     
===========================================
+ Hits         34325    34331       +6     
- Partials         0        1       +1     
Flag Coverage Δ
BB 99.99% <88.88%> (?)

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.

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 updates table editing behavior and DataTable-backed dynamic models to better support “Cancel” scenarios when using DataTableDynamicObject-based rows (fixes #7916).

Changes:

  • Fix cloning to use the runtime type (item.GetType()) so Utility.Clone works correctly when TModel is an interface/base type (e.g., IDynamicObject).
  • Ensure DataTableDynamicContext keeps the cached dynamic object’s properties in sync when a cell value changes.
  • Adjust Table.EditAsync cloning logic to special-case DataTableDynamicContext (intended to support cancel without mutating live rows).

Reviewed changes

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

File Description
src/BootstrapBlazor/Extensions/ObjectExtensions.cs Clone now reflects over the runtime type, enabling correct cloning for dynamic/interface-typed models.
src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs Updates cached dynamic object properties on cell edit so UI/model stays consistent with the underlying DataRow.
src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs Changes how EditModel is chosen (clone vs. live instance) for editing scenarios, targeting DataTable dynamic context cancel behavior.

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

Comment thread src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs Outdated
Comment thread src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs Outdated
Comment thread src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs
Comment thread src/BootstrapBlazor/Extensions/ObjectExtensions.cs
@ArgoZhang ArgoZhang merged commit bfbafc6 into main Apr 26, 2026
3 of 4 checks passed
@ArgoZhang ArgoZhang deleted the feat-dy branch April 26, 2026 09:42
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): support Cancel use DataTableDynamicObject model

2 participants