3
votes

HERE'S A STACKBLITZ FOR THIS PROBLEM:

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

I have to access the rowIndex or row data where I have used ngFor, I tried using rowIndex and went through the docs but unable to find out how it could work. Any help would be great thank. Let me know if any further information is required. Here's the code:

<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
Are you still facing the issue? Just to understand your question, where exactly do you need to access rowIndex?wentjun
@wentjun thanks for reaching out. Yes I am still having this issue. I need to access it where I have used ngFor in ngx-datatable-column. You can see that in the code too, I have made it a comment.Immad Hamid
And your getMonths() method is unable to read rowIndex?wentjun
yes, I am unable to send rowIndex in the method. It shows undefinedImmad Hamid
You can use index of array(the array will be returned from getMonths) into ngfor. An index there is when you loop into array!!!Morteza Ebrahimi

3 Answers

2
votes

Base on your 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>
...

I hope this helps you.

1
votes

It's a good question as I wasn't able to find any relevant solution to achieve this, but instead try to solve it in a different way.

I think you wouldn't be able to access the rowIndex where you are trying to access it. Here's a stackblitz which surely solves your problem. It's not the best way of doing it but it will solve the problem.

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

1
votes

After (insert swear word here)ing around with this stupid component for many hours, it turns out that on the methods that are bound to the ngx-datatable root tag, such as [rowClass]="getRowClass", the context of the function call, i.e. "this", is the datatable itself, not the component. Thus if you look for this.rowIndex while in the body of your getRowClass function you may find this.rowIndex. But here's the rub: Your TypeScript won't compile unless you add a local rowIndex property to your component.

I sure as heck hope this helps someone after all the searching and experimenting I did. I was in the process of using the method Morteza posted above/below but when I struggled to duplicate his/her results, I found out about this context switch at runtime. The only thing left is figuring out whether I want to keep using such a goofy component or look at something else like maybe PrimeNG...or good old Datatables.js.