adjust the width of a textbox in angular dynamically by fetching a value from the database

I am facing a challenge with setting the width of a textbox dynamically based on the value assigned to the controls linked to it during data retrieval from the database, typically during initialization.

After the page has loaded and all values are assigned to the bound form control, I want the textbox to adjust its width accordingly. However, I have been unable to achieve this so far. Below is the code snippet I am using:

HTML

                    <td formArrayName="entityAttributeInfo" *ngFor="let item of paramTypes.get('entityAttributeInfo')['controls']; let ii = index;">
                        <div [formGroupName]="ii" class="tabledataEquipmentNameInfo">
                            <table *ngIf="item.get('uom').value!=''" style="width:100%">
                                <tr *ngIf="item.get('uom').value!=''">
                                    <td style="width:60%;">
                                        <input formControlName="value" class="form-control" style="border-top-right-radius: 0px; border-bottom-right-radius: 0px;">
                                    </td>
                                    //this td element I want to assign width based on value retrieved from item.get('uom').value
                                    <td style="width:40%;">
                                        <input style="border-left: 0px; border-top-left-radius: 0px; border-bottom-left-radius: 0px;" formControlName="uom" class="form-control" [attr.disabled]="true" />
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </td>

I have attempted to handle the issue on the HTML side by using min-width property, but it did not work as expected. The current HTML output can be seen in the screenshot provided in the link below.

There appears to be an extra gap in the disabled textbox.

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

It should be noted that the value assigned to the textbox can vary greatly in size. Therefore, I need a solution that can adjust the width dynamically. Despite looking through various online articles, I have not been able to find a satisfactory answer.

Answer №1

Have you considered incorporating an ngStyle attribute into your code?

[ngStyle]="{width:VariableHere+'%'}"

<input style="border-left: 0px; border-top-left-radius: 0px; border-bottom-left-radius: 0px;" 
formControlName="uom" 
class="form-control" 
[attr.disabled]="true" 
[ngStyle]="{width:VariableHere+'%'}"/>

In case you wish to apply a CSS rule with a hyphen in its name, remember to enclose it in quotes like so:

[ngStyle]="{'min-width':VariableHere+'px'}"

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

Utilizing a Livewire modal to manage event data in a FullCalendar project within Laravel 9: Efficient savings and updates

After successfully setting up a Laravel 9 project with FullCalendar 5 as a Livewire component, I encountered an issue with the modal. The modal is triggered when clicking on an open date in the calendar (which triggers the 'select' function) or w ...

Creating a dynamic variable reference for ngModel in Angular 2 and above allows for flexible data binding

I am faced with a situation where I need to populate a table's headers based on the JSON data {id, name}. The ngModel name I have used in the component is this.id which needs to be mapped for custom filtering accordingly. For example, list=[{id:age ...

The element 'p-checkbox' is unrecognized in primeng version 4

Currently utilizing primeng version 4. I have included a p-checkbox in my template and imported it to the respective TypeScript file. However, upon loading the page in the browser, an error stating "'p-checkbox' is not a known element" keeps appe ...

Real-time updates to HTML5 progress bar with each button click

Looking to update the HTML5 progress bar value when a number is entered in a text area and button is clicked. I will only input properly formatted integer numbers into the text area. Check out the HTML code below: <textarea id="txt" cols=20 rows=1 pla ...

What causes a div with display: table-cell to have excessive padding when an image is added to it?

