简体   繁体   English

如何将html表行动态转换为列?

[英]How to dynamically convert html table rows to columns?

To modify the database format SQL uses two functions called PIVOT and UNPIVOT . 要修改数据库格式,SQL使用两个称为PIVOTUNPIVOT函数。 I was wondering, is there a way of achieving the same thing using a script on the client side? 我想知道,有没有办法在客户端使用脚本来实现相同的目的?

Let's take this table for example: 让我们以该表为例:

Rowa. pro_img#1|${product.name}|${product.price}|${product.description}
Rowb. prod_img#2|${product.name}|${product.price}|${product.description}
Rowc. prod_img#3|${product.name}|${product.price}|${product.description}
Rowd. pro_img#4|${product.name}|${product.price}|${product.description}

As you already know, every html table with dynamic data will print rows . 如您所知,每个带有动态数据的html表都将输出rows What i'm trying to do is having an html table with dynamic data printing columns so I can display a nice product listing like in this example: 我正在尝试做的是使用带有动态数据打印columns的html表,以便在此示例中显示出不错的产品清单:

Product_img#1|             Product_img#2|            Product_img#3|
Product_name|              Product_name|             Product_name|
Product_price|             Product_price|            Product_price|
Product_description|       Product_description|      Product_description|

I'm trying to display 3 objects per row. 我试图每行显示3个对象。 Is there a way of doing this with a simple javascript? 有没有一种方法可以使用简单的javascript?

I'm trying to achieve exactly the same thing as Vans (example here)! 我正在尝试实现与Vans完全一样的东西(此处示例)!

Edited New code 编辑新代码

 <div id="product_container"> <c:forEach var="product" items="${categoryProducts}" varStatus="iter"> <div id="product_image"><a href="viewProduct?${product.id}"> <img class="img" alt="" src="${initParam.productImagePath}${product.id} (1).jpg" /></a></div> <div id="product_name">${product.name}</div> <div id="product_price">${product.price}</div> <div id="add_toList"><form id="wishlistForm" action="addToWishlist" method="post"> <input name="productId" value="${product.id}" type="hidden"> <input class="submit" value="<fmt:message key='AddToWishlist'/>" type="submit"> </form></div> <div id="add_toCart"><form id="cartForm" action="addToCart" method="post"> <br> <br> <input name="productId" value="${product.id}" type="hidden"> <input class="submit" value="<fmt:message key='AddToCart'/>" type="submit"> </form></div> </c:forEach> </div> 

jsfiddle DEMO jsfiddle演示

This function takes an HTML table and returns a table which has the given table's rows and columns swapped. 此函数获取一个HTML表并返回一个表,该表已交换给定表的行和列。

Example input: 输入示例:

----------------
A1  |  A2  |  A3
----------------
B1  |  B2  |  B3
----------------

Output: 输出:

---------
A1  |  B1
---------
A2  |  B2
---------
A3  |  B3
---------

Javascript: 使用Javascript:

function convertTable(tbl) {
    var rows = tbl.rows.length;
    var cols = tbl.rows[0].cells.length;
    var tbl2 = document.createElement('table');

    for (var i = 0; i < cols; i++) {
        var tr = document.createElement('tr');
        for (var j = 0; j < rows; j++) {
            var td = document.createElement('td');
            var tdih = tbl.rows[j].cells[i].innerHTML;
            td.innerHTML = tdih;
            tr.appendChild(td);
        }
        tbl2.appendChild(tr);
    }
    return tbl2;
}

You could create custom divs for each object: 您可以为每个对象创建自定义div:

function generateProductDiv(product) {
   return
     '<div class="product">' +
         '<img src="' + product.image + '">' +
         '...' +
     '</div>';
}

Create a div for each product put them in a parent divs and style them with css (the CSS property display: table; and table-* might be of interest to you if you want to do it this way, another possibility is to use libraries). 为每个产品创建一个div并将它们放在父div中,并使用css对其进行样式设置(CSS属性display: table;table-*可能是您感兴趣的,如果您想这样做,则另一种可能性是使用库)。

