元素-响应表问题

时间:2019-02-16 05:20:07

标签: javascript html css responsive-design responsive

我使用Element(https://element.eleme.io/#/en-US)创建了一个应用,但我一直在努力使表具有响应性。

当我使窗口变宽时,所有功能都可以正常工作,但是当我使窗口变窄时,表的宽度保持不变,我尝试了所有表选项,但是似乎没有任何方法可以使它变好,这是我的示例:https://streamable.com/1j1e1 ,刷新页面后,表格会正确调整大小。

我发现,如果我在element-ui.common.js中注释以下行:

  this.bodyWidth = Math.max(bodyMinWidth, bodyWidth);
  this.table.resizeState.width = this.bodyWidth;

它可以工作,但是我不确定其他选项是否仍然可以工作。

能帮我吗?有什么办法可以使该表响应? :)

4 个答案:

答案 0 :(得分:0)

我不知道Element UI中的数据属性绑定,因此我仅使用纯HTML和CSS共享了响应表的代码。 在移动/平板电脑视图中,我们可以使用data属性显示表格。

.rwd-table {
  margin: 1em 0;
  min-width: 300px;
}
.rwd-table tr {
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
}
.rwd-table th {
  display: none;
}
.rwd-table td {
  display: block;
}
.rwd-table td:first-child {
  padding-top: .5em;
}
.rwd-table td:last-child {
  padding-bottom: .5em;
}
.rwd-table td:before {
  content: attr(data-th) ": ";
  font-weight: bold;
  width: 6.5em;
  display: inline-block;
}
@media (min-width: 480px) {
  .rwd-table td:before {
    display: none;
  }
}
.rwd-table th, .rwd-table td {
  text-align: left;
}
@media (min-width: 480px) {
  .rwd-table th, .rwd-table td {
    display: table-cell;
    padding: .25em .5em;
  }
  .rwd-table th:first-child, .rwd-table td:first-child {
    padding-left: 0;
  }
  .rwd-table th:last-child, .rwd-table td:last-child {
    padding-right: 0;
  }
}

body {
  padding: 0 2em;
  color: #444;
  background: #eee;
}

h1 {
  font-weight: normal;
  letter-spacing: -1px;
  color: #34495E;
}

.rwd-table {
  background: #34495E;
  color: #fff;
  border-radius: .4em;
  overflow: hidden;
}
.rwd-table tr {
  border-color: #46637f;
}
.rwd-table th, .rwd-table td {
  margin: .5em 1em;
}
@media (min-width: 480px) {
  .rwd-table th, .rwd-table td {
    padding: 1em !important;
  }
}
.rwd-table th, .rwd-table td:before {
  color: #dd5;
}
<table class="rwd-table">
  <tr>
    <th>Movie Title</th>
    <th>Genre</th>
    <th>Year</th>
    <th>Gross</th>
  </tr>
  <tr>
    <td data-th="Movie Title">Star Wars</td>
    <td data-th="Genre">Adventure, Sci-fi</td>
    <td data-th="Year">1977</td>
    <td data-th="Gross">$460,935,665</td>
  </tr>
  <tr>
    <td data-th="Movie Title">Howard The Duck</td>
    <td data-th="Genre">"Comedy"</td>
    <td data-th="Year">1986</td>
    <td data-th="Gross">$16,295,774</td>
  </tr>
  <tr>
    <td data-th="Movie Title">American Graffiti</td>
    <td data-th="Genre">Comedy, Drama</td>
    <td data-th="Year">1973</td>
    <td data-th="Gross">$115,000,000</td>
  </tr>
</table>

答案 1 :(得分:0)

kavinraj的方法是一个很好的解决方案。 如果表格真的很宽,您也可以将其包装在div中。

<div class="table-container">
    <table></table>
</div>

@media screen and (max-width:768px) {
    .table-container {
        overflow: auto;
    }
}

用户可以滑动以查看表格的其余部分

答案 2 :(得分:0)

使用以下css-每行将显示4列,您可以更改每行显示更多或更少列的百分比:

@media only screen and (max-width: 770px) {
  table tr td{
    float: left;
    width:25%;
  }
}

答案 3 :(得分:0)

我参加聚会有点晚了,但当我刚刚偶然发现同样的问题时,我想我会分享我的发现:

当我用 divspan 包裹我的桌子时,我遇到了完全相同的问题。扩展时完美响应,但在再次缩小浏览器窗口时根本没有响应。好像卡在最大宽度一样。

因为我真的需要将我的表格包裹在一些东西中以使用 v-if 并有时隐藏整个表格组件,我一直尝试并最终发现表格在包裹在 <template> 标签中时表现正常.

相关问题