Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the way to add horizontal scroll on material-ui table with many columns?

I follow this table example for react material-ui framework and I am looking for a possibility to make my table scrollable horizontally when I have a lot of columns.

For instance I have many columns that are squeezed to fit page width, hence their content is shortened.

I think it's described in material-ui spec by link Display the full column content and enable horizontal scrolling in the table container.

So I wonder if it's possible in material-ui.

like image 361
Niakrais Avatar asked May 10 '17 21:05

Niakrais


People also ask

How do I make a horizontal data table scrollable?

DataTables has the ability to show tables with horizontal scrolling, which is very useful for when you have a wide table, but want to constrain it to a limited horizontal display area. To enable x-scrolling simply set the scrollX parameter to be true .

How do I add a scroll horizontally?

Horizontal scrolling can be achieved by clicking and dragging a horizontal scroll bar, swiping sideways on a desktop trackpad or trackpad mouse, pressing left and right arrow keys, or swiping sideways with one's finger on a touchscreen.

How do I overflow horizontal scroll?

Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line. Here the scroll div will be horizontally scrollable.


1 Answers

Only overflow-x is not enough. Adding 'flex-basis' and 'flex-shrink' properties to the columns is also required. In below example, adding flex-basis of 35% to 3 columns grows the table width to 105% and 'flex-shrink: 0' ensures that the columns will not be shrinked when the width is not enough:

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>

app.component.css

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

mat-header-cell, mat-cell {
    /* 
    flex-shrink: 0;
    flex-basis: 35%;
    */
    flex: 0 0 35%;
}
like image 82
Learning Avatar answered Sep 30 '22 08:09

Learning