Divide the toggle buttons into separate rows for better organization

Snippet:

<mat-button-toggle-group value="0" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group2="matButtonToggleGroup">
    <div *ngFor="let item of test; let i = index;">
        <mat-button-toggle *ngIf="!item.answer" value="{{i}}">{{i}}</mat-button-toggle>
    </div>
</mat-button-toggle-group>

Below is the layout with more than 5 items in 'test' array :

https://i.sstatic.net/Ro2z6.jpg

The expected design:

https://i.sstatic.net/vLDgp.png

I've experimented with various CSS properties like display, width, and height but couldn't achieve the desired outcome. How can I arrange the buttons into rows instead?

Answer №1

As mentioned in this reference, a simple function can be created to divide the initial array into smaller arrays of equal length.

Once you have your array of arrays, it is easy to loop through them as indicated by @Frost in a comment.

<mat-button-toggle-group value="0" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group2="matButtonToggleGroup">
  <div *ngFor="let array of test">
    <div *ngFor="let item of array; let i = index">
      <mat-button-toggle *ngIf="!item.answer" value="{{i}}">{{i}}</mat-button-toggle>
    </div>
  </div>
</mat-button-toggle-group>

If you prefer to maintain your one-dimensional array, you can iterate through it and utilize modulo to insert a blank element for transitioning to the next line, like so:

<ng-container *ngFor="let item of test; let i = index">
  <div style="display:block" *ngIf="i%5 == 0">
  </div>
  {{i}}
</ng-container>

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

Should FormBuilder be utilized in the constructor or is it considered a poor practice?

section, you can find an example of implementation where declarations for formBuilder and services are done within the constructor(). While it is commonly known that using services inside the constructor() is not a recommended practice and should be done ...

Tips for addressing the ExpressionChangedAfterItHasBeenCheckedError issueWould you like a way

I comprehend the reason for this error occurring in dev mode and the underlying cause. (for those who are unsure: ExpressionChangedAfterItHasBeenCheckedError Explained) What perplexes me is how to rectify it in my specific scenario. I am displaying a hi ...

Encountering an issue with Angular 12 where a TypeError is being thrown, specifically stating "Cannot read properties of null (reading 'length') at

I encountered an error message while making a http request in my Angular Service. Strangely, this error only occurs after I logout, but it disappears upon logging back in: Below is the code snippet of my authentication Service: import { Injectable } from ...

Apply CSS code to create a fading effect for a container

Below is the code for my accordion list, created using only CSS and HTML. Whenever a heading is clicked, the text slides in with a different background color. How can I make it so that the container with the text and different background color fades in and ...

From Table to DIV: A Simple Conversion

I have encountered a major issue that has stumped me so far. Perhaps you can assist me. I am dealing with a table that appears as follows: __________________________________ | | time1 | time2 | time3 | +--------+-------+-------+-------+ | John | ...

Achieve the positioning of a Bootstrap navbar within a div without it getting wrapped by following

I have been attempting to overlay my navbar on top of a background image by nesting it within a div and using absolute positioning. However, I've run into an issue where the navbar-header/navbar-brand section causes the rest of the navbar to wrap onto ...

Trouble with jQuery on click function, no actions triggered

I am having trouble with this jQuery function that is supposed to post the value of a variable to a PHP page and display it in the correct status class. I have included the necessary jQuery file, but for some reason, nothing is happening. Any assistance w ...

Is there a left and right margin when incorporating Bootstrap grid?

Currently, I am working on developing an Angular application and utilizing Bootstrap columns to easily format and space my page. However, I am facing an issue where there seems to be some unwanted margin around the element even though I have applied col-lg ...

Ways to emphasize the contrast between two texts using CSS

One method to highlight specific parts of text using CSS can be found in this script: span.highlight { background-color: #B4D5FF; } However, what if we want to highlight the differences between two strings? Take for example these two strings: this ...

Mastering ForEach Loops and SetTimeout in Angular 6: A Comprehensive Guide

While I understand this question may have been raised previously, none of the threads I have come across seem to address my specific issue. I currently have code written in angularjs that I am looking to transition to angular 6. My main goal is to impleme ...

Is there a way to position the Bootstrap navbar menu items in the center specifically for mobile devices?

I have a navbar that I want to center the menu li items when it collapses. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9dbd6d6cdcacdcbd8c9f98c978b978b">[ema ...

Styles for Django Rest Framework admin interface are not displaying correctly

Has anyone else experienced an issue with the styling in their Django admin panel? The CSS appears to be functioning properly, but once you navigate to a specific model, the layout becomes distorted. I can't seem to figure out what could be causing th ...

React along with Material UI offers an effective method to avoid the child tree from remounting while toggling the parent theme

Context Implementation In an effort to replicate Material UI's dark/light mode theme toggling, I decided to encapsulate its functionality into a custom hook. This hook generates theme-related properties that are then utilized at the App() level in my ...

Switch up your text display using JQuery and toggle between different texts

I have implemented a jQuery script to toggle the display of certain paragraphs when the "More" link is clicked. However, I am facing an issue where the link always displays "More" even after the content has been expanded. I want it to change to "Less" once ...

Accessing external data in Angular outside of a subscription method for an observable

I am struggling to access data outside of my method using .subscribe This is the Service code that is functioning correctly: getSessionTracker(): Observable<ISessionTracker[]> { return this.http.get(this._url) .map((res: Response) => ...

The white-spaces in Quill JS do not retain their original formatting

I recently integrated Quill JS editor into my website. During testing, I noticed that any text inputted after a white space gets emitted when alerting the content. Below is the HTML code snippet: <div id="postCommentEditor" class="postCo ...

Checking to see if the prop 'isChecked' has been modified

I'm currently facing a challenge with testing whether a class's prop value changes after clicking the switcher. Below is the component class I am working with: import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core&a ...

Incorporate Angular frontend into current website or project

I've successfully built a website using bootstrap, but now I'm looking to integrate Angular in order to transform it into a single page application. The goal is that when a user clicks on a link, only the necessary content will be loaded from the ...

Achieving uniform distribution of items within a flex container

I have been struggling since yesterday to make this work. I just can't seem to get these flex items to fill the container equally in width and height. No matter what I try, it's not cooperating. <html> <meta charset="utf-8"> ...

Issue encountered with the inability to successfully subscribe to the LoggedIn Observable

After successfully logging in using a service in Angular, I am encountering an error while trying to hide the signin and signup links. The error message can be seen in this screenshot: https://i.stack.imgur.com/WcRYm.png Below is my service code snippet: ...