Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过循环Javascript计算总高度_Javascript_Angular_Typescript_Ag Grid - Fatal编程技术网

通过循环Javascript计算总高度

通过循环Javascript计算总高度,javascript,angular,typescript,ag-grid,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" &

嗨,我有一张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或每行的height之和,并将其分配给变量
totalRowHeight
。请告诉我。提前感谢各位

假设每行的高度不固定(在这种情况下,一个简单的
数组frows.length*x
就足够了),然后您可以使用
ElementRef
offsetHeight
获得高度,即:

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;
   } 
}
从'@angular/core'导入{Component,ElementRef};
@组成部分({
选择器:'表示例',
模板:“…”
})
导出类TableExampleComponent{
总行高:数字;
构造函数(私有elRef:ElementRef){}
//确保已使用ngAfterViewInit完全渲染视图
ngAfterViewInit(){
让totalRowHeight=this.elRef.nativeElement.offsetHeight;
} 
}

假设有n行具有固定高度,仅使用
rowArray.length*x
有什么问题,其中x是以像素为单位的高度?如果行高不同,您最好使用
ElementRef
.offsetHeight
来确定表的高度。如果您不介意unitario,Unitaro可以在代码中显示我,实际上行高不是固定的。它改变了你的问题解决了吗?没有。不起作用