0

I am self-learning JQuery and UI.

I have two entities:

  1. AppUser
@Entity 
@Table(name = "app_user")
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(...)
private List<AppRole> roles;
  1. AppRole
@Entity
@Table(name = "app_role")
private Integer id;
private String name;

On JSP I can get the role names using this:

<c:forEach items="${user.roles}" var="list">${list.name}</c:forEach>

What I am trying to achieve is - To display the user details with the roles in a bootstrap modal.

I tried the following as suggested here (any many other).

for (var i = 0; i < userroles.length; i++) {
    $('#roleslist').append(
    '<option value="' + userroles[i] + '">'
    + userroles[i]
    + '</option>');
}

This give me close to what I am trying to achieve but it gives me the weird looking object list in the dropdown like below:

[com.spring.boot.rocks.model.AppRole[ id=1 ], com.spring.boot.rocks.model.AppRole[ id=2 ], com.spring.boot.rocks.model.AppRole[ id=3 ]]

Question: How do I get the names of roles using JQuery for above scenario? Is there a way I can display/populate the drop-down with role-id and role-name?

I know there is a big disconnect somewhere but any clue on this will be helpful.

Afia
  • 683
  • 5
  • 17
Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
  • So you have a javascript array `userroles`. What does each element in that array contain? – Roamer-1888 Dec 12 '19 at 20:53
  • Is the data you want extractable from the data you have? If not, then you need to go back to where the array was composed (server side?) – Roamer-1888 Dec 12 '19 at 21:03
  • On my jsp ${user.roles} contains the list of roles. How do I get it in JQuery? Also userroles is nothing but the data atribute for BootStrap Modal like this: data-userroles="${user.roles}" – Ajay Kumar Dec 12 '19 at 21:10
  • "How do I get it in JQuery?" I don't know. What is the scenario here, page build or data acquisition by AJAX? – Roamer-1888 Dec 12 '19 at 21:20
  • What can be the easy way to achieve using JQuery as it is done in jsp (${list.name}) using - c:forEach? Using AJAX only? If that is the case its fine, I will do that. Was wondering if there is any straight forward way in JQuery as I already have the list of Objects. Thanks. – Ajay Kumar Dec 12 '19 at 21:36
  • There seems to be a fundamental misunderstanding here. In order to use jQuery, the data must first be available in Javascript. The jQuery you have written looks OK and will do the job providing the javascript `userroles` array contains the necessary data. The problem seems to be how to make your JSP `${user.roles}` data available as a javascript array. I'm sure that is well documented in Stackoverflow and elsewhere. – Roamer-1888 Dec 12 '19 at 21:45
  • We are almost there. Can you please share that SO link here. Really appreciate it. – Ajay Kumar Dec 12 '19 at 22:04
  • I would need to search for it. I'm sure you are equally as capable. – Roamer-1888 Dec 12 '19 at 22:05
  • Sure. Will search for it. For now, I settled using .replace("abc", "") to filter out the unwanted text and got what I needed. Thanks. – Ajay Kumar Dec 12 '19 at 22:21

0 Answers0