如何从列数据而不是 HTML 中的行数据创建表格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16071864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create tables from column data instead of row data in HTML?
提问by Village
According to this article at W3 Schools, one can create a basic table in HTML like this:
根据W3 Schools上的这篇文章,可以像这样用 HTML 创建一个基本表格:
<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>
回答by Istvan
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:
在现代浏览器中,您可以通过在 CSS 中重新定义 TR 和 TD 标签行为来实现这一点。鉴于您问题中的 HTML,请附加下一个 CSS 样式:
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>
回答by Chris Green
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>
回答by Aheho
You're best bet is to render the table by rows, and then use javascript to invert the table
最好的办法是按行呈现表格,然后使用 javascript 反转表格
The following code will invert a table (this sample code uses jquery)
以下代码将反转表(此示例代码使用 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] = $(""); }
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/
这是一个也适用于 th 元素的版本:http: //jsfiddle.net/zwdLj/
回答by Fat Shogun
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.
只是通过使用一堆充满 lis 的 ul 来做到这一点,看起来比我能找到的任何东西都干净。您必须执行一些操作,例如向所有 uls 添加一个类并将其样式设置为 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;
}
回答by sandy
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.
我处于同样的情况,我必须按列添加数据。但是,这个问题可以通过一种简单的方法解决。我在这个例子中使用了 twig,但你会很容易得到它。
<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 }}
注意:{{这是要打印的数据}}