简体   繁体   English

如何在HTML5 / css3中创建一个水平换行的“表”?

[英]How can I make a “Table” where rows wrap horizontally in HTML5 /css3?

I have the following table using html table, tr, td tags. 我有下表使用html table,tr,td标签。 在此输入图像描述

This is all great, but I am using angular, and the problem is if I have more rows, I don't have the space for it vertically. 这一切都很棒,但我使用角度,问题是如果我有更多的行,我没有垂直的空间。 Instead I want to "overflow" to the right of it so that it looks like: 相反,我想“溢出”它的右边,使它看起来像:

在此输入图像描述

What is the best way using HTML5/CSS so that I can make it such that content spills over if it exceeds the table? 什么是使用HTML5 / CSS的最佳方式,以便我可以使内容溢出,如果它超出表? Note that in some cases I may have 2 or 3 entries, so I am hoping I don't have to make the width double the size of normal at the start, and instead have the table be sized based on the number of entries I have in the table. 请注意,在某些情况下,我可能有2或3个条目,所以我希望我不必在开始时使宽度加倍于正常大小,而是根据我拥有的条目数量来确定表的大小在表中。 No scrollbars. 没有滚动条。

I feel like the table tag in HTML is limiting and may not allow me to do this. 我觉得HTML中的表格标签是限制性的,可能不允许我这样做。 What is the best way to do this? 做这个的最好方式是什么? Using divs? 使用div?

<table style="background:green;">
<tr><td>Name</td><td>Sport</td><td>Score</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
</table>

Attempting a flexbox approach, though it seems that if I put the flexbox in a div, then the background doesn't appear to fill properly once the entries go beyond the first column: 尝试使用flexbox方法,虽然看起来如果我将flexbox放在div中,但是一旦条目超出第一列,背景似乎不会正确填充:

https://jsfiddle.net/t0h7w2hw/ https://jsfiddle.net/t0h7w2hw/

Please follow below code example 请按照下面的代码示例