The easier solution is just to put those divs inside the cells of a table although you should only use tables if you really want to display tabular data. 较简单的解决方案是将这些div放在表的单元格内,尽管您仅在确实要显示表格数据的情况下才使用表。

You seem to have JSP code, not Javascript, here is how to generate the form s in JSP (btw. JSP is the same as Java Servlets, just another way of writing it, simply put: JSP = html with Java, Servlet = Java with html). 您似乎拥有JSP代码,而不是Javascript,这是在JSP中生成form s的方法(顺便说一句。JSP与Java Servlets相同,只是另一种编写方式,简而言之:JSP =带有Java的html,Servlet = Java与html)。 Using forms instead of divs because that seems to be what you want: 使用表单而不是div,因为这似乎是您想要的:

<c:forEach var="product" items="${categoryProducts}" varStatus="iter">
    <form id="wishlistForm" action="addToWishlist" method="post">
        ...
        <a href="viewProduct?${product.id}">
            <img class="img" alt="" src="${initParam.productImagePath}${product.id} (1).jpg" />
        </a>
        ...
    </form>
</c:forEach>

The best (easiest, most maintainable, most semantic) way to do this is pure CSS. 最好的方法(最简单,最易维护,最语义)是 CSS。 You shouldn't have to change the markup at all. 您根本不需要更改标记。

And it's actually pretty easy. 而且实际上很容易。

You just have to get the browser to stop treating it like table data. 您只需要让浏览器停止将其像表数据一样对待即可。 The key is the display: CSS property. 关键是display: CSS属性。

display defaults to table-row for <tr> elements and table-cell for <td> elements, which makes sense. 对于<tr>元素, display默认为table-row ,对于<td>元素, display默认为table-row table-cell ,这很有意义。 You have to "break" that property. 您必须“破坏”该属性。

tr, td {
    display: block;
}

In other words, you're telling the browser to "display table row and table data elements just like any other block-level element (like a div, for example)". 换句话说,您要告诉浏览器“像其他任何块级元素(例如div一样)显示表行和表数据元素”。

This gets you almost all the way there. 这几乎可以带您到那里。 Now you have to get the table "rows" (which are now being laid out like div s) to stack next to each other, instead of on top of each other. 现在,您必须获取表“ rows”(它们现在像div s一样布置)彼此堆叠,而不是彼此堆叠。

tr {
    float: left;
}

Now, you end up with the possibility of the new "rows" not being the same height. 现在,您最终可能会遇到新的“行”高度不同的情况。 If, for example, your product images are not all the same height, or if your descriptions are different lengths, you'd end up with the table being way out of alignment. 例如,如果您的商品图片高度不相同,或者说明的长度不同,则最终导致表格不对齐。

So, you need to give your <td> s all the same height. 因此,您需要赋予<td>相同的高度。

td{
    height: 150px; /*or whatever height you want*/
}

So, the full CSS code looks like this: 因此,完整的CSS代码如下所示:

tr {
    display: block;
    float: left;
}
td {
    display: block;
    height: 150px;
}

Obviously, this is only the most basic code to get your foot in the door. 显然,这只是入门的最基本代码。 In the real world, you'd give each type of <td> its own height, something like: 在现实世界中,您应该为每种<td>类型赋予自己的高度,例如:

td.product-image {
    height: 150px;
}
td.product-description {
    height: 70px;
}
td.product-price {
    height: 40px;
}

Here's a codepen that puts it all together: table data pivoted 这是将所有内容组合在一起的codepen:透视表数据

Also, here's some more info on the CSS display property if you're interested. 此外,如果您有兴趣, 这里还有一些有关CSS display属性的信息

Best of luck. 祝你好运。 Feel free to ask in comments if you have any further questions. 如果您还有其他问题,请随时在评论中提问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM