1
votes

I am using angular2. I have a datatable. I am able to edit particular cell when I click on it. But I also want the last column to appear for that particular row whose cell I am editing.

Suppose I want to edit the 1st row 2nd element. Basically 2nd element is editable when I click it but at the same moment, I need the last column to show all 3 buttons save, cancel and delete to appear only for 1st row.

<p-dataTable [value]="tableData" editable="true" editMode="cell">
                            <p-column field="id" header="CUSTOMER*" [editable]="true"></p-column>
                            <p-column field="category" header="CATEGORY" [editable]="true"></p-column>
                            <p-column field="featureName" header="FEATURE NAME" [sortable]="true" [editable]="true"></p-column>
                            <p-column field="devEstimate" header="DEV ESTIMATE" [editable]="true"></p-column>
                            <p-column field="supportEstimate" header="SUPPORT ESTIMATE" [editable]="true"></p-column>
                            <p-column field="started" header="STARTED" [editable]="true"></p-column>
                            <p-column field="release" header="RELEASE/TARGET" [sortable]="true" [editable]="true"></p-column>
                            <p-column field="notes" header="NOTES" [editable]="true"></p-column>

                            <p-column [hidden]="!isEditable"> 
                                    <template let-col let-row="rowData" let-index="rowIndex"  pTemplate="body"  >
                                        <div>
                                            <button pButton type="button" label="Save"></button>
                                            <button pButton type="button" label="Cancel"></button>
                                            <button pButton type="button" label="Delete"></button>
                                        </div>  
                                    </template> 
                            </p-column>

                    </p-dataTable>
2
Can you add the code some where like jsfiddle.net or some other placeArun
DataTable is deprecated, use TurboTable instead..user4676340

2 Answers

0
votes

You should move [hidden]="!isEditable" from the p-column to the div inside its template and isEditable property should apply to a row object so replace isEditable with row.isEditable. Then, last column should look like :

<p-column>
    <ng-template let-col let-row="rowData" let-index="rowIndex" pTemplate="body">
        <div [hidden]="!row.isEditable">
            <button pButton type="button" label="Save"></button>
            <button pButton type="button" label="Cancel"></button>
            <button pButton type="button" label="Delete"></button>
        </div>
    </ng-template>
</p-column>

In your TS component code, add that isEditable property to each of your tableData rows and initialize it to false :

this.tableData.forEach(function (row) {
  row.isEditable = false;
});

Finally, add the onEditInit event to your p-datatable and assign true value to isEditable property of the row edited :

onEditInit(event) {
  this.disableAllRowsEdition();
  event.data.isEditable = true;
}

See StackBlitz

0
votes

This is the final solution i had for 1) Showing buttons in last column 2) Editing whole row when clicking on a single cell of a particular row & also 3) to add Dropdown in data table cell.

<p-dataTable [value]="tableOne" (onRowClick)="onSelectFeature($event)" [editable]="true">

                            <p-column field="customer" header="CUSTOMER*" [sortable]="true" [style]="{'width':'15%'}">
                                <template let-col let-row="rowData" let-index="rowIndex" pTemplate="body">
                                    <span *ngIf="!row.isEditable">{{row.customer}}</span>
                                    <span *ngIf="row.isEditable"><p-dropdown [options]="field_customer" [autoWidth]="false" appendTo="body" [placeholder]="row.customer"></p-dropdown></span>
                                </template>
                            </p-column>

<p-column field="notes" header="NOTES">
                                <template let-col let-row="rowData" let-index="rowIndex" pTemplate="body">
                        <span *ngIf="!row.isEditable">{{row.notes}}</span>
                                    <span *ngIf="row.isEditable"><input type="text" pInputText [(ngModel)]="row.notes" maxlength="64"></span>
                                </template>
                            </p-column>
                            <p-column>      
                                <template let-row="rowData" let-index="rowIndex" pTemplate="body">
                                    <div class="display" *ngIf="row.isEditable">
                                        <button pButton type="button" class="btn margin-left-3p float-right" label="Save"></button>
                                        <button pButton type="button" class="btn margin-left-3p float-right" label="Cancel"></button>
                                        <button pButton type="button" class="btn float-right" label="Delete"></button>
                                    </div>
                                </template>
                            </p-column>   
                    </p-dataTable>

component.ts file

isEditable:boolean;

ngOnInit(){
        this.isEditable=false;  
    }
constructor(private router: Router,private datePipe: DatePipe){
this.tableOne = [
            {customer:'PRODUCT',category:'Product', featureName:'OSS', devEstimate:'$12345.00', supportEstimate:'$',started:'TRUE',release:'SAIL 2.28',notes:'Funded by IMS'},
            {customer:'TECHNOLOGY',category:'Product', featureName:'QWE', devEstimate:'$12345.00', supportEstimate:'$',started:'TRUE',release:'SAIL 2.28',notes:'Funded by IMS'}];
}
onSelectFeature(e)
    {
        this.CloseAllEditable(this.tableOne);
        this.CloseAllEditable(this.tableTwo);
        e.data.isEditable=true;
    }
CloseAllEditable(data){
        for(let item of data)
        {
          if(item.isEditable)
          {
            item.isEditable = false;
          }
        }
    }