Reputation: 13
I'm doing small social application in spring mvc with jdbc. I have an admin panel, with option to view all users and to manage with them. So in the table with users details I have a column with "enabled" status. I tried to write a loop that will write href in next column, a href to disable user if his enabled status is 1, and href to enable user if his enabled status is 0, but I have failed.
Controller of viewing users:
@RequestMapping(value="/admin/view-users", method = RequestMethod.GET)
public ModelAndView viewUsers()
{
List<User> listUser =dao.getUsersList();
return new ModelAndView("view-users","listUser",listUser);
}
Snippet of .jsp code to display all users in table.
<c:forEach var="aUser" items="${listUser}">
<tr>
<td>${aUser.id}</td>
<td>${aUser.username}</td>
<td>${aUser.nickname}</td>
<td>${aUser.email}</td>
<td>${aUser.country}</td>
<td>${aUser.role}</td>
<td id="isEnabled">${aUser.enabled}</td>
<td id="enableOrDisableLink">
</td>
</tr>
</c:forEach>
I tried to do it with something like this:
<script>
if( document.getElementById("isEnabled").value=="1" )
{
document.getElementById("enableOrDisableLink").innerHTML = "<a
href='disableUser/${aUser.id}'>Disable Account</a>";
}
else
{
document.getElementById("enableOrDisableLink").innerHTML = "<a
href='enableUser/${aUser.id}'>Enable Account</a>";
}
</script>
But I got something like this on the picture.
Upvotes: 0
Views: 1207
Reputation: 13
Works great, just had to change to
test = "${aUser.enabled == 1}" , because enabled isn't boolean type.
Upvotes: 0
Reputation: 1307
Loop and ID attribute does not go very well together. I would ditch the whole JS part and use <c:choose><c:test ><c:otherwise>
to do the condition.
Upvotes: 1
Reputation: 1233
you can do this:
<td id="enableOrDisableLink">
<c:if test="${aUser.enabled}">
<a href='disableUser/${aUser.id}'>Disable Account</a>
</c:if>
<c:if test="${not aUser.enabled}">
<a href='enableUser/${aUser.id}'>Enable Account</a>
</c:if>
</td>
Upvotes: 1