javascript - 通过循环Javascript计算总高度

标签 javascript angular typescript ag-grid

嗨,我有一个包含 n 行的表。我需要根据行高计算总高度。以下是 Typescript 代码:

private _getRowHeight(_params: any): number {
    let totalRowHeight: number;
    let maxHeight: number = 100;
    let contentLength: number = 50;
    for (let i in _params.data) {
        if (i != "time" && _params.data[i] instanceof Array) {
            let tempHeight: number = 100 * _params.data[i].length;
            if (tempHeight > maxHeight) maxHeight = tempHeight;
            for (let x in _params.data[i]){
                let tempContentHeight: number = 20 * _params.data[i][x].content.length;
                if (tempContentHeight > contentLength) contentLength = tempContentHeight;
            }
        }
    }
    maxHeight += contentLength;
    return maxHeight;
}

基本上 maxHeight 是每行的高度。如何计算每个 maxHeight 或每行高度的总和并将其分配给变量 totalRowHeight。请给我建议。提前感谢大家

最佳答案

假设每行的高度不固定(在这种情况下,一个简单的 arrayOfRows.length * x 就足够了),那么您可以使用 ElementRefoffsetHeight 获取高度,即:

import { Component, ElementRef } from '@angular/core';

@Component({
   selector: 'table-example',
   template: '<table-example> ... </table-example>'
})
export class TableExampleComponent {

   totalRowHeight: number;

   constructor(private elRef: ElementRef) {}

   // make sure the view has been fully rendered with ngAfterViewInit

   ngAfterViewInit() { 
      let totalRowHeight = this.elRef.nativeElement.offsetHeight;
   } 
}

关于javascript - 通过循环Javascript计算总高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43469184/

相关文章:

在 Chrome 中调试从 TypeScript 生成的动态加载的 Javascript

javascript - Angular 错误this.mymethod is not a function

javascript - json的ajax响应限制是多少?

javascript - 采集字段中的 meteor 计算值

angular - 当 http 解析时执行操作并在需要时抛出错误

angular - 如何使用 Angular Material 在工具提示中选择垫子选择值

javascript - typescript 中的 fs/promises api 无法在 javascript 中正确编译

Javascript SOAP 响应解析元素

javascript - ReactJS Modal 通过 NPM 导入的 jquery 隐藏

angularjs - 如何在 Angular 2 中使用 amcharts 的 ammaps?