Skip to content

fix(Table): parameter maybe not work on TableColumns template#7966

Merged
ArgoZhang merged 17 commits into
mainfrom
fix-table-AutoGenerate-Ignore
May 11, 2026
Merged

fix(Table): parameter maybe not work on TableColumns template#7966
ArgoZhang merged 17 commits into
mainfrom
fix-table-AutoGenerate-Ignore

Conversation

@Tony-ST0754

@Tony-ST0754 Tony-ST0754 commented May 11, 2026

Copy link
Copy Markdown
Collaborator

修正表格动态生成列时,如果页面中手动指定列属性 Ignore为true时,操作编辑等操作时,仍将手动指定Ignore为true的列显示

PS:尴尬,单元测试少了 ColumnVisibleItem 这个类找不到,看了下拉下来的源码文件,少了ColumnVisibleItem.cs这个文件

Link issues

fixes #7965

问题描述 / Problem Description

修正近期Bate版表格组件中,动态生成列时且页面有手动指定的列属性Ignore=true时,页面初次加载能正确显示,但当用户操作编辑等操作时,原先手动指定列属性 Ignore=true 的列会被组件忽略并显示出来

复现示例 / Steps To Reproduce

20260511_102042.mp4

根本原因 / Root Cause

public static IEnumerable<ITableColumn> GetTableColumns(Type type, IEnumerable<ITableColumn>? source = null, Func<IEnumerable<ITableColumn>, IEnumerable<ITableColumn>>? defaultOrderCallback = null)
{
    var columns = new List<ITableColumn>();
   if (source != null)
   {
       columns.AddRange(source); // 这里页面初次加载时能正确将页面上手动写有 `Ignore=true`传过来
   }
   /*中间代码省略*/
  foreach (var prop in props)
  {
          /*中间代码省略*/
         // 替换属性 手写优先
         var col = columns.Find(c => c.GetFieldName() == tc.GetFieldName());
        if (col != null)
        {
             // 1、第二次渲染时(编辑等操作)columns 该参数已没有手动在页面上设置有`Ignore=true`的属性列
             tc.CopyValue(col);
             columns.Remove(col);
        }

       if (!tc.GetIgnore())
       {
           // 2、第二次渲染时没有复制到原来的手写属性(没传进来,也是第一次渲染调用本方法时,只返回了不含 `Ignore=true` 的列),因此,造成二次渲染时不正确
           cols.Add(tc);
       }
  }
}

解决方案 / Solution

20260511_102042.mp4

根本原因 / Root Cause

public static IEnumerable<ITableColumn> GetTableColumns(Type type, IEnumerable<ITableColumn>? source = null, Func<IEnumerable<ITableColumn>, IEnumerable<ITableColumn>>? defaultOrderCallback = null)
{
    var columns = new List<ITableColumn>();
   if (source != null)
   {
       columns.AddRange(source); // 这里页面初次加载时能正确将页面上手动写有 `Ignore=true`传过来
   }
   /*中间代码省略*/
  foreach (var prop in props)
  {
          /*中间代码省略*/
         // 替换属性 手写优先
         var col = columns.Find(c => c.GetFieldName() == tc.GetFieldName());
        if (col != null)
        {
             // 1、移除复制手写属性后,将传进来的对应列删除的功能
             tc.CopyValue(col);             
        }

       if (!tc.GetIgnore())
       {
           cols.Add(tc);
       }

      if (columns.Count > 1)
      {
            /*
 * 1、动态生成列时,如果有手写 Ignore 属性的列,并没有添加到 cols 中,这样在编辑或其他操作时,在页面上手动 Ignore 属性的列会被删除
 * 2、等用户操作编辑等动作时,再次渲染传进来的 source 列集合中,并没有手写的含 Ignore 属性的列,这直接导致了二次渲染时,将不应该显示的列给显示了
 * 3、这里直接将即将返回的列集合,与二次渲染的列集合进行比较,将不存在于二次渲染列集合中的列给排除掉,以保证渲染表格时列集合正确
 */
           cols.RemoveAll(x => !columns.Contains(x));
      }
  }
}

