I am using primeng table cell edit. My code is as follows:
<div class="card">
<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>
</div>
TS:
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%2Fdemo%2Ftable-cell-edit-demo.html
Cell Edit is working fine. I want to highlight with red color those particular cells and respective header in non editable mode if value of any particular cell is changed from it's original value. How can I do that?
In order to colorize the edited cell in your component's HTML, you can follow these steps:
Add a template reference variable
#rowsto the<tr>element to read the rows using@ViewChildren. This will allow you to access the rows in your component.Modify the
<td>elements where you want to track edits by adding an[id]attribute. The[id]attribute should be set as a combination of the property and the rowIndex, for example:[id]="'name'+index".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 ascolorizeEditedCell(rowIndex, fieldName)and pass therowIndexandfieldNameas parameters.Add an
@ViewChildrendecorator to your component class to read the rows using the template reference variable#rows:Inject the
Renderer2into your component's constructor to use it for manipulating the DOM:Define the
colorizeEditedCellfunction in your component to set the background color of the edited cell. This function takes therowIndexandfieldNameas parameters.The above code will iterate through the cells of the specified row and check if the
col.idmatches the providedfieldName + rowIndex. If a match is found, theRenderer2is used to set the background color of the cell to#ececec.Note: Make sure to import the necessary dependencies such as
Renderer2andElementRefin your component file.There might be alternative solutions available, but this approach should effectively address your requirements.