This will wrap rows in a list style in mobile. 这将在移动设备中以列表样式包装行。 You need to data-label attribute with each content td so that it knows which column it is. 您需要为每个内容td添加数据标签属性,以便它知道它是哪个列。 In mobile you can show lable before td by using css content:"" property. 在移动设备中,您可以使用css内容:“”属性在td之前显示标签。

 body { font-family: "Arial", sans-serif; line-height: 1.25; } table { border: 1px solid #ccc; border-collapse: collapse; margin: 0; padding: 0; width: 100%; table-layout: fixed; } table caption { font-size: 1.5em; margin: .5em 0 .75em; } table tr { background: #f8f8f8; border: 1px solid #ddd; padding: .35em; } table th, table td { padding: .625em; text-align: center; } table th { font-size: .85em; letter-spacing: .1em; text-transform: uppercase; } @media screen and (max-width: 600px) { table { border: 0; } table caption { font-size: 1.3em; } table thead { border: none; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; } table tr { border-bottom: 3px solid #ddd; display: block; margin-bottom: .625em; } table td { border-bottom: 1px solid #ddd; display: block; font-size: .8em; text-align: right; } table td:before { /* * aria-label has no advantage, it won't be read inside a table content: attr(aria-label); */ content: attr(data-label); float: left; font-weight: bold; text-transform: uppercase; } table td:last-child { border-bottom: 0; } } 
 <table> <caption>Statement Summary</caption> <thead> <tr> <th scope="col">Card</th> <th scope="col">Due Date</th> <th scope="col">Amount</th> <th scope="col">Period</th> </tr> </thead> <tbody> <tr> <td data-label="Card">Visa - 3412</td> <td data-label="Due Date">04/01/2016</td> <td data-label="Amount">$1,190</td> <td data-label="Period">03/01/2016 - 03/31/2016</td> </tr> <tr> <td scope="row" data-label="Card">Visa - 6076</td> <td data-label="Due Date">03/01/2016</td> <td data-label="Amount">$2,443</td> <td data-label="Period">02/01/2016 - 02/29/2016</td> </tr> <tr> <td scope="row" data-label="Card">Corporate AMEX</td> <td data-label="Due Date">03/01/2016</td> <td data-label="Amount">$1,181</td> <td data-label="Period">02/01/2016 - 02/29/2016</td> </tr> <tr> <td scope="row" data-label="Card">Visa - 3412</td> <td data-label="Due Date">02/01/2016</td> <td data-label="Amount">$842</td> <td data-label="Period">01/01/2016 - 01/31/2016</td> </tr> </tbody> </table> 

Actually there is no such facility is defined for HTML table element by using CSS. 实际上,没有使用CSS为HTML表元素定义这样的工具。 I have tries some by giving various positions for table and containing div, not worked. 我尝试了一些通过给表和包含div的各种位置,没有工作。 As in the fiddle example you have provided, you must use HTML div element for that. 与您提供的小提琴示例一样,您必须使用HTML div元素。

//HTML

<div ng-app>
  <div class=" header-row">
    <div class="col">Name</div>
    <div class="col">Sports</div>
    <div class="col">Score</div>
  </div>
  <div ng-controller="TodoCtrl">
    <div class="row" ng-repeat="rows in chunkedData">
      <div class="span4" ng-repeat="item in rows">
        <span class="col">{{item.name}}</span>
        <span class="col">{{item.sport}}</span>
        <span class="col">{{item.score}}</span>
      </div>
    </div>
  </div>
</div>


//Script

function TodoCtrl($scope) {
  $scope.data = [
    {
      name : '0',
      sport: 'Cricket',
      score: 89
    },
    {
      name : '1',
      sport: 'Cricket',
      score: 89
    },
    {
      name : '2',
      sport: 'Cricket',
      score: 89
    },
    {
      name : '3',
      sport: 'Cricket',
      score: 89
    },
    {
      name : '4',
      sport: 'Cricket',
      score: 89
    },
    {
      name : '5',
      sport: 'Cricket',
      score: 89
    },
    {
      name : '6',
      sport: 'Cricket',
      score: 89
    }
  ];
  $scope.chunkedData = chunk($scope.data, 3);
  function chunk(arr, size) {
    var newArr = [];
    for (var i=0; i<arr.length; i+=size) {
      newArr.push(arr.slice(i, i+size));
    }
    return newArr;
  }
}

//CSS

.col{
  display: inline-block;
  width:70px;
  float: left;
  padding: 3px;
}
.header-row{
  width: 100%;
  background-color: #000;
  color: #fff;
  overflow: hidden;
  padding: 3px;
}
.row{
  overflow: hidden;
  display: inline-block;
  float: left; 
  border: 1px solid grey;
}

JsFiddle Link http://jsfiddle.net/U3pVM/32296/ JsFiddle Link http://jsfiddle.net/U3pVM/32296/

You simply need to create a chunk of records for first column which you can do in the controller itself and then with the help of ng-repeat you can render it 您只需要为第一列创建一大块记录,您可以在控制器本身中创建,然后在ng-repeat的帮助下,您可以渲染它

Using div and column 使用divcolumn

 .columns { column-count: 2; column-gap: 10px; } .myTable { display: table; width: 100%; height: 200px; } .myHeader { width: 100%; font-weight: bold; } .myCell { display: table-cell; width: 33%; float: left; } 
 <div class="columns"> <div class="myTable"> <div class="myHeader"> <div class="myCell">Name</div> <div class="myCell">Sport</div> <div class="myCell">Score</div> </div> <div class="myCell">Bob</div> <div class="myCell">Soccer</div> <div class="myCell">8</div> <div class="myCell">Jane</div> <div class="myCell">Softball</div> <div class="myCell">9</div> <div class="myCell">Hank</div> <div class="myCell">Baseball</div> <div class="myCell">6</div> <div class="myCell">Lisa</div> <div class="myCell">Soccer</div> <div class="myCell">9</div> <div class="myCell">Bill</div> <div class="myCell">Football</div> <div class="myCell">4</div> </div> </div> 

FIDDLE 小提琴

This is all great, but I am using angular, and the problem is if I have more rows, I don't have the space for it vertically. 这一切都很棒,但我使用角度,问题是如果我有更多的行,我没有垂直的空间。

What has angular got to do with any of this? 什么角度与任何这个有关?

[..] if I have more rows, I don't have the space for it vertically. [..]如果我有更多行,我没有垂直空间。 Instead I want to "overflow" to the right of it 相反,我想“溢出”到它的右边

Tables won't give you that kind of flexibility especially, you won't be able to control height and flow of the rows as columns. 表格不会给你那种灵活性,尤其是你不能控制行的高度和流量作为列。

[..] so that I can make it such that content spills over if it exceeds the table? [..]这样我可以使内容溢出,如果超出表格? Note that in some cases I may have 2 or 3 entries, so I am hoping I don't have to make the width double the size of normal at the start, and instead have the table be sized based on the number of entries I have in the table. 请注意,在某些情况下,我可能有2或3个条目,所以我希望我不必在开始时使宽度加倍于正常大小,而是根据我拥有的条目数量来确定表的大小在表中。

One simple and quick way could be to split your rows into tables of their own. 一种简单快捷的方法是将行拆分为自己的表。 If you can have that structure, then it is simply a matter of using CSS columns . 如果您可以拥有该结构,那么只需使用CSS columns

Typically, you would wrap all these tables in a div which will have its height constraints. 通常,您将所有这些表包装在一个具有height约束的div中。 Set column-* properties on the wrapping div and there you go. 在包装div上设置column-*属性,然后就可以了。 You may play around with column-gap , column-rule , column-span , and column-fill properties to beautify the whole thingy. 您可以使用column-gapcolumn-rulecolumn-spancolumn-fill属性来美化整个东西。

Also note that you will need to use break-inside: avoid on the tables to avoid them breaking half-way while flowing to the next column. 另请注意,您需要使用break-inside: avoid在桌面上使用,以避免它们在流向下一列时中途中断。

Example Snippet: 示例代码段:

 #rowflow { height: 120px; max-height: 120px; border: 1px solid #d33; overflow: hidden; column-width: 200px; column-gap-width: 0px; column-fill: auto; column-rule: 1px solid #bbb; } table { font-family: sans-serif; margin-bottom: 2px; table-layout: fixed; width: 190px; } table:first-child { column-span: all; } table, th, td { border-collapse: collapse; border: 0px solid gray; padding: 6px; } table, tr { break-inside: avoid; } th, td { text-align: left; } th:last-child, td:last-child { text-align: right; } 
 <div id="rowflow"> <table><tr><th>Name</th><th>Sport</th><th>Score</th></tr></table> <table><tr><td>Jordan</td><td>Soccer</td><td>50</td></tr></table> <table><tr><td>Jordan</td><td>Soccer</td><td>50</td></tr></table> <table><tr><td>Jordan</td><td>Soccer</td><td>50</td></tr></table> <table><tr><td>Jordan</td><td>Soccer</td><td>50</td></tr></table> <table><tr><td>Jordan</td><td>Soccer</td><td>50</td></tr></table> </div> 


In the above snippet, the wrapping div is height constrained and you can see the columns flowing. 在上面的代码片段中,包装div是高度约束的,您可以看到列流动。

Use this fiddle to try changing the window size and see the effect. 使用此小提琴尝试更改窗口大小并查看效果。

[..] so I am hoping I don't have to make the width double the size of normal at the start, and instead have the table be sized based on the number of entries I have in the table. [..]所以我希望我不必在开始时将宽度增加到正常大小的两倍,而是根据表中的条目数量来确定表的大小。 No scrollbars. 没有滚动条。

The wrapping div will be full-width on its own. 包装div将自己全宽。 However, you can twiddle with overflow to control how the overflow is handled. 但是,您可以通过overflow来控制溢出的处理方式。 Changing width dynamically based on the width of the content is a whole different ball-game altogether where you would need JavaScript and it gets messy. 根据内容的宽度动态改变宽度是一个完全不同的球类游戏,你需要JavaScript,它会变得混乱。

Note: For the above example to work, you will need to fix the widths of your tables so that the columns align neatly. 注意:要使上述示例起作用,您需要修复表格的宽度,以使列整齐排列。 You may use table-layout: fixed to use a fixed layout. 您可以使用table-layout: fixed来使用固定布局。

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

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