aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-main/angular/auto-colspan.directive.ts
blob: 8da89b668eba635d0ce33afb13d10ee106b3d8e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { AfterViewInit, Directive, ElementRef, Renderer2 } from '@angular/core'

@Directive({
  selector: '[myAutoColspan]'
})
export class AutoColspanDirective implements AfterViewInit {

  constructor (
    private host: ElementRef,
    private renderer: Renderer2
  ) { }

  ngAfterViewInit () {
    const el = this.host.nativeElement as HTMLElement
    const table = el.closest('table')
    if (!table) throw new Error('table element not found')

    const th = table.querySelectorAll('th')

    this.renderer.setAttribute(el, 'colspan', th.length + '')
  }
}