I have a page, which is of only one user thing. But now i have to create a table and i need to fetch data, so that in the table i must have edit and delete button in inline. If i click on edit button then i must be able to edit the whole row, delete means entire row must be deleted. And when i click on New button, it must create me a new row with edit and delete actions. i need to perform this using ng-prime itself. So, can anyone help me with the links, or any demo.
you can use [editable]="true"
From Official Documentation reference:
Incell editing is enabled by setting editable property true both on datatable and columns. Clicking a cell switches to edit mode and hitting enter key or clicking another cell switches it back to view mode.
Component:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
data: any = [];
delRow;
editRow(row) {
this.data.filter(row => row.isEditable).map(r => { r.isEditable = false; return r })
row.isEditable = true;
}
save(row){
row.isEditable = false
}
addNew(){
this.data.push({
name: '',
value: ''
})
}
delete(row){
console.log(row);
this.delRow = this.data.indexOf(row);
this.data.splice(this.delRow,1);
console.log(this.data);
}
getData() {
this.data = [
{name: "Name1", value: "value1"},
{name: "Name2", value: "value2"},
{name: "Name3", value: "value3"},
{name: "Name4", value: "value4"}
];
this.data.map(row => {
row.isEditable = false;
});
};
}
HTML:
<button (click)="getData()">Load Data</button>
<button (click)="addNew()">Add New</button>
<p-dataTable [immutable]="false" [value]="data" [editable]="true">
<p-column field="name" header="Name">
<ng-template let-row="rowData" pTemplate="body">
<div *ngIf="!row.isEditable">{{row.name}}</div>
<div *ngIf="row.isEditable">
<input type="text" [(ngModel)]="row.name">
</div>
</ng-template>
</p-column>
<p-column field="value" header="Value">
<ng-template let-row="rowData" pTemplate="body">
<div *ngIf="!row.isEditable">{{row.value}}</div>
<div *ngIf="row.isEditable">
<input type="text" [(ngModel)]="row.value">
</div>
</ng-template></p-column>
<p-column field="" header="" [style]="{'text-align':'center'}">
<ng-template let-row="rowData" pTemplate="body">
<button (click)="editRow(row)">Edit</button>
</ng-template>
</p-column>
<p-column field="" header="" [style]="{'text-align':'center'}">
<ng-template let-row="rowData" pTemplate="body">
<button (click)="save(row)">Save</button>
</ng-template>
</p-column>
<p-column field="" header="" [style]="{'text-align':'center'}">
<ng-template let-row="rowData" pTemplate="body">
<button (click)="delete(row)">Delete</button>
</ng-template>
</p-column>
</p-dataTable>