0

I used this answer to create a horizontal table as i have a fixed amount of horizontal rows but N number of vertical columns. This works really well but because the HTML rows are actually columns, the height between cells does not stay consistent in the displayed rows.

By adding the following to the answer provided, I'm able to ensure each column fits on the page, causing text wrap to occur, but then the rows height don't necessarily match.

td {
    max-width: 200px;
}

JSFiddle example.

In the fiddle example above, how do I make the cell with "Title" and the cell with "Braveheart" match the height of the third cell in that row?

Hint: the correct answer is not to hard code the height of the first two cells, since I have no idea how long another cell may be when it is added later.

EDIT

I can't use JavaScript as the html never hits anything client side. It's rendered and then passed into a PDF creation utility.

Community
  • 1
  • 1
Lefka
  • 633
  • 6
  • 20

1 Answers1

0

This may not be the best performing answer, or even the most concise, but it could start you in the right path (fiddle):

<div>
<table>
    <tbody>
        <tr>
            <th class="title">Title</th>
            <th class="year">Release Year</th>
            <th class="director">Director</th>
        </tr>
        <tr>
            <td class="title">Braveheart</br>part 2</td>
             <td class="year">1992?</td>
            <td class="director">Mel Gibson?</td>
        <tr>
            <td class="title">Some movie with a ridiculously long title. I mean, like two paragraphs for a title, which is ridiculous of course but it proves my point.</td>
            <td class="year">2015</td>
            <td class="director">The Great Anton - Director of the year 2012</td>
        </tr>
    </tbody>
</table>
</div>

$(document).ready(function(){

    function getMaxHeight(e){
        var maxHeight = Math.max.apply(null, $(e).map(function ()
           {
             return $(this).height();
           }).get());
        return maxHeight;
    }


   $('tr').each(function(){
       $(this).find('.title').css("height", getMaxHeight('.title')); 
       $(this).find('.year').css("height", getMaxHeight('.year'));
       $(this).find('.director').css("height", getMaxHeight('.director'));
   });
});

The max height function came from this SO answer.

Community
  • 1
  • 1
deebs
  • 1,390
  • 10
  • 23
  • I guess I should have mentioned that the answer needed to be HTML and CSS based (hence no javascript tag). I cannot use javascript as this is a razor view that never gets displayed client side. The straight HTML is passed to a PDF creation library and then rendered into PDF format. There is no option to execute javascript. – Lefka Sep 24 '15 at 20:10
  • ahhhh. yes that would have helped :) This seems to be an unusual layout (horizontal table)... are you forced to use that layout? A simple regular table layout would accomplish what you're looking for: http://jsfiddle.net/Lsr1vy6v/ – deebs Sep 24 '15 at 20:11
  • No but the idea is to minimize unnecessary code. I don't know how many columns will be displayed. It could be 1 or it could be 10. Therefore I went this route so the tr tags and subsequent td tags are all encapsulated in a foreach loop. Then it just dynamically creates what I need. It would still be possible to do this with a traditional table but I would end up with numerous foreach loops instead of one. – Lefka Sep 24 '15 at 20:17