Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syncing column width of between tables in two different frames, etc


For reasons which are somewhat unavoidable (lots of legacy code, compatibility, design needs) I have the following problem: I have two tables, one directly below the other, but split between two frames (see the pseudo-example below my sig.). I need the column widths of these tables to synchronize exactly so that these two tables 'act' like one. The reason being to have a 'header' table which will not scroll above a 'data' table which can scroll.

Now there are a few obvious suggestions which don't (yet) work.

First, I have heard that by using CSS there is a way to put a scrollbar just on the table rows, not the table header, which is the intended effect here. Unfortunately, this is not a viable option due to the reasons mentioned above.

Second, percentage width formatting on the columns. Unfortunately the scrollbar will mess this up, and this solution also shares a problem with the next possible solution: pixel width formatting. Here, if there is column content that is extra wide these widths (px or %) will be overridden in one table but not the other, and one mismatching width will break -all- the vertical alignments. Apparently correcting this with a CSS 'max-width' does not seem to work.

The final possible solution is using some sort of Javascript and DOM to dynamically force the issue. Here forcing a min-width on each column and forcing bottom widths to override the top widths would be sufficient. Still, the ability to actually split a table in two while having them share the same column/row model would be pretty neat to. Hopefully this solution is feasable and not extremely complicated (pardon my present lack of knowledge RE Javascript/DOM).

Thanks,

Skolem

<!-- In frame 1 (top, non-scrolling): -->
<table id="t1" ...> (Just the header, really)
  <tr> 
    <td>Name</td><td>User Image</td><td>Email</td><td>Favorite Language</td>
  </tr>
</table>

<!-- In frame 2 (bottom, scrolling): -->
<!-- table "t2" intended to have equal column widths -->
<table id="t2" ...> (Data below the header)
  <tr>
   <!-- Lots of crazy stuff of wildly varying widths -->
   <td>...</td><td>...</td><td>...</td><td>...</td>
  </tr>
</table>
like image 998
Rex Butler Avatar asked Aug 26 '10 22:08

Rex Butler


People also ask

How do I make columns with different widths in a table?

Change column and row widthSelect the boundary of the column or row you want to move and drag it to the width or height you want. Select the rows or columns and then select Layout and choose your height and width. Select View > Ruler checkbox, select the cell you want, and then drag the markers on the ruler.

How do I change the column width in Word without affecting other cells?

Before adjusting the column width, make sure the table's 'preferred width' option is not checked. To resize a column without affecting other columns, move the gray column markers on the ruler instead of dragging cell edges. Works like a charm.

How do you make all tables the same size in Word?

On the Layout tab, you can specify the custom height and width. To resize specific rows or column, click on a cell and then adjust the row/column. To make multiple rows or columns the same size, select the columns or rows and click Distribute Rows or Distribute Columns.


2 Answers

First, set the t1's width to t2's width. Then, take every td from t1, and set its width matching the widths of t2's columns.

Try this proof of concept: http://jsfiddle.net/HqpGp/.

You will need jQuery, and modify it to work with frames, but i think it's a good start. I'm pretty sure the JS part could be written in a nicer way, though.

Hope it helps!

like image 104
fcingolani Avatar answered Sep 28 '22 09:09

fcingolani


I couldn't get the accepted answer to work directly, so I reworked it a bit, just in case it doesn't work for anyone else.

$('.table1 tr:eq(1) td').each(function (i) {
                var _this = $(this);
                $('.table2 tr:eq(1) td:eq(' + i + ')').width(_this.width());
            });
like image 28
Brian Leishman Avatar answered Sep 28 '22 07:09

Brian Leishman