修正表格动态生成列时,如果页面中手动指定列属性 Ignore为true时,操作编辑等操作时,仍将手动指定Ignore为true的列显示
@bb-auto

bb-auto Bot commented May 11, 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.

@sourcery-ai

sourcery-ai Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts table column generation to correctly honor columns marked Ignore=true during dynamic rendering, especially on subsequent renders (e.g., edit operations), by preserving manual column definitions and filtering out columns that should not be displayed.

Flow diagram for updated GetTableColumns behavior with Ignore handling

flowchart TD
    start[Start GetTableColumns] --> initCols[Initialize empty list cols]
    initCols --> initColumns[Initialize list columns]

    initColumns --> hasSource{source is not null?}
    hasSource -->|yes| addSource[Add all source columns to columns]
    hasSource -->|no| propsLoopStart
    addSource --> propsLoopStart

    propsLoopStart[Iterate over props] --> nextProp{More props?}
    nextProp -->|no| afterProps
    nextProp -->|yes| buildTc[Build tc from prop metadata]

    buildTc --> findCol[Find col in columns with same field name as tc]
    findCol --> colFound{col found?}

    colFound -->|yes| copyValues[CopyValue from col to tc]
    colFound -->|no| checkIgnore

    copyValues --> checkIgnore[Check tc.GetIgnore]

    checkIgnore --> isIgnored{tc.GetIgnore is true?}
    isIgnored -->|yes| skipAdd[Do not add tc to cols]
    isIgnored -->|no| addTc[Add tc to cols]

    skipAdd --> propsLoopStart
    addTc --> propsLoopStart

    afterProps[After processing all props] --> checkColumnsCount{columns.Count > 1?}

    checkColumnsCount -->|no| applyOrder
    checkColumnsCount -->|yes| filterCols[Remove from cols any item not in columns]

    filterCols --> applyOrder[Apply defaultOrderCallback if provided]

    applyOrder --> returnCols[Return resulting cols]

    returnCols --> endNode[End GetTableColumns]
Loading

File-Level Changes

Change Details Files
Preserve manually defined table columns and filter the final column list to keep it consistent with the provided source, ensuring Ignore=true columns stay hidden across renders.
  • Stopped removing matched source columns from the working columns list after copying values into generated columns.
  • Replaced the previous behavior of appending remaining source columns with a filtering step that removes any generated columns not present in the source when multiple source columns are provided.
  • Adjusted the condition from checking for any remaining source columns to requiring more than one, and commented out the old logic that appended all remaining columns.
src/BootstrapBlazor/Utils/Utility.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#7965 Ensure that in the beta table component, when columns are dynamically generated and the page manually specifies a column with Ignore = true, this Ignore setting is preserved on subsequent operations (such as edit) so that the column remains ignored/hidden instead of appearing.

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

@bb-auto bb-auto Bot added the bug Something isn't working label May 11, 2026
@bb-auto bb-auto Bot requested a review from ArgoZhang May 11, 2026 06:07
@bb-auto bb-auto Bot added this to the v10.6.0 milestone May 11, 2026

@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:

  • The new cols.RemoveAll(x => !columns.Contains(x)) logic relies on reference equality for ITableColumn; if these are reconstructed objects between calls, you may want to compare by field name or another stable key instead of Contains on the interface instances.
  • Changing the condition from columns.Count > 0 to columns.Count > 1 looks arbitrary and may skip the filter logic when exactly one column is passed; consider clarifying the intent or reverting to > 0 if not strictly required.
  • Rather than leaving the old behavior commented out and adding a long inline comment block, consider refactoring the post-processing into a small helper with a clear name and a concise summary comment to keep this method easier to follow.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `cols.RemoveAll(x => !columns.Contains(x))` logic relies on reference equality for `ITableColumn`; if these are reconstructed objects between calls, you may want to compare by field name or another stable key instead of `Contains` on the interface instances.
