0
<ng-container cdkColumnDef="weight">
  <th cdk-header-cell *cdkHeaderCellDef> Weight </th>
  <td cdk-cell *cdkCellDef="let element"> {{element.weight}} </td>
</ng-container>

我想像滚动总重量一样显示 {{element.weight} + element of previous row.weight}。语法是什么?工作样本源取自 这里

4

1 回答 1

1

我已经通过自定义逻辑实现了滚动重量总计,如果您有任何疑问,请通过并评论!

结果

堆栈闪电战链接

在 html 中

    <!-- Weight Column -->
<ng-container cdkColumnDef="weight">
    <th cdk-header-cell *cdkHeaderCellDef> Weight </th>
    <td cdk-cell *cdkCellDef="let element"> {{getElementWeight(element)}} </td>
    <!--  <td cdk-cell *cdkCellDef="let element"> {{element.weight}} </td> -->
</ng-container>

在 ts

declare global {
interface Array < T > {
    getSummedWeight(): any;
    containsElement(e: any): any;
     }
  }

    custom:any = [];
getElementWeight(e) {
    if (this.custom.containsElement(e)) {
        this.custom = [];
        console.log("########contains#####")
    }
    this.custom.push({
        'position': e.position,
        'name': e.name,
        'weight': e.weight,
        'newweight': e.weight + (this.custom.length == 0 ? 0 : this.custom.getSummedWeight())
    })
    console.log(this.custom)
    return this.custom.length == 0 ? 0 : this.custom[this.custom.length - 1].newweight

}


  if (!Array.prototype.getSummedWeight) {
Array.prototype.getSummedWeight = function(): any {
    let totalWeight = this.length == 1 ? this[this.length - 1].weight :
        this[this.length - 1].weight;
    return totalWeight;
    }
}
  if (!Array.prototype.containsElement) {
Array.prototype.containsElement = function(e): any {
    for (let i = 0; i < this.length; i++) {
        if (this[i].position === e.position)
            return true;
    }
    return false;
   }
 } 
于 2018-09-26T16:41:54.137 回答