Exploring Enum properties within an angular js select drop down

I am working with an enum in Java that looks like this:

public enum myEnum{
   enum1("enumDisplayVal1"), enum2("enumDisplayVal2")
   myEnum(String displayValue) { this.displayValue = displayValue;}

   private String displayValue;
   public String getDisplayValue(){return displayValue}
}

Now, in AngularJS, I want to create a select dropdown using the values from the enum.

<select class="multiselect" multiple="multiple" id="enumDropDown"
    data-ng-change="update()"
    data-ng-options="e for e in myEnumValues"
    data-ng-model="selectedEnum">
    <option value="">All</option>
  </select>

Currently, the dropdown displays values like enum1, enum2 which are the actual values of the enum. How can I modify it to show the "displayValue" property instead?

To send the enum values to AngularJS, I use myEnum.values()

Answer №1

This is the solution I came up with: I created a mapping of the enum values() like this,

 Arrays.stream(myEnum.values()).collect(toMap(i -> i.name(), i -> i.getDisplayValue())

For the Angular js part :

 <select class="multi-selection"  data-ng-change="update()"
                data-ng-options="key as value for (key, value) in myEnumValues"
                data-ng-model="selectedEnum">
                <option value="">All</option>
   </select>

This resolved the issue of having separate display names and different values.

Answer №2

Here is a suggestion for you:

 import com.fasterxml.jackson.annotation.JsonValue;

.....

 @JsonValue
 public String getDisplayValue(){return displayValue}

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

Tally of number series in an array

If my array looks like [0, 2, 4, 10, 10, 10, 10, 2, 5, 3, 2, 10, 10, 5, 7, 4, 10, 10, 10, 10] How do I determine the number of times a sequence of 10's occurs with at least 3 repetitions. In this example, the output would be 2 because there are 2 s ...

Callbacks for AJAX responses that are asynchronous

After using AJAX in a limited and straightforward manner for some time, I find myself currently debugging a web application that heavily relies on JavaScript and JQuery for client-side coding. One issue that has caught my attention is the possibility of mu ...

Tips for Implementing Error Handling in Angular using Sweetalert2

On this code snippet, I have implemented a delete confirmation popup and now I am looking to incorporate error handling in case the data is not deleted successfully. confirmPopUp(){ Swal.fire({ title: 'Are You Sure?', text: 'Deleti ...

Deploying Node.js on Heroku

After deploying a test app using node.js on Heroku, I checked the Heroku log file and it seems like everything is running smoothly. 2017-02-09T12:25:56.034718+00:00 heroku[web.1]: Starting process with command `npm start` 2017-02-09T12:25:58.77986 ...

The package-lock file may vary depending on the npm version being used

I am experimenting with a new typescript react app that was created using CRA. I am running @6.4.1 on one PC and an older version on another. Interestingly, the newer version installs dependencies with an older version instead of the expected new one. ...

Display React component when clicked

As a newcomer to the world of React, I find myself intrigued by its potential. However, I am still in the process of grasping the fundamental concepts and would greatly appreciate any explanations provided. My goal is to display an 'About' compo ...

Utilizing Gson to deserialize a list of Java objects containing new elements with missing IDs

Within my tree structure consisting of ol and li elements, I am dynamically adding new elements. However, when attempting to deserialize them into a Java object, an error occurs: SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/ ...

Guide to converting an arraylist of custom objects into JSON using JavaScript

I have a List returned to the jag. It is of type (java.util.List) and I need to print it as a json. var storeForum = Packages.org.wso2.carbon.forum.registry.RegistryForumManager; var forum = new storeForum(); var start = request.getParameter(&a ...

Display a different string from an array at random, revealing each one gradually with a time delay

Looking for a way to display strings randomly without replacing one with another? I need a script that can shuffle words in a list and present them in a random order, with the ability to adjust the delay between each word. If you have a more effective code ...

Iterate through an array and update the innerHTML content for each value contained in the array

Looking to replace a specific word that keeps appearing on my website, I experimented with various functions and stumbled upon this method that seems to work: document.getElementsByClassName("label")[0].innerHTML = document.getElementsByClassName ...

Creating a nested JSON file dynamically in Angular: A step-by-step guide

I am looking to dynamically generate a nested JSON file within an Angular project. The data will be extracted from another JSON file, with two nested loops used to read the information. Below is an example of the initial JSON file structure: { "data": [ ...

Can you explain the concept of System.register in a JavaScript file?

Can you explain the purpose of System.register in a JS file when utilizing directives in Angular 2? ...

Tips on sorting items in React

Hey there, I'm a beginner at react and currently working on my first ecommerce website. My main concern is about filtering products by size. I'm struggling with the logic behind it. Any help or guidance would be greatly appreciated. I also attem ...

Appium for testing the functionality of Android hybrid mobile apps

Exploring the realm of test automation, I decided to tackle a basic Hybrid Mobile Application built on Cordova and running on Android. My weapon of choice? Appium. Drawing inspiration from a helpful tutorial video, I kicked off my journey by downloading an ...

What is the best way to specify a type for an object without altering its underlying implicit type?

Suppose we have a scenario where an interface/type is defined as follows: interface ITest { abc: string[] } and then it is assigned to an object like this: const obj: ITest = { abc: ["x", "y", "z"] } We then attempt to create a type based on the valu ...

Setting up Jest

I'm currently attempting to integrate the Jest Testing framework into my React Native project. Unfortunately, I am encountering an error message: Failed to retrieve mock metadata: /Users/me/Documents/Development/project/node_modules/global/window.js ...

Accessing the background page of a Chrome extension while it's in operation

I am in the process of developing my first chrome extension that allows youtube.com/tv to run in the background so it can be accessed easily on a phone or tablet. Everything is working fine, except for the fact that if I want to watch the video and not j ...

Preventing request interceptors from altering the request header value in Node.js

I am currently using the http-proxy-middleware to set up a proxy, and it is working smoothly. However, before I apply app.use('/', proxy_options);, I am attempting to intercept my request and update the request header. Unfortunately, the changes ...

Swapping out the values in the existing array with the updated one

My challenge involves working with an interface - //INTERFACE - public interface IntSequence { int length(); int get(int index); void set(int index, int value); IntSequence subSequence(int index, int size); } In addition to the inter ...

Revealing private and protected Typescript members within Angular 1.x's view

When integrating TS and Angular, I've noticed that everything in my controller is accessible from the view. For example, myPrivate will be visible on $ctrl. class MyController extends BaseController implements SomeInterface { private myPrivate: s ...