1

I have a requirement to always display minimum of 5 rows(5 or more rows) in a table. For example, if there are 2 rows available in Database, I need to display other 3 more rows in UI with empty rows.

Here is what I tried so far:

<div *ngFor="let task of tasks; let i = index">
    <div class="rowDiv">{{task.id}}</div>
</div>

Here I want to run the loop from i = tasks.size to i < = 5. So that I have total of 5 rows in UI. How to achieve this?

<div *ngFor=" let i = index">
    <div class="rowDiv"></div>
</div>
3
  • Like Alex mentions, the easiest way is to make sure the taks array exist of 5 element. Like this: let tasks= ['task 1', 'task 2', 'task 3']; tasks = tasks.length < 5 ? [...new Array(5)].map((_, i) => tasks.length >= i ? tasks[i] : null) : tasks; Commented May 25, 2020 at 16:36
  • Are you fetching the data from the API? If so, do you have control over the API? If so, you can easily do this in API. If not, this can be done in the typescript. Commented May 25, 2020 at 16:39
  • What do you mean API? I am fetching the values from the backend REST service that is exposed. Commented May 25, 2020 at 16:40

4 Answers 4

4

You can loop over an array of 5 items, and use *ngIf to display an additional row if no data item exists at a given index:

<div *ngFor="let task of tasks">
  <div class="rowDiv">{{task.id}}</div>
</div>
<ng-container *ngFor="let i of [0,1,2,3,4]">
  <div *ngIf="!tasks[i]">
    <div class="rowDiv">This row is empty</div>
  </div>
</ng-container>

See this stackblitz for a demo.

Sign up to request clarification or add additional context in comments.

1 Comment

This is closer to what I wanted. However, in this way I get maximum 5 elements in the UI. My requirement is minimum 5 elements should be displayed in UI at any point. However, this list can grow from backend and the items will be scrolled in the table.
2

you can also add so many rows you need after

  <table>
    <row *ngFor="let task in task">
    </row>
    <!--if task.length<5-->
    <ng-container *ngIf="tasks.length<5">
    <!-use slice:0:5-tasks.length-->
    <row *ngFor="let i of [0,1,2,3,4] |slice:0:5-tasks.length">
    </row>
    </ng-container>
  </table>

Comments

1

You don't need to keep this logic in html. In you class you can do something like this: (suppose you fetch tasks from server)

this.getTasks().subscribe((tasks) => {
   const emptyTasks = Array(5).fill({id: 'empty'});
   this.tasks = tasks.map((t, index) => t || emptyTasks[index]);
})

1 Comment

this.tasks = tasks.length < 5 ? [...new Array(5)].map((_, i) => tasks.length >= i ? tasks[i] : {id: 'empty'}) : tasks;
0

This could be better handled in the controller. In case of default change detection strategy, the template is reloaded multiple times without our control or knowledge. So it's better to make sure the tasks variable has atleast 5 elements in the controller rather than to control the flow in the template. You could something like the following in the controller and leave the template unchanged.

for (let i = 0; i < 5; i++) {
  if(!this.tasks[i].id) {
    this.tasks[i].id = '';
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.