【问题标题】:Ngx-Datatable Access rowIndex in columnsNgx-Datatable 访问列中的行索引
【发布时间】:2019-03-25 07:41:04
【问题描述】:

这里有一个针对这个问题的 StackblITZ:

https://dynamic-ng-grid.stackblitz.io

我必须访问我使用过 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>

【问题讨论】:

  • 您还面临这个问题吗?只是为了理解您的问题,您究竟需要在哪里访问rowIndex
  • @wentjun 感谢您与我们联系。是的,我仍然有这个问题。我需要在 ngx-datatable-column 中使用 ngFor 的地方访问它。您也可以在代码中看到,我已将其作为注释。
  • 而您的getMonths() 方法无法读取rowIndex
  • 是的,我无法在方法中发送 rowIndex。它显示未定义
  • 您可以使用数组的索引(数组将从getMonths返回)到ngfor。循环进入数组时会有一个索引!!!

标签: angular ngx-datatable


【解决方案1】:

基于你的 stackblitz:

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>
...

希望对你有帮助。

【讨论】:

  • 感谢 Morteza 的回答,我一直在 stackblitz 上尝试不同的东西,我以为人们没有看它。请立即检查,您将能够看到我想要实现的目标
  • 请检查 HTML 中的第 13 行和 TS 文件中的第 15 行
【解决方案2】:

在(在这里插入脏话)用这个愚蠢的组件玩了几个小时之后,事实证明,在绑定到 ngx-datatable 根标签的方法上,例如 [rowClass]="getRowClass",上下文函数调用,即“this”,是数据表本身,而不是组件。因此,如果您在 getRowClass 函数的主体中查找 this.rowIndex,您可能会找到 this.rowIndex。但问题是:除非您向组件添加本地 rowIndex 属性,否则您的 TypeScript 将无法编译。

在我进行了所有搜索和实验之后,我当然希望这对某人有所帮助。我正在使用 Morteza 在上面/下面发布的方法,但是当我努力复制他/她的结果时,我发现了运行时的上下文切换。剩下的唯一事情就是弄清楚我是想继续使用这样一个愚蠢的组件还是看看其他的东西,比如 PrimeNG……或者好的旧 Datatables.js。

【讨论】:

    【解决方案3】:

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

    我认为您将无法访问您尝试访问它的 rowIndex。这是一个stackblitz,它肯定可以解决您的问题。这不是最好的方法,但它会解决问题。

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

    【讨论】:

    • 感谢您的回答,我会检查一下,并会回复您。
    • 这行得通。如果我无法在我的方法中获取 rowIndex,这实际上是我想到的最后一件事。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2019-10-22
    • 2017-11-15
    • 2019-03-18
    • 1970-01-01
    相关资源
    最近更新 更多