views:

105

answers:

8

I would want to create a table with the same widths (table and its cells) then a previous one created dynamically

1st

__________________
|_____|___|_______|
|_____|___|_______|
|_____|___|_______|

2nd

__________________
|_____|___|_______|
|_____|___|_______|

What do I need to do to set the second one with the desired widths. (Table 1 widths depends on it's content).

Edit: Finally, I went for the jQuery solution and did it like that:

  $("#mySecondTable tr") // mySecondTable was already generated server side
  .first() // All rows in my second table are similar so I just apply on the first one
  .find("td")
  .width(function(i){ return $("#myFirstTable tr").eq(1).find("td").eq(i).width() })
  // eq(1) fetches the line I want to use as a model since some have different colspans
  .end()
+5  A: 

Use Javascript (I'd recommend jQuery) to get the width of each "column" (th/td) from the 1st table, and set the width CSS property to that pixel value on the th and/or tds in the 2nd table.

Matt Ball
+1  A: 

if it is acceptable in your case use style="table-layout:fixed;" on both the tables to exactly controls the cell width

luca
A: 

Since PHP works server side it can't be aware of browser windows dimensions and/or table design. It may check the table cell contents for string lengths and you may estimate and set the width of any cell in your output. But if table one has no width properties you'll probably need to use some javascript on your second table to make them fit the first widths.

Paul
A: 

'luca' and 'Bears will eat you' gave two good solutions. The only other one I see is to put all the data in one table, rather than two.

Sonny
A: 

If I'm understanding correctly, its seems you should just css the desired width, and then everything will stretch according to the content inserted

Ralph The Mouf
+1  A: 

The quick and dirty answer: you could make them one table, with the text in between them being a single row with colspan="x", where x is either the number of columns in the table, or the number of columns + 1.

Dean J
A: 

Would it work for you to not actually put them in separate tables, but have both as a part of a single table, separated by a row with a <td colspan="3">? Maybe include a header for the second table in that?

Scott Cranfill
I had tried this solution but I don't think we can hide the table border for just one tr. Which makes the whole thing look weird for what I want to do
DrDro
+1  A: 

You could accomplish this with jQuery pretty easily:

$("table.foo")
  .clone()
    .find("td")
      .html("")
      .width(function(i){ return $("table.foo td").eq(i).width() })
      .height(function(i){ return $("table.foo td").eq(i).height() })
    .end()
  .appendTo("body");

Online Demo: http://jsbin.com/obuco/3/edit

Jonathan Sampson
That looks good, I'm going to test it right now.
DrDro