我正在开发一个网格组件,它主要由三部分组成,一些GridComponent组件接收数组数据源,一个RowItem指令只负责为一行创建上下文,以及一个ColumnComponent,它是列的内容的定义。
简而言之,组件的用法如下所示:
<my-grid [data-source]="dataSource">
<ng-container *rowItem="let item">
<my-column column-header="Person">
{{item.name}}
</my-column>
<my-column column-header="Age">
{{item.age}}
</my-column>
<my-column column-header="Car">
{{item.car}}
</my-column>
</ng-container>
</my-grid>现在,我的列定义如下:
<ng-container *ngIf="someConditionHere">
<ng-content></ng-content>
</ng-container>它将收集开发人员指定的内容并传递到网格,网格将负责呈现完整的控件,如下面的模板所示。
<table>
<thead>
<tr>
<th *ngFor="let col of columns">
{{col.columnHeader}}
</th>
</tr>
</thead>
<tbody>
<tr class="core-grid-row" *ngFor="let row of dataSource">
<ng-template [ngTemplateOutlet]="rowItem" [ngOutletContext]="{$implicit: row}"></ng-template>
</tr>
</tbody>
</table>我现在遇到的问题与列名的呈现有关。我正在尝试使用下面的ContentChildren收集名称
@ContentChildren(ColumnComponent)
public columns: QueryList<ColumnComponent>;但是,由于网格中有*ngFor指令,所以我的ContentChildren查询将收集列乘以行数,而不是只收集第一行。有什么方法可以让我在不受*ngFor的副作用的情况下以干净整洁的方式收集列名吗?
耽误您时间,实在对不起!
发布于 2017-07-20 16:50:24
您可以做的是标记第一行中的列。
<tr class="core-grid-row" *ngFor="let row of dataSource; let i = index">
<ng-template [ngTemplateOutlet]="rowItem" [ngOutletContext]="{$implicit: row, 'i: i}"></ng-template>
</tr>(我不太确定用ngOutletContext传递索引的代码是什么,.也许这有点不一样)
然后,可以使用索引标记第一列:
<ng-container *ngIf="i == 0 && someConditionHere" #headerColumn>
<ng-content></ng-content>
</ng-container>
<ng-container *ngIf="i > 0 && someConditionHere">
<ng-content></ng-content>
</ng-container>然后,您应该能够通过以下方法获取第一行列:
@ContentChildren(#headerColumn)
public columns: QueryList<ColumnComponent>;发布于 2017-07-21 12:04:04
通过将容器包装在my-row组件中而不是ng-container中,我设法以某种方式修复了这个问题。像这样,我可以直接访问组件。但是,仍然使用该指令来创建上下文变量。
https://stackoverflow.com/questions/45219183
复制相似问题