I am using prime ng cell edit. My code is as follows.
<p-table [value]="products" dataKey="id" [tableStyle]="{ 'min-width': '50rem' }">
<ng-template pTemplate="header">
<tr>
<th style="width:25%">Code</th>
<th style="width:25%">Name</th>
<th style="width:25%">Inventory Status</th>
<th style="width:25%">Price</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-product let-editing="editing">
<tr>
<td [pEditableColumn]="product.code" pEditableColumnField="code">
<p-cellEditor>
<ng-template pTemplate="input">
<input pInputText type="text" [(ngModel)]="product.code" />
</ng-template>
<ng-template pTemplate="output">
{{ product.code }}
</ng-template>
</p-cellEditor>
</td>
<td [pEditableColumn]="product.name" pEditableColumnField="name">
<p-cellEditor>
<ng-template pTemplate="input">
<input pInputText type="text" [(ngModel)]="product.name" required />
</ng-template>
<ng-template pTemplate="output">
{{ product.name }}
</ng-template>
</p-cellEditor>
</td>
<td [pEditableColumn]="product.inventoryStatus" pEditableColumnField="inventoryStatus">
<p-cellEditor>
<ng-template pTemplate="input">
<input pInputText [(ngModel)]="product.inventoryStatus" />
</ng-template>
<ng-template pTemplate="output">
{{ product.inventoryStatus }}
</ng-template>
</p-cellEditor>
</td>
<td [pEditableColumn]="product.price" pEditableColumnField="price">
<p-cellEditor>
<ng-template pTemplate="input">
<input pInputText type="text" [(ngModel)]="product.price" />
</ng-template>
<ng-template pTemplate="output">
{{ product.price | currency: 'USD' }}
</ng-template>
</p-cellEditor>
</td>
</tr>
</ng-template>
</p-table>
TS:
import { Component, OnInit } from '@angular/core';
import { Product } from '../../domain/product';
import { ProductService } from '../../service/productservice';
@Component({
selector: 'table-cell-edit-demo',
templateUrl: 'table-cell-edit-demo.html'
})
export class TableCellEditDemo implements OnInit {
products!: Product[];
constructor(private productService: ProductService) {}
ngOnInit() {
this.productService.getProductsMini().then((data) => {
this.products = data;
});
}
}
Stackblitz :
https://stackblitz.com/run?file=src%2Fapp%2Fapp.component.html
Cell edit is working fine. I just want to highlight that particular header and row with red background color which is getting edited currently. How can I do that?
in order to colorize the row when edit a cell: 1- Add a template reference variable
#rowsto the<tr>element inside thepTemplate="body"to read the rows using@ViewChildren. This will allow you to access the rows in your component.2- Add a template reference variable
#tableHeaderto the<tr>element inside thepTemplate="header"to read the row using@ViewChild. This will allow you to access the header row in your component.3- Modify the
<th>elements where you want to track edits by adding an [id] attribute.set Id for example: [id]="'name'". name of thethfield.4- Inside the
<input>element that represents the editable field, add(ngModelChange)to listen for changes in the input value. When the value changes, call a method such ascolorizeRow(rowIndex, headerId)and pass therowIndexandheaderIdas parameters.5-Add an
@ViewChildrendecorator to your component class to read the rows using the template reference variable#rows:6-Add an
@ViewChilddecorator to your component class to read the header row using the template reference variable#tableHeader:7-Inject the
Renderer2into your component's constructor to use it for manipulating the DOM:8-Define the
colorizeRowfunction in your component to set the background color of the edited row and header. This function takes the rowIndex and headerId as parameters.