Vik
Vik

Reputation: 9319

material 2 angular 4 table width does not apply inside mat-card

I am using material 2 angular 4. I have a mat-card and a table inside it. the mat-card has a width set to 700px. and for table inside it to 650px with overflow-x:auto.

However, the table does not obey that and creating a problem for me. this is how it is coded:

<mat-card  style="width:700px;background-color: #F8F8F8; box-shadow : 1px 2px 2px 2px rgba(0, 0, 0, 0.14), 0 3px 1px 2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12)  !important">
<mat-card-title >
  Import
</mat-card-title>
<mat-card-content>
<div fxLayout="column"   fxLayoutAlign="start start">

 <div fxLayout="row" *ngIf="csvRecords.length > 0">
   <div fxLayout="column">
          <table class="fa-table-list-page" style="width:650px;overflow-x: auto;">
                      <tr class="fa-table-header">
                              <td class="fa-table-data-col">From Location</td>
                              <td class="fa-table-data-col">To Location</td>
                              <td class="fa-table-data-col">Product Type</td>
                              <td class="fa-table-data-col">TechnicalRevenue</td>
                              <td class="fa-table-data-col">Revenue(/yr)</td>
                               <td class="fa-table-data-col">Volume (/yr)</td>
                               <td class="fa-table-data-col">Volume Uom</td>
                               <td class="fa-table-data-col">Chargeable Weight (kgs/yr)</td>
                                <td class="fa-table-data-col">Shipmts (/yr)</td>
                                <td class="fa-table-data-col">Existing %</td>
                                <td class="fa-table-data-col">Awarded %</td>
                                <td class="fa-table-data-col">Service</td>
                                <td class="fa-table-data-col">Density</td>
                                <td class="fa-table-data-col">Density Copy Help</td>
                                <td class="fa-table-data-col">Responsible</td>
                                <td class="fa-table-data-col">Sales Branch</td>
                                <td class="fa-table-data-col">Service Name</td>
                                <td class="fa-table-data-col">Position of target warehouse revenue / yr</td>
                                <td class="fa-table-data-col">Position of target transportation revenue / yr</td>
                                <td class="fa-table-data-col">Product Split</td>
                          </tr>
                          <tbody>
                          <tr *ngFor="let rev of csvRecords" class="fa-table-data-row">
                               <td  class="fa-table-data-col">{{rev[0]}}</td>
                               <td  class="fa-table-data-col">{{rev[1]}}</td>
                               <td  class="fa-table-data-col">{{rev[2]}}</td>
                               <td  class="fa-table-data-col">{{rev[3]}}</td>
                               <td  class="fa-table-data-col" >{{rev[4]}}</td>
                               <td  class="fa-table-data-col" >{{rev[5]}}</td>
                               <td  class="fa-table-data-col" >{{rev[6]}}</td>
                               <td  class="fa-table-data-col" >{{rev[7]}}</td>
                               <td  class="fa-table-data-col" >{{rev[8]}}</td>
                               <td  class="fa-table-data-col" >{{rev[9]}}</td>
                               <td  class="fa-table-data-col" >{{rev[10]}}</td>
                               <td  class="fa-table-data-col" >{{rev[11]}}</td>
                               <td  class="fa-table-data-col" >{{rev[12]}}</td>
                               <td  class="fa-table-data-col" >{{rev[13]}}</td>
                               <td  class="fa-table-data-col" >{{rev[14]}}</td>
                               <td  class="fa-table-data-col" >{{rev[15]}}</td>
                               <td  class="fa-table-data-col" >{{rev[16]}}</td>
                               <td  class="fa-table-data-col" >{{rev[17]}}</td>
                               <td  class="fa-table-data-col" >{{rev[18]}}</td>
                               <td  class="fa-table-data-col" >{{rev[19]}}</td>
                          </tr>
                      </tbody>
            </table>
   </div>
  </div>
  </div>
</mat-card-content>
</mat-card>

Upvotes: 1

Views: 4942

Answers (1)

woodykiddy
woodykiddy

Reputation: 6455

You might want to try setting the table layout to fix by adding table-layout: fixed; to your inline style. Otherwise the cell would always size to its content and never overflow.

Also try to set the width and overflow-x on your wrapping div element instead of table element.

<div style="max-width: 650px; overflow-x: auto; white-space: nowrap;">
<table>
  ...
</table>
</div>

For horizontal scroll on table, can refer to this example, http://jsfiddle.net/5WsEt/

Upvotes: 3

Related Questions