【问题标题】:Table column width表格列宽
【发布时间】:2019-02-28 17:21:51
【问题描述】:

我有一个很大的 HTML 表格(比如 50 列),每列有 4 种可能的可配置样式:

  • 自动(如 flex 1)-> '自动'
  • px(像素数)-> '100px'
  • %(百分比数字)-> '10%'
  • content(该列最大内容的宽度)-> 'content'
<table *ngIf="features.length > 0" id="list-tab-table" #listTabTable>
    <colgroup>
        <col *ngFor="let attribute of features[0].getAttributesListView(); let i = index" [ngClass]="{
                'cls-auto': attribute.listviewwidth === 'auto',
                'cls-content': attribute.listviewwidth === 'content',
                'cls-px': attribute.listviewwidth.indexOf('px') > 0,
                'cls-percent': attribute.listviewwidth.indexOf('%') > 0
            }">
    </colgroup>
    <thead>
        <tr class="label-row">
            <th *ngFor="let attribute of features[0].getAttributesListView()">
                <p>{{attribute.label}}</p>
            </th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let feature of features; let i = index">
            <td *ngFor="let attribute of feature.getAttributesListView()" title="{{attribute.value}}">
                <p [innerHTML]="attribute.value"></p>
            </td>
        </tr>
    </tbody>
</table>

我已经尝试了所有我知道的东西:col 元素、表格布局、flex 项......但似乎没有什么能涵盖所有选项。

所有选项可能同时出现在不同的列中:column1-auto、column2-200px、column3-content、column4-10%、column5-100px、column6-20%...

Stackblitz:https://stackblitz.com/edit/table-widths

【问题讨论】:

  • 你能创建一个stackbliz吗?
  • 添加了 stackbliz。它只有 1 行和随机值,但已经足够了
  • StackBliz 更新了我的方法。它使用 vw 而不是 % (我知道它不一样,但唯一有效的),我认为必须有更好的方法。

标签: javascript css angular html-table


【解决方案1】:

我不太清楚您的各种描述所描述的内容,但可以实现很多目标,但可以在table 标签上输入特定属性并在col 元素上设置宽度等。

假设col 元素的数量是已知的,那么可以实现百分比宽度。

几个例子:

我认为这是您的“自动”选项

table {
  border: 1px solid grey;
  background: greenyellow;
  width: 100%;
}

col:nth-of-type(2n) {
  background: pink;
}

col {}
<table>
  <colgroup>
    <col>
    <col>
    <col>
    <col>
  </colgroup>
  <thead>
    <tr>
      <th>columns</th>
      <th>second column</th>
      <th>third column</th>
      <th>fourth column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Placeat id vero debitis nihil ducimus esse!</td>
      <td>more content</td>
      <td>random comment</td>
      <td>more stuff</td>
    </tr>
  </tbody>
</table>

固定像素宽度选项

table {
  border: 1px solid grey;
  background: greenyellow;
}

col:nth-of-type(2n) {
  background: pink;
}

col {
  width: 100px;
}
<table>
  <colgroup>
    <col>
    <col>
    <col>
    <col>
  </colgroup>
  <thead>
    <tr>
      <th>columns</th>
      <th>second column</th>
      <th>third column</th>
      <th>fourth column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Placeat id vero debitis nihil ducimus esse!</td>
      <td>more content</td>
      <td>random comment</td>
      <td>more stuff</td>
    </tr>
  </tbody>
</table>

% 选项:

table {
  border:1px solid grey;
  background: greenyellow;
  width:100%;
  table-layout: fixed
}


col:nth-of-type(2n) {
background: pink;
}

col:nth-child(2) {
  width:50%;
}
<table>
  <colgroup>
    <col>
    <col>
    <col>
    <col>
  </colgroup>
  <thead>
    <tr>
      <th>columns</th>
      <th>second column</th>
      <th>third column</th>
      <th>fourth column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Placeat id vero debitis nihil ducimus esse!</td>
      <td>more content</td>
      <td>random comment</td>
      <td>more stuff</td>
    </tr>
  </tbody>
</table>

内容决定选项:

table {
  border: 1px solid grey;
  background: greenyellow;
}

col:nth-of-type(2n) {
  background: pink;
}

col {}
<table>
  <colgroup>
    <col>
    <col>
    <col>
    <col>
  </colgroup>
  <thead>
    <tr>
      <th>columns</th>
      <th>second column</th>
      <th>third column</th>
      <th>fourth column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Lorem ipsum dolor sit, amet consectetur </td>
      <td>more content</td>
      <td>random comment</td>
      <td>more stuff</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 当你有喜欢...50 列时,一切都会中断。我会更新这个问题。当表格溢出时,宽度百分比不再起作用。
【解决方案2】:

你可以试试下面的css。它将向表格添加水平滚动条。

#list-tab-table {
    overflow-x: auto;        
}

【讨论】:

  • 它已经处于自动状态。但我找不到适合所有 4 个要求的解决方案
猜你喜欢
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 2023-03-18
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多