Skip to content

Conversation

@AApuci
Copy link
Collaborator

@AApuci AApuci commented Oct 11, 2025

Updated autoFitColumnWidth to calculate the header cell width using the element directly instead of querying for a child span. This ensures more accurate column width fitting when including the header.

Link issues

fixes #6879

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

Bug Fixes:

  • Include header cell padding in auto-fit column width calculation to ensure accurate header sizing

Updated autoFitColumnWidth to calculate the header cell width using the <th> element directly instead of querying for a child span. This ensures more accurate column width fitting when including the header.
@bb-auto
Copy link

bb-auto bot commented Oct 11, 2025

Thanks for your PR, @AApuci. 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 October 11, 2025 10:19
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 11, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

autoFitColumnWidth now accounts for header cell padding by computing styles directly on the element, leading to more accurate column width measurements.

Class diagram for updated autoFitColumnWidth logic

classDiagram
class Table {
  options: Object
}
class Column {
}
class autoFitColumnWidth {
  +async (table, col)
}
Table <|-- autoFitColumnWidth
Column <|-- autoFitColumnWidth

class "th (header cell)" {
  +getComputedStyle()
  +querySelector('.table-cell')
}
autoFitColumnWidth --> "th (header cell)" : uses
"th (header cell)" --> "span.table-cell" : queries
Loading

Flow diagram for header cell width calculation in autoFitColumnWidth

flowchart TD
A["Start autoFitColumnWidth(table, col)"] --> B["Check fitColumnWidthIncludeHeader option"]
B -->|true| C["Find closest th element to col"]
C --> D["Query .table-cell span inside th"]
D --> E["Calculate cell width using calcCellWidth(span)"]
C --> F["Get computed style of th"]
F --> G["Extract padding-left and padding-right"]
E --> H["Add margin (padding) to maxWidth"]
G --> H
H --> I["Continue with autoFitColumnWidth logic"]
Loading

File-Level Changes

Change Details Files
Include header cell padding in auto-fit column width calculation
  • Retrieve computed style of the
element
  • Parse padding-left and padding-right values
  • Add total horizontal padding margin to the maxWidth calculation
  • src/BootstrapBlazor/Components/Table/Table.razor.js

    Assessment against linked issues

    Issue Objective Addressed Explanation
    #6879 Include header cell padding when calculating table header cell width.

    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[bot]
    sourcery-ai bot previously approved these changes Oct 11, 2025
    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:

    • Add a null check for th before calling calcCellWidth to avoid potential runtime errors on unexpected DOM structures.
    • Confirm that calcCellWidth accounts for padding, borders, and other CSS on the <th> itself to ensure the measurement is accurate.
    • Evaluate performance implications of measuring the entire <th> element instead of the inner span, especially for tables with many header cells.
    Prompt for AI Agents
    Please address the comments from this code review:
    
    ## Overall Comments
    - Add a null check for `th` before calling `calcCellWidth` to avoid potential runtime errors on unexpected DOM structures.
    - Confirm that `calcCellWidth` accounts for padding, borders, and other CSS on the `<th>` itself to ensure the measurement is accurate.
    - Evaluate performance implications of measuring the entire `<th>` element instead of the inner span, especially for tables with many header cells.

    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.

    @codecov
    Copy link

    codecov bot commented Oct 11, 2025

    Codecov Report

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

    Additional details and impacted files
    @@            Coverage Diff            @@
    ##              main     #6876   +/-   ##
    =========================================
      Coverage   100.00%   100.00%           
    =========================================
      Files          739       739           
      Lines        31765     31765           
      Branches      4466      4466           
    =========================================
      Hits         31765     31765           
    Flag Coverage Δ
    BB 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.

    @AApuci
    Copy link
    Collaborator Author

    AApuci commented Oct 11, 2025

    <th> 样式中的padding占了一定宽度,对应 var(--bb-table-td-padding-y) var(--bb-table-td-padding-x);
    如果只是计算 .table-cell 占用宽度会导致结果偏小一点。

    @bb-auto bb-auto bot added the bug Something isn't working label Oct 12, 2025
    @bb-auto bb-auto bot added this to the 9.11.0 milestone Oct 12, 2025
    @ArgoZhang
    Copy link
    Member

    @sourcery-ai review

    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 and they look great!

    Prompt for AI Agents
    Please address the comments from this code review:
    
    ## Individual Comments
    
    ### Comment 1
    <location> `src/BootstrapBlazor/Components/Table/Table.razor.js:748-750` </location>
    <code_context>
             const span = th.querySelector('.table-cell');
    -        maxWidth = Math.max(maxWidth, calcCellWidth(span));
    +        const thStyle = getComputedStyle(th);
    +        const margin = parseFloat(thStyle.getPropertyValue('padding-left')) + parseFloat(thStyle.getPropertyValue('padding-right'))
    +        maxWidth = Math.max(maxWidth, calcCellWidth(span)) + margin;
         }
    </code_context>
    
    <issue_to_address>
    **suggestion:** Handle NaN results from parseFloat for padding values.
    
    Default to 0 if parseFloat returns NaN to ensure margin calculation is reliable.
    
    ```suggestion
            const thStyle = getComputedStyle(th);
            const paddingLeft = parseFloat(thStyle.getPropertyValue('padding-left'));
            const paddingRight = parseFloat(thStyle.getPropertyValue('padding-right'));
            const safePaddingLeft = isNaN(paddingLeft) ? 0 : paddingLeft;
            const safePaddingRight = isNaN(paddingRight) ? 0 : paddingRight;
            const margin = safePaddingLeft + safePaddingRight;
            maxWidth = Math.max(maxWidth, calcCellWidth(span)) + margin;
    ```
    </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
    Copy link
    Member

    @sourcery-ai summary

    @ArgoZhang ArgoZhang changed the title Fix column width calculation to use header cell fix(Table): include header cell padding in auto-fit column width calculation to ensure accurate header sizing Oct 12, 2025
    @ArgoZhang ArgoZhang changed the title fix(Table): include header cell padding in auto-fit column width calculation to ensure accurate header sizing fix(Table): include padding in auto-fit column width Oct 12, 2025
    @ArgoZhang ArgoZhang merged commit a31fcaa into main Oct 12, 2025
    8 checks passed
    @ArgoZhang ArgoZhang deleted the Feat-Table-FitColumnWidthIncludeHeader branch October 12, 2025 01:26
    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): 计算表头单元格宽度时未包含 padding 量

    3 participants