Is there a way to remove the padding completely? Take a look at the jsfiddle version: CSS: .table { display: table; height: 250px; border-color: black; width: 500px; padding: 0px; } .cell { display: table-cell; height: 250px ...

Associate a unique identifier string with a randomly generated integer identifier by Agora

For my current web project, I am utilizing a String username as the UID to connect to the channel in an Agora video call. However, I now need to incorporate individual cloud recording by Agora into the project. The challenge lies in the fact that cloud r ...

Having difficulty arranging two <p> or <div> elements in a stacked position within the footer

https://i.sstatic.net/7MTbK.png I've been experimenting with different methods to stack the smaller "test" text under the bigger TestTest. It seems to work fine on the rest of the page when I use a separate div, but for some reason, it's not wor ...

The functionality of data binding becomes unclear when the ngif directive is applied to a mat-selection-list in

I am facing an issue with displaying a mat-selection-list based on a condition. Since adding the ngif condition, the data is consistently being set to undefined. I am struggling to identify the root cause of this problem. Thank you in advance for your assi ...

Issues encountered when attempting to utilize removeEventListener() with multiple elements within a for loop in Javascript

After experimenting, I have been exploring ways to avoid encountering repeated event listeners The script provided below iterates through all buttons with a specific attribute. However, the issue is that it only functions effectively for a single button ...

"Enhance your Vue 3 projects with a dynamic library featuring universal components and full

Currently, I am in the process of developing a Vue 3 component library using Vue 3, Vite, and TypeScript. The unique aspect about this library is that it installs as a plugin and registers all components as global entities. Here is an overview of how this ...

Generate a series of buttons with generic identifiers that can be easily targeted using getElementById. The IDs will be dynamically assigned

I am looking to dynamically generate unique IDs for a list of buttons, allowing me to easily target specific buttons using getElementById. Here is an example code snippet where each button has the same ID: @foreach (var cat in Model) { <div clas ...

How should form elements be named and handled for avoiding duplication?

Suppose my form contains 2 inputs <input name="person_name" value="John" type="text" /> <input name="person_name" value="Jean" type="text" /> After submitting the form, which "person_name" will be received? John or Jean? Also, what if I have ...

inline-block: the first div appears smaller than the second div, creating a gap that is filled within the layout

I need help with aligning three divs, named a, b, and c. Each div is set to 48% width and displayed as inline blocks. Div a will always be shorter than div b, creating a gap between the bottom of a and the top of c. The heights of a and b may vary on diffe ...

Incorporating custom markers into Google Map API V3 by rounding and resizing photos

Is there a way to create a custom map marker for Google Maps by combining two images, marker_bg and marker_pic? I want the marker_bg to be a marker with empty space inside that will be filled by marker_pic. How can I round the picture and set it into the ...

Using Javascript to select a radio button in a form depending on the value entered in a text box

I have a form that interacts with a Google Sheet to insert and retrieve data. For instance, the form contains two radio buttons: <input id="Rdio_1" name="RdioSelect" type="radio" class="FirstCheck" value="1" onchange="RadioValInsert ()"/> < ...

Obtaining API/JSON Data to Implement in a NextJS Application

Currently, I am working on developing a NextJs website that focuses on detecting crop diseases. The process involves sending photos and environmental data to a fastapi python server for processing. Subsequently, the processed data is supposed to be display ...

Tips for avoiding repetitive animations when using jQuery animate toggle

Check out my Pen here. I'm having an issue with a share button that expands on hover. Everything works fine, except if you hover over it multiple times, the animation repeats as many times as you've hovered. Is there a way to prevent this from h ...

Creating uniform width children with Bootstrap's flexbox

Within my flex box container, the children are all displayed in a flex-row. I'm utilizing bootstrap to try and make sure that all the children have the same width and height. HTML <div class="d-flex flex-row flex-grow"> <div class="backgroun ...

JavaScript - Trouble encountered while trying to use splice to insert one array into another array

I've been working on creating a Cache Hashtable using JavaScript. When I use the code cache.splice(0,0, ...dataPage);, it inserts my data starting from the first position up to the length of dataPage. Assuming that my dataPage size is always 10. Th ...

An issue has arisen where Selenium Auth0 is unable to establish a connection with the server

I am using a protractor selenium test for an angular2 application that I execute with the command protractor conf.js --params.login.username=John --params.login.password=Doe. The purpose of the test is to attempt to log in to the backend and intentionally ...