1

I have a header table and a data table that need to match up. Currently, I am attempting to grab the first row of each table, get those cells, cycle over each cell, determine which width is the longest, and set the width of the other cell based on that. However, there is no change at all. The code is below and runs on document ready. Help?

function setHeaderLengths(){
    var headers = document.getElementById("headersTbl").rows[0].cells;
    var data = document.getElementById("dataTbl").rows[0].cells;
    for (var i = 0; i < data.length; i++){
        if(data[i].offsetWidth > headers[i].offsetWidth){
            headers[i].style.width = data[i].offsetWidth;
        }
        else{
            data[i].style.width = headers[i].offsetWidth;
        }
    }

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • 1
    You have a typo on your first `if`. `offseWidth` instead of `offsetWidth`. – Rémi Breton Feb 24 '12 at 20:01
  • check my anwser here http://stackoverflow.com/a/3679666/443685 – Saic Siquot Feb 24 '12 at 20:09
  • @LuisSiquot Your solution loops through all cells (a possibly costly procedure). Shouldn't I be able to set just the first row and the length waterfall down to the others? – steventnorris Feb 27 '12 at 15:51
  • my script, measures one cell per column on each table, on row 0 (for two or more tables), but not all cells!!! After that sets maximum width on row 0 on all tables – Saic Siquot Feb 27 '12 at 16:48
  • @LuisSiquot Ok that makes sense then. I must have missed that part. I've done some checking, and my rows seem to not be changing in size at all unless a certain threshold is reached (like a couple 1000 px). It's as if the style.width attribute is diminishing the px set when it displays. (This table is built auto with a asp.net gridview so that may be where the weirdness is). – steventnorris Feb 27 '12 at 16:53
  • do any cell or the table a width value in percentage? ie: width:100% – Saic Siquot Feb 27 '12 at 17:08
  • 1
    Nope. It's really odd width behaviour. I haven't seen anything like it before. I'm beginning to think my script above works, but the width is just "too small" by these standards to change anything. I may just end up killing gridview and building my own table. I thought the out-of-the-box sorting and paging would be easier... – steventnorris Feb 27 '12 at 17:12

0 Answers0