How can I dynamically generate a table using data from my database?
I am very new to this, so please be kind. I have a table 'users' in my database and each user is part of a team. What I was trying to do is to display all users in different tables, based on their team name.
This is the code in my UserService:
public List<User> getUsersFromFirstTeam()
List<User> usersFromFirstTeam = userRepository.findByTeam(FIRST_TEAM);
return usersFromFirstTeam;
public List<User> getUsersFromSecondTeam()
List<User> usersFromSecondTeam = userRepository.findByTeam(SECOND_TEAM);
return usersFromSecondTeam;
This is the code in my UserController:
@Autowired
private UserService userService;
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement(Model firstTeamModel, Model secondTeamModel)
ModelAndView modelAndView = new ModelAndView();
firstTeamModel.addAttribute("firstTeamUsers", userService.getUsersFromFirstTeam());
secondTeamModel.addAttribute("secondTeamUsers", userService.getUsersFromSecondTeam());
modelAndView.setViewName("userManagement");
return modelAndView;
And finally this is my .jsp page that is displayed:
<body>
<div class = "tab" style="width: 250px">
<button class ="tablinks" onclick = "openTable(event, 'FirstTeam')">First
Team</button>
<button class ="tablinks" onclick = "openTable(event, 'SecondTeam')">Second
Team</button>
</div>
<div id="FirstTeam" class="tabcontent">
<h3>First team members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$firstTeamUsers" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div id="SecondTeam" class="tabcontent">
<h3>Secondteam members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$secondTeamUsers" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
So, this code works and does exactly what I want. It creates two tabs 'First Team' and 'Second Team' and when I click on each tab it displays the table with sorted users by team name. My problem is that I want to do this whole thing without hardcoding the team's name and by generating the divs dinamically so that it won't matter how many teams will be in the future or what are their names. Any ideas on how I can achieve that?
Thank you in advance for your time and help!
sql-server database jsp spring-boot spring-data
add a comment |
I am very new to this, so please be kind. I have a table 'users' in my database and each user is part of a team. What I was trying to do is to display all users in different tables, based on their team name.
This is the code in my UserService:
public List<User> getUsersFromFirstTeam()
List<User> usersFromFirstTeam = userRepository.findByTeam(FIRST_TEAM);
return usersFromFirstTeam;
public List<User> getUsersFromSecondTeam()
List<User> usersFromSecondTeam = userRepository.findByTeam(SECOND_TEAM);
return usersFromSecondTeam;
This is the code in my UserController:
@Autowired
private UserService userService;
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement(Model firstTeamModel, Model secondTeamModel)
ModelAndView modelAndView = new ModelAndView();
firstTeamModel.addAttribute("firstTeamUsers", userService.getUsersFromFirstTeam());
secondTeamModel.addAttribute("secondTeamUsers", userService.getUsersFromSecondTeam());
modelAndView.setViewName("userManagement");
return modelAndView;
And finally this is my .jsp page that is displayed:
<body>
<div class = "tab" style="width: 250px">
<button class ="tablinks" onclick = "openTable(event, 'FirstTeam')">First
Team</button>
<button class ="tablinks" onclick = "openTable(event, 'SecondTeam')">Second
Team</button>
</div>
<div id="FirstTeam" class="tabcontent">
<h3>First team members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$firstTeamUsers" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div id="SecondTeam" class="tabcontent">
<h3>Secondteam members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$secondTeamUsers" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
So, this code works and does exactly what I want. It creates two tabs 'First Team' and 'Second Team' and when I click on each tab it displays the table with sorted users by team name. My problem is that I want to do this whole thing without hardcoding the team's name and by generating the divs dinamically so that it won't matter how many teams will be in the future or what are their names. Any ideas on how I can achieve that?
Thank you in advance for your time and help!
sql-server database jsp spring-boot spring-data
add a comment |
I am very new to this, so please be kind. I have a table 'users' in my database and each user is part of a team. What I was trying to do is to display all users in different tables, based on their team name.
This is the code in my UserService:
public List<User> getUsersFromFirstTeam()
List<User> usersFromFirstTeam = userRepository.findByTeam(FIRST_TEAM);
return usersFromFirstTeam;
public List<User> getUsersFromSecondTeam()
List<User> usersFromSecondTeam = userRepository.findByTeam(SECOND_TEAM);
return usersFromSecondTeam;
This is the code in my UserController:
@Autowired
private UserService userService;
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement(Model firstTeamModel, Model secondTeamModel)
ModelAndView modelAndView = new ModelAndView();
firstTeamModel.addAttribute("firstTeamUsers", userService.getUsersFromFirstTeam());
secondTeamModel.addAttribute("secondTeamUsers", userService.getUsersFromSecondTeam());
modelAndView.setViewName("userManagement");
return modelAndView;
And finally this is my .jsp page that is displayed:
<body>
<div class = "tab" style="width: 250px">
<button class ="tablinks" onclick = "openTable(event, 'FirstTeam')">First
Team</button>
<button class ="tablinks" onclick = "openTable(event, 'SecondTeam')">Second
Team</button>
</div>
<div id="FirstTeam" class="tabcontent">
<h3>First team members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$firstTeamUsers" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div id="SecondTeam" class="tabcontent">
<h3>Secondteam members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$secondTeamUsers" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
So, this code works and does exactly what I want. It creates two tabs 'First Team' and 'Second Team' and when I click on each tab it displays the table with sorted users by team name. My problem is that I want to do this whole thing without hardcoding the team's name and by generating the divs dinamically so that it won't matter how many teams will be in the future or what are their names. Any ideas on how I can achieve that?
Thank you in advance for your time and help!
sql-server database jsp spring-boot spring-data
I am very new to this, so please be kind. I have a table 'users' in my database and each user is part of a team. What I was trying to do is to display all users in different tables, based on their team name.
This is the code in my UserService:
public List<User> getUsersFromFirstTeam()
List<User> usersFromFirstTeam = userRepository.findByTeam(FIRST_TEAM);
return usersFromFirstTeam;
public List<User> getUsersFromSecondTeam()
List<User> usersFromSecondTeam = userRepository.findByTeam(SECOND_TEAM);
return usersFromSecondTeam;
This is the code in my UserController:
@Autowired
private UserService userService;
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement(Model firstTeamModel, Model secondTeamModel)
ModelAndView modelAndView = new ModelAndView();
firstTeamModel.addAttribute("firstTeamUsers", userService.getUsersFromFirstTeam());
secondTeamModel.addAttribute("secondTeamUsers", userService.getUsersFromSecondTeam());
modelAndView.setViewName("userManagement");
return modelAndView;
And finally this is my .jsp page that is displayed:
<body>
<div class = "tab" style="width: 250px">
<button class ="tablinks" onclick = "openTable(event, 'FirstTeam')">First
Team</button>
<button class ="tablinks" onclick = "openTable(event, 'SecondTeam')">Second
Team</button>
</div>
<div id="FirstTeam" class="tabcontent">
<h3>First team members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$firstTeamUsers" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div id="SecondTeam" class="tabcontent">
<h3>Secondteam members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$secondTeamUsers" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
So, this code works and does exactly what I want. It creates two tabs 'First Team' and 'Second Team' and when I click on each tab it displays the table with sorted users by team name. My problem is that I want to do this whole thing without hardcoding the team's name and by generating the divs dinamically so that it won't matter how many teams will be in the future or what are their names. Any ideas on how I can achieve that?
Thank you in advance for your time and help!
sql-server database jsp spring-boot spring-data
sql-server database jsp spring-boot spring-data
edited Nov 12 '18 at 15:06
Ioana
asked Nov 12 '18 at 14:46
IoanaIoana
136
136
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Create a Map in your controller and add that to the model:
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement()
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
usersMap.put("First Team", userService.getUsersForTeam(FIRST_TEAM));
usersMap.put("Second Team", userService.getUsersForTeam(SECOND_TEAM));
modelAndView.add("usersMap", usersMap)
modelAndView.setViewName("userManagement");
return modelAndView;
In the JSP you can iterate the Map entries, something like:
<c:forEach items="$usersMap" var="entry">
<div id="$entry.key" class="tabcontent">
<h3>$entry.key Members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$entry.value" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</c:forEach>
You'd still need to change the controller code for each new type so you can clean that up by fetching a list of teams dynamically so it would look something like the below. Any new team in the database should then be automatically reflected in the view with no further code change.
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
List<String> teamNames = //db lookup on distinct team names
for(String name : teamNames)
usersMap.put(name, userService.getUsersForTeam(name));
modelAndView.add("usersMap", usersMap);
Thank you so much !! I'll let you know how it worked out .
– Ioana
Nov 12 '18 at 17:18
I tried exactly how you suggested and it worked very well for me. Thank you !!
– Ioana
Nov 13 '18 at 10:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264587%2fhow-can-i-dynamically-generate-a-table-using-data-from-my-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Create a Map in your controller and add that to the model:
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement()
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
usersMap.put("First Team", userService.getUsersForTeam(FIRST_TEAM));
usersMap.put("Second Team", userService.getUsersForTeam(SECOND_TEAM));
modelAndView.add("usersMap", usersMap)
modelAndView.setViewName("userManagement");
return modelAndView;
In the JSP you can iterate the Map entries, something like:
<c:forEach items="$usersMap" var="entry">
<div id="$entry.key" class="tabcontent">
<h3>$entry.key Members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$entry.value" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</c:forEach>
You'd still need to change the controller code for each new type so you can clean that up by fetching a list of teams dynamically so it would look something like the below. Any new team in the database should then be automatically reflected in the view with no further code change.
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
List<String> teamNames = //db lookup on distinct team names
for(String name : teamNames)
usersMap.put(name, userService.getUsersForTeam(name));
modelAndView.add("usersMap", usersMap);
Thank you so much !! I'll let you know how it worked out .
– Ioana
Nov 12 '18 at 17:18
I tried exactly how you suggested and it worked very well for me. Thank you !!
– Ioana
Nov 13 '18 at 10:16
add a comment |
Create a Map in your controller and add that to the model:
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement()
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
usersMap.put("First Team", userService.getUsersForTeam(FIRST_TEAM));
usersMap.put("Second Team", userService.getUsersForTeam(SECOND_TEAM));
modelAndView.add("usersMap", usersMap)
modelAndView.setViewName("userManagement");
return modelAndView;
In the JSP you can iterate the Map entries, something like:
<c:forEach items="$usersMap" var="entry">
<div id="$entry.key" class="tabcontent">
<h3>$entry.key Members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$entry.value" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</c:forEach>
You'd still need to change the controller code for each new type so you can clean that up by fetching a list of teams dynamically so it would look something like the below. Any new team in the database should then be automatically reflected in the view with no further code change.
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
List<String> teamNames = //db lookup on distinct team names
for(String name : teamNames)
usersMap.put(name, userService.getUsersForTeam(name));
modelAndView.add("usersMap", usersMap);
Thank you so much !! I'll let you know how it worked out .
– Ioana
Nov 12 '18 at 17:18
I tried exactly how you suggested and it worked very well for me. Thank you !!
– Ioana
Nov 13 '18 at 10:16
add a comment |
Create a Map in your controller and add that to the model:
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement()
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
usersMap.put("First Team", userService.getUsersForTeam(FIRST_TEAM));
usersMap.put("Second Team", userService.getUsersForTeam(SECOND_TEAM));
modelAndView.add("usersMap", usersMap)
modelAndView.setViewName("userManagement");
return modelAndView;
In the JSP you can iterate the Map entries, something like:
<c:forEach items="$usersMap" var="entry">
<div id="$entry.key" class="tabcontent">
<h3>$entry.key Members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$entry.value" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</c:forEach>
You'd still need to change the controller code for each new type so you can clean that up by fetching a list of teams dynamically so it would look something like the below. Any new team in the database should then be automatically reflected in the view with no further code change.
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
List<String> teamNames = //db lookup on distinct team names
for(String name : teamNames)
usersMap.put(name, userService.getUsersForTeam(name));
modelAndView.add("usersMap", usersMap);
Create a Map in your controller and add that to the model:
@GetMapping(value = "/admin/usermanagement")
public ModelAndView userManagement()
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
usersMap.put("First Team", userService.getUsersForTeam(FIRST_TEAM));
usersMap.put("Second Team", userService.getUsersForTeam(SECOND_TEAM));
modelAndView.add("usersMap", usersMap)
modelAndView.setViewName("userManagement");
return modelAndView;
In the JSP you can iterate the Map entries, something like:
<c:forEach items="$usersMap" var="entry">
<div id="$entry.key" class="tabcontent">
<h3>$entry.key Members</h3>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach items="$entry.value" var ="user">
<tr>
<td>$user.username</td>
<td>$user.firstName</td>
<td>$user.lastName</td>
<td>$user.email</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</c:forEach>
You'd still need to change the controller code for each new type so you can clean that up by fetching a list of teams dynamically so it would look something like the below. Any new team in the database should then be automatically reflected in the view with no further code change.
ModelAndView modelAndView = new ModelAndView();
Map<String, List<User>> usersMap = new LinkedHashMap<>();
List<String> teamNames = //db lookup on distinct team names
for(String name : teamNames)
usersMap.put(name, userService.getUsersForTeam(name));
modelAndView.add("usersMap", usersMap);
edited Nov 12 '18 at 15:37
answered Nov 12 '18 at 15:24
Alan HayAlan Hay
15.8k22872
15.8k22872
Thank you so much !! I'll let you know how it worked out .
– Ioana
Nov 12 '18 at 17:18
I tried exactly how you suggested and it worked very well for me. Thank you !!
– Ioana
Nov 13 '18 at 10:16
add a comment |
Thank you so much !! I'll let you know how it worked out .
– Ioana
Nov 12 '18 at 17:18
I tried exactly how you suggested and it worked very well for me. Thank you !!
– Ioana
Nov 13 '18 at 10:16
Thank you so much !! I'll let you know how it worked out .
– Ioana
Nov 12 '18 at 17:18
Thank you so much !! I'll let you know how it worked out .
– Ioana
Nov 12 '18 at 17:18
I tried exactly how you suggested and it worked very well for me. Thank you !!
– Ioana
Nov 13 '18 at 10:16
I tried exactly how you suggested and it worked very well for me. Thank you !!
– Ioana
Nov 13 '18 at 10:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264587%2fhow-can-i-dynamically-generate-a-table-using-data-from-my-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown