Angular has the feature of a right float button with *ngfor

I've successfully implemented a form using Reactive Forms in Angular.

Currently, my form is displayed as follows:

<div class="center" appMcard>
   <form [formGroup]="GroupRMPM_FG">
      <div formArrayName="GroupId_Name"  *ngFor="let control of GroupRMPM_FG.controls.GroupId_Name.controls; let i= index">
         <input  type="text" pInputText [formControl]="control.controls.Group_Id_Name"/>
         <button pButton type="button" class="delete-btn " *ngIf="GroupRMPM_FG.controls.GroupId_Name.controls.length > 1" (click)="deleteGroup(i)" icon="fa-minus" >
         </button>
      </div>
      <button pButton type="button" (click)="addNewGroup()" icon="fa-plus" class="add-btn"></button>
   </form>
</div>

https://i.stack.imgur.com/yCGpv.png

I want the "+" button to be aligned to the right of the last minus button.

To achieve this, I added a class to the ngfor div:

<div formArrayName="GroupId_Name" class="formcontainer" *ngFor="let control of GroupRMPM_FG.controls.GroupId_Name.controls; let i=index">

Below is the CSS code for the new class:

.formcontainer {
    display: inline-block;
}

Despite adding the class, I'm still unable to position the "+" icon next to the last minus button.

https://i.stack.imgur.com/IMsLn.png

How can I ensure that the "+" icon appears to the right of the last minus button?

Answer №1

It is advisable to include a condition in the button element to display it only when the array reaches a certain length.

<div class="center" appMcard>
    <form [formGroup]="GroupRMPM_FG">
       <div formArrayName="GroupId_Name"  *ngFor="let control of GroupRMPM_FG.controls.GroupId_Name.controls; let i= index">
          <input  type="text" pInputText [formControl]="control.controls.Group_Id_Name"/>
        <button pButton type="button" class="delete-btn " *ngIf="GroupRMPM_FG.controls.GroupId_Name.controls.length > 1" (click)="deleteGroup(i)" icon="fa-minus" ></button>
        <button pButton type="button" (click)="addNewGroup()" icon="fa-plus" class="add-btn" *ngIf="i===GroupRMPM_FG.controls.GroupId_Name.controls.length-1"></button>
       </div>       
    </form>
 </div>

Answer №2

It seems like your approach is on the right track, but consider wrapping your loop-container and utilizing flexbox for alignment (there are other methods to achieve this as well):

<form [formGroup]="GroupRMPM_FG" style="display: flex; flex-direction: row; justify-content: flex-end">
    <div style="display: inline-block">   
       <div formArrayName="GroupId_Name" *ngFor="let control of GroupRMPM_FG.controls.GroupId_Name.controls; let i= index">
       <input  type="text" pInputText [formControl]="control.controls.Group_Id_Name"/>
       <button pButton type="button" class="delete-btn " *ngIf="GroupRMPM_FG.controls.GroupId_Name.controls.length > 1" (click)="deleteGroup(i)" icon="fa-minus" >
     </button>
    <button pButton type="button" (click)="addNewGroup()" icon="fa-plus" class="add-btn"></button>
 </form>

Answer №3

Example utilizing the bootstrap pull-right class:

Code Snippet:

<button type="button" pButton (click)="addNewGroup()" icon="fa-plus" class="add-btn pull-right"></button>

Styling in CSS:

.pull-right{
  float:right
}

Check out my demonstration on JsFiddle https://jsfiddle.net/example/1/

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

Steps for inserting a clickable phone number link within innerHTML1. First, create an

I've been trying to include a specific phone number using the <a href> tag within the innerHTML. I attempted using both double and single quotes. In the first case, nothing happens at all. In the second case, although the phone number appears, ...

jquery counter is malfunctioning

I noticed a strange issue with the counters on my website. They count up as expected when the page loads, but for some reason, when the screen width shrinks below 800px, they don't start automatically. However, if I quickly scroll to where the counter ...

"Implementing a responsive design with a background image and radial gradient - how to

The image currently in use: https://i.stack.imgur.com/piK6l.jpg Here is how I've implemented it using Vue.js, Bootstrap, and CSS: Code Snippet <div class="landing"> <div class="container text-white details h-100"> ...

Adding a component to a page in Angular 4: A step-by-step guide

