Ngx数据表访问rowIndex的列

时间:2019-03-25 07:41:04

标签: angular ngx-datatable

  

此问题的掩饰:

https://stackblitz.com/edit/angular-7dgk8e

我必须在使用ngFor的地方访问rowIndex或行数据,我尝试使用rowIndex并浏览了文档,但无法找到其工作方式。任何帮助将不胜感激。让我知道是否需要进一步的信息。这是代码:

<ngx-datatable
    style="height: 450px; cursor: pointer;"
    class="material"
    [rows]="rows"
    [rowHeight]="70"
    [footerHeight]="50"
    columnMode="force"
    [scrollbarV]="true"
    [scrollbarH]="true">

    <!-- months col -->
    <ngx-datatable-column 
        *ngFor="let month of getMonths(rows, rowIndex)" // THIS IS THE PLACE WHERE I HAVE TO ACCESS THE ROWINDEX OR ROW BUT UNABLE TO ACCESS IT 
        [name]="month.name"
        [width]="465">

        <ng-template let-value="value" let-row="row" let-rowIndex="rowIndex" ngx-datatable-cell-template>

            <span *ngIf="!month.detail; then avgTemp; else ratingTemp;"></span>
            <!-- average score -->
            <ng-template #avgTemp>{{ month.value | json }} MV</ng-template>
            <!-- monthly rating -->
            <ng-template #ratingTemp>
            <span *ngIf="month.detail.rating.isNA === 'Y'; then isNaTemp; else notNaTemp"></span>
            <!-- N/A -->
            <ng-template #isNaTemp>N/A</ng-template>
            <!-- Non-N/A -->
            <ng-template #notNaTemp>
                <span *ngIf="month.detail.rating.hrRating !== null; then hrRatTemp; else otherRatTemp"></span>
                <!-- Hr Rating -->
                <ng-template #hrRatTemp>{{ month.detail.rating.hrRating }} HR</ng-template>

                <ng-template #otherRatTemp>
                    <span *ngIf="month.detail.rating.lmRating !== null; then lmRatTemp; else otherRatTemp"></span>
                    <!-- Lm Rating -->
                    <ng-template #lmRatTemp>{{ month.detail.rating.lmRating }} LM</ng-template>

                    <ng-template #otherRatTemp>
                        <span *ngIf="month.detail.rating.empRating !== null; then empRatTemp; else zeroTemp"></span>
                        <!-- Emp Rating -->
                        <!-- <ng-template #empRatTemp>{{ month.detail.rating.empRating }} Emp</ng-template> -->
                        <ng-template #empRatTemp>{{ row.months | json }} Emp</ng-template>
                        <ng-template #zeroTemp>
                        <span *ngIf="(rowIndex + 1) != rows.length">0</span>
                        </ng-template>
                    </ng-template>
                </ng-template>
            </ng-template> <!-- Non-N/A -->
            </ng-template> <!-- monthly rating -->

        </ng-template>
    </ngx-datatable-column>

</ngx-datatable>

3 个答案:

答案 0 :(得分:1)

这是一个好问题,因为我无法找到任何相关的解决方案来实现此目的,而是尝试以其他方式解决它。

我认为您将无法在尝试访问它的地方访问rowIndex。这是一个肯定能解决您问题的堆叠式闪电战。这不是最好的方法,但可以解决问题。

https://stackblitz.com/edit/angular-hpm8k9

答案 1 :(得分:0)

基于您的堆叠闪电战:

app.component.ts

@ViewChild(DatatableComponent) table: DatatableComponent;

public getRowIndex(row: any): number {
    return this.table.bodyComponent.getRowIndex(row); // row being data object passed into the template
}

app.component.html

...
<ngx-datatable  ....  #table> 
   <ngx-datatable-column>
        {{ getRowIndex(row) }}
   </ngx-datatable-column>
...

希望对您有帮助。

答案 2 :(得分:0)

在这个笨拙的组件上(在这里插入咒骂)几个小时之后,事实证明,绑定到ngx-datatable根标记的方法,例如[rowClass] =“ getRowClass”,上下文函数调用(即“ this”)的名称是数据表本身,而不是组件。因此,如果在getRowClass函数的主体中查找this.rowIndex,则可能会找到this.rowIndex。但这很麻烦:除非将本地rowIndex属性添加到组件,否则TypeScript不会编译。

我确实希望,这对我所做的所有搜索和实验都可以有所帮助。我正在使用上面/下面发布的Morteza方法,但是当我难以复制他/她的结果时,我在运行时发现了此上下文切换。剩下的唯一一件事就是弄清楚我是否要继续使用这种愚蠢的组件,还是想看看其他东西,例如PrimeNG ...或旧的Datatables.js。

相关问题