- Changing the condition from `columns.Count > 0` to `columns.Count > 1` looks arbitrary and may skip the filter logic when exactly one column is passed; consider clarifying the intent or reverting to `> 0` if not strictly required.
- Rather than leaving the old behavior commented out and adding a long inline comment block, consider refactoring the post-processing into a small helper with a clear name and a concise summary comment to keep this method easier to follow.

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Utils/Utility.cs" line_range="566" />
<code_context>
+             * 2、等用户操作编辑等动作时,再次渲染传进来的 source 列集合中,并没有手写的含 Ignore 属性的列,这直接导致了二次渲染时,将不应该显示的列给显示了
+             * 3、这里直接将即将返回的列集合,与二次渲染的列集合进行比较,将不存在于二次渲染列集合中的列给排除掉,以保证渲染表格时列集合正确
+             */
+            cols.RemoveAll(x => !columns.Contains(x));
         }
+
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider using a more explicit equality strategy and/or a `HashSet` when filtering `cols` by `columns`.

`columns.Contains(x)` depends on the default equality for `ITableColumn`, which may be reference-based. If column identity is defined by a property (e.g., `Field`/`PropertyName`), this can filter incorrectly when distinct instances represent the same logical column. Also, repeated `Contains` on a list is O(n²) for large `cols`/`columns`. Consider building a `HashSet` from the column key for lookups, or providing an `IEqualityComparer<ITableColumn>` that matches the intended identity.

Suggested implementation:

```csharp
            /*
             * 1、动态生成列时,如果有手写 Ignore 属性的列,并没有添加到 cols 中,这样在编辑或其他操作时,在页面上手动 Ignore 属性的列会被删除
             * 2、等用户操作编辑等动作时,再次渲染传进来的 source 列集合中,并没有手写的含 Ignore 属性的列,这直接导致了二次渲染时,将不应该显示的列给显示了
             * 3、这里直接将即将返回的列集合,与二次渲染的列集合进行比较,将不存在于二次渲染列集合中的列给排除掉,以保证渲染表格时列集合正确
             *
             * 使用列标识(例如 PropertyName)构建 HashSet,避免依赖引用相等以及 O(n²) 的 List.Contains 调用。
             */
            var columnKeySet = new HashSet<string>(
                columns
                    .Where(c => c != null)
                    .Select(c => c.PropertyName)
                    .Where(name => !string.IsNullOrEmpty(name)),
                StringComparer.Ordinal
            );

            cols.RemoveAll(x =>
                x == null ||
                string.IsNullOrEmpty(x.PropertyName) ||
                !columnKeySet.Contains(x.PropertyName)
            );
        }

```

1. This edit assumes `ITableColumn` (or whatever the element type of `columns`/`cols` is) exposes a stable identity via a `PropertyName` property. If your column identity is represented differently (e.g., `Field`, `FieldName`, `ColumnName`, etc.), replace `PropertyName` with that property.
2. If there is an existing utility or comparer in your codebase that defines column equality (e.g., `ITableColumnComparer` or a specific `IEqualityComparer<ITableColumn>`), you can instead build the `HashSet` on that key or use `new HashSet<ITableColumn>(columns, yourComparer)` and update the `RemoveAll` predicate accordingly.
3. If `columns` and `cols` are strongly typed as a more specific type (e.g., `TableColumn`), consider changing the `HashSet<string>`/`PropertyName` access to whatever the canonical key is used elsewhere in `Utility.cs` or in the table component, to keep identity handling consistent.
</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/Utils/Utility.cs Outdated
@codecov

codecov Bot commented May 11, 2026

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #7966   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          765       765           
  Lines        34112     34124   +12     
  Branches      4683      4685    +2     
=========================================
+ Hits         34112     34124   +12     
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.

@ArgoZhang ArgoZhang changed the title 修正表格动态生成列时在页面中手动指定Ignore为true时的bug fix(Table): parameter maybe not work on TableColumns template May 11, 2026
@ArgoZhang ArgoZhang merged commit 0297ba0 into main May 11, 2026
2 checks passed
@ArgoZhang ArgoZhang deleted the fix-table-AutoGenerate-Ignore branch May 11, 2026 09:15
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): parameter Ignore not work

2 participants