views:

3008

answers:

3

I have table with multiple rows, showing items for sale. When the user clicks on a row, a Javascript inserts / shows a new row right beneath it with details about the item. The issue is when the description is long, it forces the column widths to readjust / resize. This shifts the columns positions and is really annoying, especially for the user. Right now, I have my table.style.tableLayout: auto. I actually prefer it this way, because the columns are adjusted to the content.

My question is: how do I dynamically "lock" the widths of the columns in my table so that when I insert / show the new row, the columns do not readjust / resize?

I've tried:

  1. dynamically setting the table to temporarily "tableLayout: fixed"
  2. inserting / showing my new row
  3. changing the table back to "tableLayout: auto"

Actions 1 & 2 works in in FireFox, but not in Safari and IE (6 & 7). However, doing all three seems to prevent the columns from shifting too much.

The frustration is unbearable ... loosing lots of sleep ... please help!

Thanks.

A: 

You can display the details of the row beneath the clicked one in DIV and set its

style="overflow:auto";

so that details will wrap and scrollbar will be available to display entire text.

Sachin Gaur
Hi. The issue is that the extra text seems to resize the widths of the columns (i.e with table.style.tableLayout: fixed, there's no resizing of col widths, but with tableLayout: auto, there is). I want the table to automatically adjust for content first, but then freeze the widths of the columns.
A: 

I don´t know if you´re familiar with jquery, but that´s what I would use - in combination with a separate class for the column that´s causing resizing in the new row - to:

  1. Calculate / get the with of the column
  2. Set the with of the afore mentioned class
  3. Add the row

I haven´t tried it, but that should do it.

By the way, there are probably other ways to do it, I´m just more familiar with jquery (for point 1. and 2.).

jeroen
+1  A: 

I would set a percent width on each column simply as a guide. Set it just once on the TH of each column. The browser will still adjust the columns to content if necessary, but the columns will stay in place more consistently.

Next, I would never put css "white-space:nowrap" anywhere on that table. A long description should not break the table layout, it should wrap around properly on multiple lines, and be readable if you set the widths on each column to suit the type of data. Similarly I would keep the use of   (non breakable spaces) to dates, times and numbers and allow the text to wrap.

Other than that, I do this at my job on a dialy basis, and there's a time when you need to stop ulling hairs asking the browser to do something it's not designed to do. Content should flow and adapt. Locking column widths to pixels is 99.99999% of the time a bad idea.

PS: If you really, reeally, REALLY need to lock columns, the only solution I'm aware of that works with CSS2 and accross all browsers is to use images. You could insert a 1px high transparent gif image in each column, and counting in the padding of the cells (TD), set a pixel width on each image (IMG), instead of on the columns (TH/TD). You could hide those in the TH for example. You can leave the images at 1 pixel wide and set percent widths on TDs, and when you open a new row, you would get each column width minus TD Padding, and set that to the corresponding IMG. I haven't tried! I just know that in many projects I've worked on, I've used small gif images to lock a minimum vertical spacing between columns, for example.

faB