2017-04-18 2 views
1

안녕하세요. 행 수가 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를 사용할 수있는 해결되지 않은 각 행의 높이를 가정

+0

는 가정 고정 높이의 _n_ 행을 가지고 있는데, 'rowArray.length * x'를 사용할 때의 문제점은 무엇입니까? x는 픽셀 단위의 높이입니다. 행 높이가 변하면'ElementRef'와'.offsetHeight'를 사용하여 테이블의 높이를 억제하는 것이 더 나을 것입니다. – unitario

+0

Unitaro, 마음에 들지 않으면 코드에 나를 표시 할 수 있습니다. – blackdaemon

+0

unitario, 실제로 행 높이는 고정되어 있지 않습니다. 그것의 변화 – blackdaemon

답변

1

사전들 감사드립니다, 즉 :

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