Currently engaged in a project that involves creating a mobile application design within a web application. I'm wondering how to display my Component object on a page using ViewChild? I have a list of PageComponents, below is the PageComponent class ...

Dynamically load various services based on the URL parameter within an Angular application

My goal is to dynamically inject different services based on a URL parameter. @Injectable({ providedIn: 'root' }) export class SomeService { : } @Injectable({ providedIn: 'root' }) export class AService extends SomeService { ...

Utilizing CSS to Implement a Background Image

Here is where I am looking to upload the image. Ideally, I would like to position my image to the left side of the text related to Food and Travel. You can view the image here. .block-title h3 { color: #151515; font-family: 'Montserrat', s ...

Tips for transferring a value from a component class to a function that is external to the class in angular

Is there a way to pass a variable value from a component class to a function outside the class? I've been struggling to make it work. Whenever I click the button, I receive an error message saying "cannot read property divide of undefined". Here' ...

Creating a completely dynamic button in Angular 6: A step-by-step guide

I have been working on creating dynamic components for my application, allowing them to be reusable in different parts of the program. At this point, I have successfully developed text inputs that can be dynamically added and customized using the component ...

Comparing transition effects: scale, transform, and all

My goal is to apply transition effects to specific transform functions in CSS, such as scale(), while excluding others like translate(). Initially, I attempted the following code snippet: input:focus + label, input:not(:placeholder-shown) + label { tran ...

Show off a sleek slider within a Bootstrap dropdown menu

Is there a way to display a sleek slider within a Bootstrap dropdown element? The issue arises when the slider fails to function if the dropdown is not open from the start, and the prev/next buttons do not respond correctly. For reference, here is my curr ...

"Enhance your website with a dynamic hover effect and striking letter spacing in

In the div below, there is a span with the following code: <div class="mission-button"><span class="mission">view our mission</span></div> Accompanied by this CSS: .mission-button::before { content:'&ap ...

Unique solution: "Using CSS to ensure the overflow document always scrolls to the bottom

Is there a way in CSS to make a div with overflow: scroll always automatically scroll to the bottom along the Y-axis, regardless of its content? For example, like a chat thread that consistently displays the latest message at the bottom. I've encount ...

Error message "Undefined is not a function" occurred while using jQuery's .replace and scrollTop functions

I'm having issues with the scroll function in my code. It doesn't seem to be able to locate the ids in my HTML, even though I can't figure out why. I had a previous version that worked perfectly fine (unfortunately, I didn't save it D:) ...

Struggling to set a theme for Angular Ag Grid within an Angular 10 project

Currently, I am working on a project where Angular 10 is being used along with ag-grid-community version 25.1. When running the application with ag-theme-alphine.css, I encountered the following error: Error: Failed to locate '../node_modules/ag-grid- ...

Attempting to vertically center text within a percentage div

I'm trying to create a button that is centered on the screen with text aligned vertically within the button. I want to achieve this using CSS only and restrict the usage of percentages only. Here's what I have so far: Check out my code snippet h ...

Issue with mouseout gap in Microsoft Edge when using the <select> element

A noticeable gap appears in Microsoft Edge between the <select> menu and its options when expanding the menu, as shown here: https://i.stack.imgur.com/SprJ2.png This particular issue can cause problems with the mouseout event due to inconsistent be ...

Exploring ways to customize the styles of MUI Accordion components in a Next.js project using CSS modules

I'm trying to customize the default MUI CSS for the accordion component using a CSS module. The issue I'm facing is that the class is dynamically added by MUI, making it impossible to target directly. Here's the solution I attempted, but it ...

From transitioning from a radio button's checked indicator to a complete button

I'm in the process of customizing my WordPress plugin and seem to be struggling with styling the radio buttons. For reference, you can find the code snippet here: https://jsfiddle.net/cd3wagvh/ The goal is to conceal the radio buttons and modify the ...

Creating a sticky menu in a column using affix with a CSS bootstrap list-group

My goal is to develop a sticky menu utilizing CSS Bootstrap affix and the list-group menu. I have successfully implemented most of it, except for one issue when the user scrolls down. Upon scrolling down, the menu expands to fill the entire width of the ...

summoning the iframe from a separate window

In my current setup, I have a link that passes a source to an iframe: <a href='new.mp4' target='showVideo'></a> <iframe src='sample.jpg' name='showVideo' ></iframe> However, what I would lik ...