Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:14:44+00:00 2026-06-15T19:14:44+00:00

I am trying to determine the widest content in HTML table cells of fixed

  • 0

I am trying to determine the widest content in HTML table cells of fixed width. Currently I am using an external sizer DIV:

 <div id="xdivSizer" style="position:absolute;display:inline;visibility:hidden">
 </div>

in this code

var iColWidth = 0;

for (var Y = 0; Y < oDataTable.rows.length; Y++) {
    oDivSizer.innerHTML = oDataTable.rows[Y].cells[0].innerHTML;
    iColWidth = Math.max(oDivSizer.offsetWidth, iColWidth)
}

It works, but extremally slow, even when the table has only 100 rows or so. It looks like main offender is calculating of offsetWidth. Is there a more efficient way to achieve this?

I am doing this because I need to resize that table column to the size of the widest data. If there’re other better way, that would be great as well.

(More precisely I am trying to implement “column autosize” feature in Infragistics WebHierarchicalDataGrid control for ASP.NET – columns need to take the widest width Max(Header Width, Data Width) while maintaining fixed header/pager positions).

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:14:45+00:00Added an answer on June 15, 2026 at 7:14 pm

    Rewriting the DIV contents will trigger copious redraws, applications of your CSS, DOM manipulations etc..

    Try putting all of the cells from the column in the DIV all at once, separated by line breaks — or each in it’s own sub-DIV. The width of the resulting DIV should be that of the longest cell.

    var html = [];
    for (var Y = 0; Y < oDataTable.rows.length; Y++) {
        html.push(oDataTable.rows[Y].cells[0].innerHTML);
    }
    
    oDivSizer.innerHTML = html.join("<br />");
    var iColWidth = Math.max(oDivSizer.offsetWidth, 1);
    

    Another possibility is a simple heuristic based on each cell’s innerText.

    var iColWidth = 0;
    for (var Y = 0; Y < oDataTable.rows.length; Y++) {
        var w = Math.floor(oDataTable.rows[Y].cells[0].innerText.length * 11.5);
        iColWidth = Math.max(w, iColWidth);
    }
    

    And yet another possibility, as indicated in my last comment, is to redraw only the TD in the column that consumes the greatest area.

    var a = 0;
    var n = null;
    for (var Y = 0; Y < oDataTable.rows.length; Y++) {
        var _a = oDataTable.rows[Y].cells[0].offsetHeight * oDataTable.rows[Y].cells[0].offsetWidth;
        if (Number(_a) > a) {
          a = _a;
          n = oDataTable.rows[Y].cells[0];
        }
    }
    oDivSizer.innerHTML = n.innerHTML;
    var iColWidth = Math.max(oDivSizer.offsetWidth, 1);
    

    Or, taking it a step further, divide the calculated area of each cell by the number of BR’s found to account for “intentionally” tall cells.

    var a = 0;
    var n = null;
    for (var Y = 0; Y < oDataTable.rows.length; Y++) {
        var _a = oDataTable.rows[Y].cells[0].offsetHeight * oDataTable.rows[Y].cells[0].offsetWidth;
        var brs = oDataTable.rows[Y].cells[0].getElementsByTagName('br');
        _a = Number(_a) / brs.length;
        if (_a > a) {
          a = _a;
          n = oDataTable.rows[Y].cells[0];
        }
    }
    oDivSizer.innerHTML = n.innerHTML;
    var iColWidth = Math.max(oDivSizer.offsetWidth, 1);
    

    But, as the complexity of the fuzzy approaches grows and the result becomes more accurate, you can expect performance to decline.

    As a side note, if this auto-sizing is being applied as the result of a user interaction, I’d suggest performing the sizing calculations / processes immediately after the table is available to be analyzed, rather than waiting for the user to initiate the resize.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to determine how to count the matching rows on a table using
I am trying to determine if a phone is located in this polygon using
I'm trying to determine the iPhone user's location using a CLLocationManager and CLGeocoder. My
I'm trying to determine when to provide static HTML vs. when to generate HTML
I'm trying to determine if the user is using 24 hour or 12 hour
I am trying to determine the class type of a class using reflection and
I'm trying to determine the version number of my Silverlight application. Currently I am
I am trying to determine whether a ring is contained by another ring using
I'm trying to determine if my Kinect is plugged into the PC using the
I'm currently trying to determine the name of a parent process that launched a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.