Display a list of items, and if there are more than two items, switch to a mat-menu

I have a collection of items that I would like to display. In cases where there are more than two items, instead of showing all the items, I want to display a button with the number of remaining items and their names.

 <div *ngFor="let item of items; let i = index">
        <div>
            <div class="avatar" *ngIf=" i<2" />
              {{items.Name}}
            </div>
            <div *ngIf="i >= 2 && items?.length > 2">
               <button class="avatar" mat-button [matMenuTriggerFor]="menu">+ {{items?.length-2}}</button>
            <mat-menu #menu="matMenu">
              <button mat-menu-item>{{items.Name}}</button>
            </mat-menu>
          </div>
        </div>
        </div>

The issues I am facing:

  • The button displaying the number of remaining items is being repeated due to ngFor. How can I resolve this while still displaying item names using ngFor?

  • Is there a more efficient way to show the count of remaining items other than + {{items?.length-2}}?

Answer №1

When faced with a decision, here are your options:

<div *ngIf="i == 2 && items?.length > 2">
               <button class="avatar" mat-button [matMenuTriggerFor]="menu">+ {{items?.length-2}}</button>
            <mat-menu #menu="matMenu">
              <button mat-menu-item>{{items.Name}}</button>
            </mat-menu>
            

To make a change, consider moving the button outside of the loop and updating it to:

<div *ngIf="items?.length > 2">
               <button class="avatar" mat-button [matMenuTriggerFor]="menu">+ {{items?.length-2}}</button>
            <mat-menu #menu="matMenu">
              <button mat-menu-item>{{items.Name}}</button>
            </mat-menu>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a table and populating its cells with values all within the confines of a single function

This section of code aims to create 3 arrays by extracting values inputted by the user from a popup menu in the HTML file. These values are then utilized to populate the table displayed below. var arrM = new Array; var arrT = new Array; var ar ...

JS stop the timer from the previous function call before starting a new function call

Within my React app, I have implemented a slider component from Material UI. Each time the slider is moved, its onChange event triggers a function to update the state. However, I have noticed that the component rerenders for every step of the slider moveme ...

Repair the masthead background during overscroll

The Dilemma At the top of my webpage, I have a sleek masthead with a captivating background image that scrolls along with the page. However, there is an issue when users overscroll upwards, causing an undesirable white overflow to appear. To rectify this ...

Using a plain JavaScript React component within a TypeScript React project: A step-by-step guide

A coworker of mine used Typescript for a frontend React project. With their departure, I have been tasked by management to take over the project and deliver results from Day 1. While they are open to me using Javascript in the project, I am unsure how to i ...

What steps should I follow to set up a dynamic theme in an Angular Material application?

I have spent countless hours trying to find clear documentation on setting up an Angular Material app with a theme, including changing the theme dynamically. Despite searching through numerous search results and visiting various pages, I have not been able ...

Having trouble getting the Javascript/jQuery listener to function properly with Underscore.js templates

I've been working on developing a webapp using underscore.js for creating templates. However, I've encountered an issue when trying to render a basic template like the one below: <div id="container"></div> <script type=& ...

Tips for Configuring a Nestjs Query Using TypeORM to Retrieve Several Entries

When I send this specific URL from my Angular application: http://localhost:3000/api/skills?category_id=2 The main issue revolves around how to modify the code in order to successfully retrieve all skills with a category_id of 2. It is important to note ...

When transmitting data from the parent component to the child component, the data successfully passes through, yet the view fails to reflect the

I'm facing an issue with passing data from a parent component to a child component using props. Here is the code snippet I'm working with: <div id="root"> <my-component v-bind:account-types='accountTypes'> </my-c ...

Guide to integrating animations into a React JS array sorting program

I am currently working on a project that utilizes react js to visually sort an array. I have successfully implemented the sorting algorithm, and the website now displays the correctly sorted array. However, I would like to incorporate an animation to show ...

How can PHP handle JSON data that arrives in an incomplete format?

I have created a basic website application that interacts with an API to gather backlink data for any website entered by the user. The API sends data in JSON format, consisting of strings and numbers. After parsing the response, I filter out the desired da ...

Controlling the Quantity of Selected Checkboxes with JavaScript

I am facing an issue with implementing multiple checkboxes with limits in JavaScript, as shown below. $(".checkbox-limit").on('change', function(evt) { var limit = parseInt($(this).parent().data("limit")); if($(this).siblings(':checked&ap ...

Steps to successfully deploy an Angular application using Jenkins

I'm in the process of deploying my Angular application to an AWS EC2 instance. For version control, I'm utilizing Bitbucket and Jenkins for continuous integration. ...

Post-render for HTML linkage

Is there a method to execute customized code after Knockout has inserted the html into the DOM and completed rendering? This is required in order to bind a nested view model to dynamically generated html code. Perhaps like this: <div data-bind="html: ...

Reducing an array group using index in JavaScript: A beginner's guide

Do you have coding questions? Check out this sample array group: myArray = { tab1 : [], tab2 : [], tab3 : [], tab4 : [] } I'm looking to always retain the first tab (tab1) and an additional tab based on an index (ranging from 2 to 4) For instance, ...

What is the best way to keep the heights of two divs in sync?

Is there a way to make two divs have the same height, even though their content determines their individual heights? I am looking for a solution that doesn't involve using a table or a parent div. I'm new to JavaScript, so I'm hoping there i ...

Tips for eliminating flicker upon the initial loading of a webpage caused by dynamic changes in CSS styles through JavaScript

Struggling with the load order of my CSS and JavaScript... Currently, I have a div with a blue background that is styled to appear sliced diagonally. However, upon initial page load (or CTRL+SHIFT+R), there's a brief moment where the slice effect doe ...

How can you synchronize a prop with the parent component in Vue?

I want to establish a bidirectional relationship between a component and its child. For instance, I have a common string that is passed to my sub-components: <template> <div> <foobar title="Foo" :value.sync="shared" ...

A guide to creating a form using Angular components

I am working on creating an authentication form using an Angular component. Here is what I have tried so far: <form (ngSubmit)="onSubmit()" #loginCtrl="ngForm"> <div class="input"> <material-input type="text" ...

Passing data between Vue.js components effortlessly without relying on events

At the moment, I am utilizing Laravel Nova. It's worth noting that it operates using vue.js. I've created a personalized vue component which requires the ability to modify data inside another component. This specific component is located in the ...

Angular: implementing a service for conditional module imports

Currently, I have a service that is responsible for loading a list of modules: @Injectable() export class MyService { public allowedModules: any = this.modulesFilter(); constructor() { } public modulesFilter() { const testPef = true; co ...