Eradicate lines that are empty

I have a list of user roles that I need to display in a dropdown menu:

export enum UserRoleType {
  masterAdmin = 'ROLE_MASTER_ADMIN'
  merchantAdmin = 'ROLE_MERCHANT_ADMIN'
  resellerAdmin = 'ROLE_RESELLER_ADMIN'
}

export const AdminRoleType2LabelMapping = {
  [UserRoleType.masterAdmin]: 'Master Admin'
};

export const MerchantRoleType2LabelMapping = {
  [UserRoleType.merchantAdmin]: 'Merchant Admin'
};

export const ResellerRoleType2LabelMapping = {
  [UserRoleType.resellerAdmin]: 'Reseller Admin'
};


// Assigning the mapping values and role types
public ResellerRoleType2LabelMapping = ResellerRoleType2LabelMapping;
public roleTypes = Object.values(UserRoleType).filter(value => typeof value === 'string');

To create the dropdown menu, I am using the following code snippet:

<select class="custom-select" formControlName="role">
        <option [value]="roleType" *ngFor="let roleType of roleTypes">{{ ResellerRoleType2LabelMapping[roleType] }}</option>
        <div class="help-block form-text with-errors form-control-feedback" *ngIf="controlHasErrors('role')">
          {{controlValidateMessage('role')}}
        </div>
      </select>

I encountered an issue where blank lines were appearing in the dropdown menu. To address this, I attempted to use only the keys from the UserRoleType enum, but it resulted in more blank rows:

public roleTypes = Object.keys(UserRoleType).filter(value => typeof value === 'string');

Could you provide any insights on how I can fix this problem? My goal is to display the user roles without any empty lines in the dropdown menu.

Answer №1

Cause

The issue arises from the fact that ResellerRoleType2LabelMapping only includes UserRoleType.resellerAdmin, while all roles are present in roleTypes. Consequently, querying

ResellerRoleType2LabelMapping[UserRoleType.masterAdmin]
will result in an "undefined" value.

Resolution

To address this, utilize a unique "labelMapping".

Typescript

Substitute

public ResellerRoleType2LabelMapping = ResellerRoleType2LabelMa

with

public labelMapping = {...AdminRoleType2LabelMapping, ...MerchantRoleType2LabelMapping, ...ResellerRoleType2LabelMapping};

HTML

then modify

{{ ResellerRoleType2LabelMapping[roleType] }}

to

{{ labelMapping[roleType] }}

Answer №2

Implement

roleTypes = Object.values(UserRoleType).filter(value => ResellerRoleType2LabelMapping[value]);

This code snippet filters the role types that exist in ResellerRoleType2Map.

Check out this example on StackBlitz

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

Ways to integrate npm dependencies into your Cordova plugin

Currently working on implementing a Cordova plugin called core-cordova found in this repository. This particular plugin has a dependency on another NPM package. The issue arises after installing the plugin in my app using: $ cordova plugin add @aerogears ...

Is there a way to apply Validators.required just once for all required form fields in a Reactive Form?

