3

I'm trying to make an Angular component to both display data and receive data. I made a mat-table with input type form fields and the regular data display with {{element.value}}. And I set up the width of each column diffrently like this :

.mat-column-v{
  width: 32%!important;
}

.mat-column-w{
  width: 17%!important;
}

.mat-column-x{
  width: 17%!important;
}

.mat-column-y{
  width: 17%!important;
}

.mat-column-z{
  width: 17%!important;
}

My problem is that when I use the input mode the width of the columns isnt set correctly. On the other hand the output mode works correctly as show in this picture: two tables side by side

I had to cover the information but on the right is the input mode with the 1st column having a kind of random width and on the left the output mode where the width is set correctly.

My component's code :

  <table mat-table [dataSource]="data">
    <!-- v Column -->
    <ng-container matColumnDef="v">
      <th mat-header-cell *matHeaderCellDef> v </th>
      <td mat-cell *matCellDef="let element"> {{element.v}} </td>
    </ng-container>

    <!-- w Column -->
    <ng-container matColumnDef="w">
      <th mat-header-cell *matHeaderCellDef> w </th>
      <td mat-cell *matCellDef="let element">
        <ng-container *ngIf="type == 'input'">
           <mat-form-field>
            <input matInput [value]="element.w" [(ngModel)]="element.w">
           </mat-form-field>
        </ng-container>
       <ng-container *ngIf="type == 'output'"> {{element.w}} </ng-container>
     </td>
    </ng-container>

    <!-- x Column -->
    <ng-container matColumnDef="x">
      <th mat-header-cell *matHeaderCellDef> x </th>
      <td mat-cell *matCellDef="let element">
         <ng-container *ngIf="type == 'input'">
           <mat-form-field>
             <input matInput [value]="element.x" [(ngModel)]="element.x">
           </mat-form-field>
         </ng-container>
        <ng-container *ngIf="type == 'output'"> {{element.x}} </ng-container>
      </td>
    </ng-container>

    <!-- y Column -->
    <ng-container matColumnDef="y">
      <th mat-header-cell *matHeaderCellDef> y </th>
      <td mat-cell *matCellDef="let element">
         <ng-container *ngIf="type == 'input'">
           <mat-form-field>
             <input matInput [value]="element.y" [(ngModel)]="element.y">
           </mat-form-field>
         </ng-container>
        <ng-container *ngIf="type == 'output'"> {{element.y}} </ng-container>
      </td>
    </ng-container>

    <!-- z Column -->
    <ng-container matColumnDef="z">
      <th mat-header-cell *matHeaderCellDef> z </th>
      <td mat-cell *matCellDef="let element">
         <ng-container *ngIf="type == 'input'">
           <mat-form-field>
             <input matInput [value]="element.z" [(ngModel)]="element.z">
           </mat-form-field>
         </ng-container>
        <ng-container *ngIf="type == 'output'"> {{element.z}} </ng-container>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="heads"></tr>
    <tr mat-row *matRowDef="let row; columns: heads;"></tr>
  </table>

My component's CSS :

table {
  width: 100%;
}

.mat-form-field {
  font-size: 14px;
  width: 90%;
}

.mat-column-v{
  width: 32%!important;
}

.mat-column-w{
  width: 17%!important;
}

.mat-column-x{
  width: 17%!important;
}

.mat-column-y{
  width: 17%!important;
}

.mat-column-z{
  width: 17%!important;
}

How do I set the 1st column size in the table containing inputs just like the other one ?

Akber Iqbal
  • 14,487
  • 12
  • 48
  • 70
AnotherBrick
  • 153
  • 3
  • 6
  • 14

1 Answers1

5

You can check the complete demo here

I have added a toggle on the top, so that you can check the exact impact which you are aiming for...

we had to over-write the default width for the input which was set to 180px... we did that using the css below:

::ng-deep .mat-column-w div.mat-form-field-infix,
::ng-deep .mat-column-x div.mat-form-field-infix,
::ng-deep .mat-column-y div.mat-form-field-infix,
::ng-deep .mat-column-z div.mat-form-field-infix { width: 17% !important; }
Akber Iqbal
  • 14,487
  • 12
  • 48
  • 70
  • 1
    I am glad it helped, would appreciate a vote up on the answer also :) – Akber Iqbal Apr 03 '19 at 11:36
  • done, but I don't have 15 rep yet ;d . I appreciate your help, thanks a lot again! – AnotherBrick Apr 03 '19 at 13:04
  • 1
    An up vote to your question would help get you to 15 :d – Akber Iqbal Apr 03 '19 at 13:40
  • @ Akbar is there any way to define same size for the column even screen size changes? https://stackoverflow.com/questions/65296518/how-to-support-same-column-size-when-screen-size-reducing-in-angular-material-ta – app Dec 14 '20 at 23:13