Skip to content

Instantly share code, notes, and snippets.

@chgc
Last active December 30, 2016 07:51
Show Gist options
  • Save chgc/d441384ae38fb34b8e8af59d56d2c44f to your computer and use it in GitHub Desktop.
Save chgc/d441384ae38fb34b8e8af59d56d2c44f to your computer and use it in GitHub Desktop.
Angular2 踩雷系列文(一) - 不同的寫法
<tree-table [data]="treedata">
<div class="header">
<h2>Table Header Display Here</h2>
</div>
<div class="footer">
<p>Footer text</p>
</div>
</tree-table>
.tbrow-even{
background:lightgray;
}
.tbcell {
display:table-cell;
width:50px;
padding:10px;
}
.tbcell.tbcell-name{
width:150px;
}
<div class="tbrow">
<div class="tbcell">{{ level }}</div>
<div class="tbcell tbcell-name">
<span [style.padding-left.px]="level * 10">{{ item.name }}</span>
</div>
<div class="tbcell">
<button>Save</button>
</div>
</div>
<tree-row *ngFor="let subitem of item.submenu" [item]="subitem" [level]="level+1"></tree-row>
import { Component, Input } from '@angular/core';
@Component({
selector: 'tree-row',
templateUrl: './tree-row.component.html',
styleUrls: ['./tree-row.component.css']
})
export class TreeRowComponent {
@Input() item: any;
@Input() level: number;
}
import { Component, Input, OnInit, AfterViewChecked, ElementRef } from '@angular/core';
import { TreeRowComponent } from '../tree-row/tree-row.component';
@Component({
selector: 'tree-table',
template: `
<ng-content select=".header"></ng-content>
<tree-row *ngFor="let item of data" [item]="item" [level]="level"></tree-row>
<ng-content select=".footer"></ng-content>
`
})
export class TreeTableComponent implements OnInit, AfterViewChecked {
@Input() data: any[];
level:number = 0;
constructor(private elementRef: ElementRef) { }
ngOnInit() {
this.level = this.level + 1;
}
ngAfterViewChecked() {
let elements: HTMLElement[] = this.elementRef.nativeElement.querySelectorAll('.tbrow');
if (elements.length > 0) {
for (let i = 0; i < elements.length; i++) {
if (i % 2 == 0) continue;
elements[i].className += " tbrow-even";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment