2017-02-05 93 views
3

我有一个ngFor行和我什么时,我的一些细胞点击使其可编辑上点击表格单元格角2使其可编辑

<tr *ngFor="let invoice of invoiceItem.rows"> 
    <td contenteditable='true' (input)="onRowClick($event, invoice.rowId)">{{ invoice.rowName }}</td> 
    <td>{{ invoice.hours}}</td> 
    <td>{{ invoice.price }}</td> 
    <td>{{ invoice.comments }}</td> 
    <td>{{ invoice.total}} </td> 


</tr> 

更过我如何处理新行支持其加入

<button (click)='addRow()'>Add a row</button> 



addRow() { 
     this.invoiceItem.rows.push({ 
      invoiceDetailsId: this.invoiceItem.item.invoiceId, 
      rowName: '', 
      hours: 0, 
      price: 0, 
      comments: '', 
      total: 0, 
      rowId: 
     }) 
    } 
    onRowClick(event, id) { 
     console.log(event.target.outerText, id); 
    } 

我应该为这项任务实施什么?

+0

嘿!我不知道当我取消删除答案时是否收到通知。但请检查,有一个工作解决方案。 ) – Alex

+0

好的检查,tnx – shaharnakash

回答

4

做到这一点这里有一个解决方案,它的工作原理。它可能不漂亮,但它的工作原理。

你的表结构更改为这样的事情:

<tr *ngFor="let invoice of invoiceItem.rows"> 
    <td *ngIf="invoice.rowId == editRowId"> 
     <input type="text" [(ngModel)]="invoice.hours"> 
    </td> 
    <td *ngIf="invoice.rowId !== editRowId" (click)="toggle(invoice.rowId)"> 
     {{invoice.rowId}} 
    </td> 
    <!-- the rest of your fields in same manner --> 
</tr> 

而在你的组件:

editRowId: any; 

toggle(id){ 
    this.editRowId = id; 
} 

这也支持了新行的加入。我想出了一个黑客用于设置ID为新行,像这样:

addRow() { 
    let indexForId = this.invoiceItem.rows.length + 1 
    this.rows.push({ 
    // ..... 
    rowId: indexForId, 
    }) 
} 

你可能会找到一个更好的办法:)

这里的工作plunker

+0

作为评论,如果您愿意使用... https://www.npmjs.com/package/ng2-editable-table – Alex

0

好吧,首先,您将为每个invoice添加一个更多属性,名称为editable: false,然后当循环显示列表时,您将像这样绑定数据;

[contenteditable]="invoice.editable" 

所有td

<td (input)="onRowClick($event, invoice.rowId)"> 
    <div [contenteditable]="invoice.editable ? 'true': 'false'">{{ invoice.rowName }}</div> 
</td> 
+0

我没有得到它@Tiep Phan,我添加了你的saud到行,但得到一个错误 – shaharnakash

+0

有什么错误,请抓住你的截图 –

+3

发生未处理的异常同时处理请求。 异常:调用节点模块失败,出现错误:错误:模板解析错误: 无法绑定到'contenteditable',因为它不是'td'的已知属性。 (“ ] [contenteditable] =”invoice.editable“(click)=”invoice.editable =!invoice.editable“> {{invoice。 rowNam“):DemoTestComponent @ 34:16 无法绑定到'contenteditable',因为它不是'td'的已知属性。 (“ditable”(click)=“invoice.editable =!invoice.editable”> {{invoice.rowName}} – shaharnakash

相关问题