Inline text editing with Mat-table:

16,551

Solution 1

inside your matColumnDef you should have a <th></th> and a <td></td>, simply add input to the <td></td> with reactive forms.


if you wish it to be practically the same as the material example, you could create a position absolute element inside a positioned-relative element inside the that would propmt on click, receive and input, and close after some logic, to update the formControl.

Still, you'd need formControls, and to edit the matColumnDef.

example taken from the link of the github request

      <ng-container matColumnDef="time">
        <th mat-header-cell *matHeaderCellDef> time </th>
        <td mat-cell *matCellDef="let element; let i = index" [formGroupName]="i"> 
            <input type="text" formControlName="time" placeholder="Time"> 
        </td>
      </ng-container>

Solution 2

The git hub issue you mentioned has a few implementations people have come up with.

You can get inspiration from this:

https://stackblitz.com/edit/material2-beta12-es19ub?file=app%2Fapp.component.html

or this: https://stackblitz.com/edit/angular-g5u7cy?file=app%2Ftable-editing-example.html

I would go through the github issue to see the different options people have come up with.

Share:
16,551
Flash
Author by

Flash

Updated on June 04, 2022

Comments

  • Flash
    Flash about 2 years

    Does anyone have a good solution to adding inline text editing to a column where you can edit and save and re-edit with mat table like in this example: https://material.io/design/components/data-tables.html#behavior it look like it's still an open issue on gitHub: https://github.com/angular/material2/issues/5982

    enter image description here

  • Flash
    Flash over 5 years
    Sorry I was being a little to generic. I get that part but how do you make it editable and savable. Like when you are editing the input displays but when your done it just shows the comment
  • Francisco Santorelli
    Francisco Santorelli over 5 years
    you could add (keydown.enter)="onUpdate()" to update on enter in the input... or add (keyup) if you want editting as you type, or (blur) to edit on focus-lose, or (change) to edit on input change... etc... try this and tell me if it works! edit: onUpdate() should call formControl.setValue() and it should get the input directly from the DOM or from the formcontrol. Either way is fine
  • Flash
    Flash over 5 years
    Thanks I will git it a try. Do you have a working example like on stackblitz?
  • Francisco Santorelli
    Francisco Santorelli over 5 years
    sadly my working examples are on work-related projects, which I don't have at hand and cannot share publicly (I would share just the important parts though)...
  • kushal Baldev
    kushal Baldev almost 4 years
    Here is the working example medium.com/@nsrathore/…