Issue with the naming of Angular button conflicting with array naming conventions

I'm encountering a challenge with button naming in my Angular SPA component that I'm currently developing.

Background - On a page where a data table is displayed, there's a 'Select' control that allows users to choose which columns to hide. When a user selects a column, it gets hidden and a button with the column name appears. Users can select multiple columns to hide, each resulting in a corresponding button on display. However, my issue lies in the fact that all buttons end up named after the last column selected.

Approach - To manage column information, I've established an array.

Component HTML:

<div *ngFor="let column of columns">
<button class="actionbutton" id="{{columnId}}" (click)="showColumn(selectedColumn)">{{columnName}}</button></div>

Component .ts File:

for (let i = 0; i < this.columnArray.length; i++) {
this.columnId = this.columnArray[i];
this.columnId = this.columnId.replace(' ', '-') + '-' + i;

this.columnName = this.columnArray.splice(0, this.columnArray.length);
this.columns.push(this.columnName);}

As a newcomer to Angular & Typescript, resolving this issue has been a bit tricky for me. Any assistance or guidance would be highly appreciated.

Answer №1

Allow me to clarify things for you. To display the columnName in your code, all you need to do is adjust your HTML as follows (please note that your columnId may not function correctly):

<div *ngFor="let column of columns">
<button class="actionbutton" id="{{columnId}}" (click)="showColumn(selectedColumn)">{{column}}</button></div>

This is because columns is essentially an array of columnNames. When you use *ngFor to iterate through columns, each column represents a single columnName-string.

In addition, there are some areas in your code that could be improved. It would be better if your columns-array contains objects with the properties id and name. This way, you can modify your HTML like this:

<div *ngFor="let column of columns">
<button class="actionbutton" id="{{column.id}}" (click)="showColumn(selectedColumn)">{{column.name}}</button></div>

Moreover, using this.columnName suggests that you might be utilizing a global variable being constantly overwritten within a loop... But don't worry, you'll get the hang of it gradually :-)

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

I want to modify a class in Angular 8 by selecting an element using its ID and adjust the styling of a div

Is it possible to dynamically add and remove classes using getElementById in Angular 8? I need to switch from 'col-md-12' to 'col-md-6' when a user clicks on the details icon. I also want to modify the style of another div by setting d ...

Is there a method in TypeScript to create an extended type for the global window object using the typeof keyword?

Is it possible in TypeScript to define an extended type for a global object using the `typeof` keyword? Example 1: window.id = 1 interface window{ id: typeof window.id; } Example 2: Array.prototype.unique = function() { return [...new Set(this)] ...

Issue with Angular 2: Unable to locate the module '@angular/animations/browser'

I am currently attempting to install Angular2-Toaster (https://github.com/Stabzs/Angular2-Toaster). Initially, I successfully installed it using npm install angular2-toaster and was able to import it without any errors. However, I realized that I also need ...

How to utilize a defined Bootstrap Modal variable within a Vue 3 single file component

I'm diving into the world of TypeScript and Vue 3 JS. I created a single-file-component and now I'm trying to implement a Bootstrap 5 modal. However, my VSCode is showing an error related to the declared variable type. The error message reads: ...

Error message 2339 - The property 'toggleExpand' is not recognized on the specified type 'AccHeaderContextProps | undefined'

When utilizing the context to share data, I am encountering a type error in TypeScript stating Property 'toggleExpand' does not exist on type 'AccHeaderContextProps | undefined'.ts(2339). However, all the props have been declared. inter ...

What is the best way to dynamically populate a list in Angular when a button is clicked?

Currently, I'm in the process of developing a website that will serve as the interface to control a robot. My goal is to create a user-friendly system where users can input latitude and longitude coordinates, click a designated button, and have those ...

Strategies for distinguishing between identical Xpath for each edit text field in Selenium

I am facing an issue passing values into 8 text fields in Selenium using Java. The problem arises when I try to locate each text field individually as they all have the same XPath. Is there a way to differentiate between these fields and input values acc ...

Issue with ion-datetime component causing unexpected value changes when both maximum and minimum values are set

My current project includes the use of ion-datetime. Here are some details regarding the Ionic version being used: Ionic: Ionic CLI : 5.4.16 Ionic Framework : ionic-angular 3.9.5 @ionic/app-scripts : 3.2.2 Below is a snippet showcasi ...

Creating a factory function through typhography

I have a dynamically generated list of functions that take an argument and return different values: actions: [ param => ({name: param, value: 2}), param => ({label: param, quantity: 4}), ] Now I am looking to create a function that will gen ...

Angular - utilizing subscription within a for-loop to determine completion

Below is the code I am using to generate sticky notes: update() { this.tab3Service.updateStickyNote(this.stickyNoteUserConnection.stickyNote).subscribe(response => { const updatedStickyNote: StickyNote = response; for(let i = 0; i < this.stickyNo ...

I'm trying to determine in Stencil JS if a button has been clicked in a separate component within a different class. Can anyone assist

I've created a component named button.tsx, which contains a function that performs specific tasks when the button is clicked. The function this.saveSearch triggers the saveSearch() function. button.tsx {((this.test1) || this.selectedExistingId) && ...

An error occurred with the datepicker: Unable to connect to 'bsValue' as it is not recognized as a property of 'input'

Despite importing DatepickerModule.forRoot() in my Angular unit test, I am encountering the following error: Error: Template parse errors: Can't bind to 'bsConfig' since it isn't a known property of 'input'. (" ...

Adaptable features for ever-changing forms

Creating reactive forms from a JSON object model can be a complex task, similar to the example provided in https://angular.io/guide/dynamic-form. For instance: { "id": "1" "type":"input" }, { "id": "2" "type":"dropDown" } The form generation proc ...

How can I personalize a Leaflet popup with image thumbnails and additional customization options?

I've been researching and trying out different solutions, but so far none of them have worked for me. My goal is to dynamically add a title, image, address, and name to popups on my leaflet map as data comes in. However, I'm experiencing some cha ...

What is the best way to activate an <ion-datetime> with an <ion-button>?

Im currently attempting to execute the following action: I would like to select only the month and year using the example below: <ion-datetime presentation="month-year"></ion-datetime> However, I do not wish for this label to be vis ...

Loading an Angular application by sending an HTTP request to populate an array

After developing a simple shopping cart application using Angular 9, I faced an issue where I could only retrieve the product list by triggering a click event. The core functionality of my app involved sending an HTTP request to the server in order to fet ...

Adapting the Homepage Based on User Authentication

Currently, I am working on a website where I am implementing a login feature. Once the user logs in successfully, they should be redirected to the index page and see something other than the login button. I have been using the following code for my implem ...

Angular 2+ enables the creation of dynamic folders and allows for uploading multiple files to the server in a seamless

Can an Angular application be developed similar to FileZilla or Core FTP for uploading files and folders to the server via FTP? I need a way to upload files and folders through the admin panel instead of relying on external applications like FileZilla. ...

Tips on sorting objects by comparing them to array elements

I have an array called myarrays and an object named obj. I need to filter the object by comparing the elements of the array with the keys of the object. If you want to see the code in action, you can check it out on StackBlitz: https://stackblitz.com/edit ...

Ensuring Map Safety in Typescript

Imagine having a Map structure like the one found in file CategoryMap.ts export default new Map<number, SubCategory[]>([ [11, [100, 101]], [12, [102, 103]], ... ]) Is there a way to create a type guard for this Map? import categoryMap fro ...