Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have the *ngFor loop to execute for first n-1 times, don't want last index' data to be displayed?

HTML :

<div *ngIf="userdata; else blank;">                         
 <table>
  <thead>
   <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Email</th>
    <th>Address</th>
    <th>Role</th>
    <th>Gender</th>
    <th>Actions</th>
   </tr>
  </thead>
  <tbody>
   <tr *ngFor="let data of userdata">                                          
    <td>{{data.Id}}</td>
    <td>{{data.Name}}</td>
    <td>{{data.Email}}</td>
    <td>{{data.Address}}</td>
    <td>{{data.Role}}</td>
    <td>{{data.Gender}}</td>
    <td>
     <button type="button" (click)="setUser(data)" class="btn btn-default btn-sm edit">
      <span class="glyphicon glyphicon-edit"/> Edit</button>                                              
     <button type="button" id="close" style="height: 30px;"
      (click)="deleteUser(data.Id)" class="btn btn-default btn-sm close" aria-label="Close">
      <span aria-hidden="true">&times;</span>
     </button>
    </td>                                           
   </tr>  
  </tbody>
 </table>
</div>
<ng-template #blank>No Records found...</ng-template>

This is what i exactly get in userdata Array :

[{
    "Id": "1",
    "Name": "sdfd",
    "Email": "fdg",
    "Address": "dfg",
    "Role": "Admin",
    "Gender": "male"
}, {
    "Id": "2",
    "Name": "dgfhh",
    "Email": "gdh",
    "Address": "hhh",
    "Role": "User",
    "Gender": "female"
}, {
    "Id": "3",
    "Name": "dfgf",
    "Email": "fgdh",
    "Address": "fghd",
    "Role": "Super-Admin",
    "Gender": "male"
}, "{\"blank-email\":\"Email cannot be blank\",\"blank-role\":\"You must play some role\",\"blank-gender\":\"You must specify your Gender\"}"]

so here, i don't want this to be executed from the array:

"{\"blank-email\":\"Email cannot be blank\",\"blank-role\":\"You must play some role\",\"blank-gender\":\"You must specify your Gender\"}"

like image 609
Jitendra Ahuja Avatar asked Mar 06 '23 19:03

Jitendra Ahuja


1 Answers

You could add *ngIf and render the elements which are not last.

<tr *ngFor="let data of userdata; last as l">
   <ng-container *ngIf="!l">
     .....
   </ng-container>
</tr>

Rather good approach would be slice or dice down the viewModel collection itself and keep ngFor on that collection to display it on the UI.

like image 55
Pankaj Parkar Avatar answered Mar 08 '23 23:03

Pankaj Parkar