0

I want to implement a few angular toggle buttons. I don't exactly know how many, that depends on my database data.

The buttons are shown on a sidebar, which means that only up to 3 buttons can be rendered in one line.

Is there a way to break to a new line after e.g. 3 buttons ? Without creating a new mat-button-toggle-group ?

Here's my code:

      <mat-button-toggle-group multiple name="categories_1" ngModel aria-label="Kategorien">
        <mat-button-toggle value="LOREM1_1" checked>Value1</mat-button-toggle>
        <mat-button-toggle value="LOREM1_2" checked>Value2</mat-button-toggle>
        <mat-button-toggle value="LOREM1_3" checked>Value3</mat-button-toggle>
        <mat-button-toggle value="LOREM2_1" checked>Value4</mat-button-toggle>
      </mat-button-toggle-group>

Thanks for your help!

1
  • Are you using Bootstrap? Commented Dec 11, 2018 at 12:08

1 Answer 1

1

You can use flex for this.

<mat-button-toggle-group fxLayout="row" multiple name="categories_1" ngModel aria-label="Kategorien">
    <mat-button-toggle fxFlex="33%" value="LOREM1_1" checked>Value1</mat-button-toggle>
    <mat-button-toggle fxFlex="33%" value="LOREM1_2" checked>Value2</mat-button-toggle>
    <mat-button-toggle fxFlex="33%" value="LOREM1_3" checked>Value3</mat-button-toggle>
    <mat-button-toggle fxFlex="33%" value="LOREM2_1" checked>Value4</mat-button-toggle>
    ...
</mat-button-toggle-group>

Or you can also loop through with *ngFor in case of unknown number of items, and still use the 33% flex width to get 3 items per line. You can adjust the width% to get more or fewer items in a single line.

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

1 Comment

This works for me when using fxLayout="row wrap" instead, though the look becomes a bit wonky (messed up border lines, and extra space on right unless you do fxFlex="33.33%")

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.