Latest version of Angular is 4.4.3. In Reactive Form, you can use Validators.required for each form field as shown below: this.loginForm = this.fb.group({ firstName: ['', [Validators.required, Validators.maxLength(55)]], ...

What is the process for implementing a unique Angular theme across all components?

I created a unique Angular theme called 'my-theme.scss' and added it to the angular.json file. While it works with most Angular Elements, I am facing issues getting it to apply to certain HTML Elements inside Angular Components. For example: < ...

What is the best way to implement multiple HTTP subscriptions in a loop using Angular?

I find myself in a predicament where I need to iterate through an array of strings passed as parameters to a service, which then responds through the HTTP subscribe method. After that, some operations are performed on the response. The issue arises when t ...

What makes TS unsafe when using unary arithmetic operations, while remaining safe in binary operations?

When it comes to arithmetic, there is a certain truth that holds: if 'a' is any positive real number, then: -a = a*(-1) The Typescript compiler appears to have trouble reproducing arithmetic rules in a type-safe manner. For example: (I) Workin ...

What is the best way to execute my mocha fixtures with TypeScript?

I am seeking a cleaner way to close my server connection after each test using ExpressJS, TypeScript, and Mocha. While I know I can manually add the server closing code in each test file like this: this.afterAll(function () { server.close(); ...

Hostlistener is unresponsive to inputs from arrow keys

Within my Angular 2 component, I have implemented a key event listener to capture keystrokes: @HostListener('document:keypress', ['$event']) handleKeyboardEvent(event: KeyboardEvent) { console.log(event); } Oddly enough, I am not ...

When trying to access a specific property of an object in Typescript using a key that is defined as a subset of

UPDATE: Take a look at the revised solution below, inspired by @GarlefWegart's input. I've been exploring the creation of generic typings for dynamic GraphQL query outcomes (mostly for fun, as I suspect similar solutions already exist). I' ...

What does it signify when it is stated that "it is not a descendant of the indexer"?

Currently, I am diving into Typescript with the help of this informative guide on indexer types. There is a specific piece of code that has me puzzled: interface NumberDictionary { [index: string]: number; length: number; // okay, length shoul ...

Issue: (SystemJS) Unable to find solutions for all parameters in $WebSocket: ([object Object], [object Object], ?)

Upon running the code snippet below, an error is thrown: Error: (SystemJS) Can't resolve all parameters for $WebSocket: ([object Object], [object Object], ?). app.component.ts import { Component } from '@angular/core'; import {$WebSocket} ...

What are the steps to styling a component with CSS Emotion?

I am facing an issue with using a theme with TypeScript in this component const buttonDisabled = css` color: ${({ theme }) => theme.color}; `; Is there a way to correctly type this component? Error: No overload matches this call. Overload 1 of 2, & ...

Having trouble showcasing the response data from a service in Angular onto the HTML view

I am facing an issue in my Angular2 application where I am trying to utilize a GitHub service to display returned data on the UI. Initially, I encountered an error stating "Cannot find a differ supporting object '[object Object]' of type 'o ...

Using any random function as a property value in a TypeScript React Component

I am facing a challenge in my React component where I need to define a property that can accept any arbitrary function which returns a value, regardless of the number of arguments it takes. What matters most to me is the return value of the function. Here ...

Creating React Components with TypeScript: Ensuring Typechecking in Class and Function Components

Want to ensure typechecking works when defining React Class/Function components in TypeScript? Struggling to find a comprehensive guide on how to do it correctly. I've managed to define the Props and State interfaces, but I'm unsure about how to ...

Refactoring TypeScript components in Angular

How can I streamline the TypeScript in this component to avoid repeating code for each coverage line? This angular component utilizes an ngFor in the HTML template, displaying a different "GroupsView" based on the context. <div *ngFor="let benefitG ...

Upgrading Angular from version 8 to 9: Dealing with Compilation Errors in Ivy Templates

After successfully upgrading my Angular application from version 8 to version 9 following the Angular Update Guide, I encountered some issues in the ts files and managed to resolve them all. In version 8, I was using the View Engine instead of Ivy, which ...

Implementing global user authentication state with Zustand in Next.js version 13.4.9

I'm grappling with incorporating zustand into my Next.js 13.4.9 app, specifically for managing global authentication state. Below is the code snippet I have in my application: zustand store: // /src/store/AuthStore.ts import { create } from 'zu ...

Talebook by Syncfusion

I'm completely new to Storybook and I am currently exploring the possibility of creating a Storybook application that showcases a variety of controls, including Syncfusion controls and other custom controls that I will be developing in the future. Ha ...

Angular form grouping radio buttons in a unique way

Encountering some unusual behavior with radio buttons and forms currently. In my form, I have 4 radio buttons set up. The issue I'm facing is that when I click on a radio button, it remains enabled permanently. This means I can have all 4 options sel ...

Category of ambiguous attributes

I am currently working on developing a class that will store values for rows and columns, with each row/column identified by a string. I have provided some code below as an example: class TMatrix<TYPE>{ [idRow: string]: { [idCol: string]: TYPE ...