Search code examples
angularangular-ng-if

*Ngif for all columns except last 2 Angular 4


I'm trying to use *Ngif for all columns except last 2, I've been trying to do it for hours but nothings working so far mes variable has the number of the current month. listaMonthsNames has the same number of elements as the current month check variable is a boolean set to false

<thead>
    <tr>
      <th></th>
      <ng-container *ngFor="let month of listaMonthsNames let idx=index">
      <th class="col-{{idx}}" *ngIf="check">{{month}}</th>
      </ng-container>
    </tr>
</thead>

I also got a checkbox when I click on it shows all months of the array listaMonthsNames and if I click it again it hides all months but I want it to leave last 2 months visible


Solution

  • If you are trying to apply ngFor to exclude columns, you can use slice pipe:

    <....*ngFor="let month of listaMonthsNames | slice: mySlicer"...>
    

    where mySlicer can have values from 0 - include all - to listaMonthsNames.length - exclude all, and any N number in [0;listaMonthsNames.length] interval to exclude first N number columns, e.g.: 2 - to exclude first two columns, as in this demo example