css - 在多列的material-ui表格上添加水平滚动的方法是什么?

标签 css reactjs material-design material-ui

我关注 this table example对于 React material-ui 框架,我正在寻找一种可能性,当我有很多列时,可以使我的表格水平滚动。

例如,我有许多列被压缩以适应页面宽度,因此它们的内容被缩短了。

我认为它在 material-ui 规范中通过链接 Display the full column content and enable horizontal scrolling in the table container 进行了描述.

所以我想知道在material-ui中是否可行。

最佳答案

只有 overflow-x 是不够的。还需要向列添加“flex-basis”和“flex-shrink”属性。在下面的示例中,将 35% 的 flex-basis 添加到 3 列会将表格宽度增加到 105%,并且 'flex-shrink: 0' 确保列在宽度不够时不会收缩:

enter image description here

app.component.html

<mat-table #table [dataSource]="payments" matSort class="mat-elevation-z8 overflow-x-auto">
  <ng-container matColumnDef="payment_hash">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Payment Hash</mat-header-cell>
    <mat-cell *matCellDef="let payment">{{payment?.payment_hash}}</mat-cell>
  </ng-container>
  <ng-container matColumnDef="path">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Path</mat-header-cell>
    <mat-cell *matCellDef="let payment">{{payment?.path}}</mat-cell>
  </ng-container>
  <ng-container matColumnDef="payment_preimage">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Payment Pre Image</mat-header-cell>
    <mat-cell *matCellDef="let payment">{{payment?.payment_preimage}}</mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

应用程序组件.css

.overflow-x-auto {
  overflow-x: auto;
}

mat-header-cell, mat-cell {
    /* 
    flex-shrink: 0;
    flex-basis: 35%;
    */
    flex: 0 0 35%;
}

关于css - 在多列的material-ui表格上添加水平滚动的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43902850/

相关文章:

javascript - 页面中的多个滑动面板

javascript - api block 被调用 2 次,导致渲染组件两次,但条件得到正确检查

适用于较低 API 的 Android Material Design 抽屉导航

reactjs - 多个 setState 不会累积到一次更新/渲染中...?

android - 将图标+文本添加到 TabLayout

java - 使用 RecyclerView 时的 getListView() 替换

html - anchor 标记不能在 div 中间歇性工作

css - 隐藏的滚动条不允许页面滚动。 (CSS)

css - 网格系统仅在 xs 中不在 md 中

reactjs - 在reactJs中使整行可点击