How to create tables from column data instead of row data in HTML?

HtmlHtml Table

Html Problem Overview


According to this article at W3 Schools, one can create a basic table in HTML like this:

<table border="1">
    <tr>
        <td>row 1, cell 1</td>
        <td>row 1, cell 2</td>
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table>

From above, it appears that one enters data by rows.

I have a situation where I need to enter all of the data by columns. Is something like this possible?

<table border="1">
    <tc>
        <td>row 1, cell 1</td>
        <td>row 2, cell 1</td>
    </tc>
    <tc>
        <td>row 1, cell 2</td>
        <td>row 2, cell 2</td>
    </tc>
</table>

Html Solutions


Solution 1 - Html

In modern browsers you can achieve this by redefining the TR and TD tags behavior in CSS. Given the HTML in your question attach the next CSS style:

table {
	display: table;
}
table tr {
	display: table-cell;
}
table tr td {
	display: block;
}

<table border="1">
    <tr>
        <td>row 1, cell 1</td>
        <td>row 2, cell 1</td>
    </tr>
    <tr>
        <td>row 1, cell 2</td>
        <td>row 2, cell 2</td>
    </tr>
</table>

Solution 2 - Html

You can render tables in columns by using a table within a table...

<table>
<tr>
<td>
    <table>
        <thead>
            <tr>
                <td>column 1 header 1</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>column 1 row 1</td>
            </tr>
            <tr>
                <td>column 1 row 2</td>
            </tr>
        </tbody>
    </table>
</td>
<td>
    <table>
        <thead>
            <tr>
                <td>column 2 header 1</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>column 2 row 1</td>
            </tr>
            <tr>
                <td>column 2 row 2</td>
            </tr>
        </tbody>
    </table>
</td>
</tr>
</table>

Solution 3 - Html

You're best bet is to render the table by rows, and then use javascript to invert the table

http://jsfiddle.net/CsgK9/2/

The following code will invert a table (this sample code uses jquery)


$("table").each(function() {
var $this = $(this);
var newrows = [];



    $this.find("tr").each(function(){
        var i = 0;
        
        $(this).find("td").each(function(){
            i++;
            if(newrows[i] === undefined) { newrows[i] = $("<tr></tr>"); }
            newrows[i].append($(this));
        });
    });
    
    $this.find("tr").remove();
    $.each(newrows, function(){
        $this.append(this);
    });
});


UPDATE:

Here is a version that works with th elements as well: http://jsfiddle.net/zwdLj/

Solution 4 - Html

Just did this by using a bunch of uls filled with lis, seemed a lot cleaner than anything I could find. You'll have to do something like adding a class to all the uls and setting its style to display: inline-table.

@* pseudo html/razor *@
@foreach(var col in columns){
    <ul class='tableColumn'>
        foreach(var data in col){
            <li>data</li>
        }
    </ul>    
}

and some styling

.tableColumn {
    border: 1px solid;
    display: inline-table;
}

Solution 5 - Html

You can always create a parent element (the table), and then inside the table you can create in-line block elements with limited width to serve as your columns, that way any content you add to those child columns will follow the downward pattern, then to make the grid-like pattern just make sure to set the height of the elements within the column, like so Using the list to display content:

<div id="your_table">
    <span style="width: 25%" id="fist_column"> <ul></ul></span>
    <span style="width: 25%" id="second_column"><ul></ul></span>
    <span style="width: 25%" id="third_column"><ul></ul></span>
    <span style="width: 25%" id="fourth_column"><ul></ul></span>
</div>

Solution 6 - Html

I was in same situation where I have to add the data by column. But, this problem is solved in a simple method. I have used twig in this example, but you will get it easily.

<table>
<tr>
    <th>id</th>
    <th>title</th>
    <th>ISBN</th>
    <th>author_id</th>
    <th>publisher_id</th>
</tr>

{% for book in books %}   //for loop is used before the <tr> tag
    <tr>
	    <td>{{ book.id }}</td>
		<td>{{ book.title }}</td>
		<td>{{ book.isbn }}</td>
		<td>{{ book.publisher.id }}</td>
		<td>{{ book.author.id }}</td>
	{% endfor %}
	</tr>

Note:{{ this is data to print }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVillageView Question on Stackoverflow
Solution 1 - HtmlIstvanView Answer on Stackoverflow
Solution 2 - HtmlChris GreenView Answer on Stackoverflow
Solution 3 - HtmlAhehoView Answer on Stackoverflow
Solution 4 - HtmlFat ShogunView Answer on Stackoverflow
Solution 5 - HtmlVictor MoraesView Answer on Stackoverflow
Solution 6 - HtmlsandyView Answer on Stackoverflow