在PrimeNG数据表中编辑该行时启用特定列

时间:2018-06-05 10:51:59

标签: angular angular2-forms primeng primeng-datatable

我正在使用angular2。我有一个数据表。当我点击它时,我能够编辑特定的单元格。但是我也希望最后一列出现在我正在编辑其单元格的特定行中。

假设我要编辑第1行第2个元素。基本上第二个元素是可编辑的,当我点击它但同时,我需要最后一列显示所有3个按钮保存取消删除仅出现在第1行。

<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 个答案:

答案 0 :(得分:0)

您应该将[hidden]="!isEditable"p-column移动到其模板中的div isEditable 属性应该应用于对象所以用isEditable替换row.isEditable。然后,最后一列应如下所示:

<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>

在您的TS组件代码中,将 isEditable 属性添加到每个 tableData 行,并将其初始化为 false

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

最后,将onEditInit事件添加到p-datatable并将true值分配给已编辑行的isEditable属性:

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

请参阅StackBlitz

答案 1 :(得分:0)

这是我的最终解决方案 1)显示最后一栏中的按钮 2)在单击特定行的单个单元格时编辑整行。也 3)在数据表单元格中添加下拉列表

<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文件

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;
          }
        }
    }