So I have the ability to list all users in from a database in a view, but I would like to list them alphabetically and by tab. For example, I'll have a tab for "All", "A", "B", "C", etc. What I would like to happen is that when the user clicks the "A" tab, it displays only the users with last name starting with "A".
My idea for doing this is such:
<ul class="tabs">
<li><a href="#A">A</a></li>
<li><a href="#B">B</a></li>
<li><a href="#C">C</a></li>
...
</ul>
<div class="tabcontents">
<div id="All">
<nav>
<ul>
<g:each var="user" in="${users}" ID="">
<li><g:link controller="user" action="profile" id="${user.employeeNumber}">${user.lastName}, ${user.firstName}</g:link></li>
</g:each>
</ul>
</nav>
</div>
<div id="A">
<nav>
<ul>
<g:each var="user" in="${users}" ID="A">
<li><g:link controller="user" action="profile" id="${user.employeeNumber}">${user.lastName}, ${user.firstName}</g:link></li>
</g:each>
</ul>
</nav>
</div>
...
</div>
with the back end UserController containing something like
class UserController {
def index() {
def users = UserFeed.createCriteria().list {
eq('lastName[0]', '${ID}')
}
render(view: 'index', model: [users: users])
}
def search() {
}
def results(String lastName) {
def users = UserFeed.where {
lastName =~ "%${lastName}%"
}.list()
return [ users: users,
term: params.lastName,
totalUsers: UserFeed.count() ]
}
def profile(String id) {
def user = UserFeed.findByEmployeeNumber(id)
render(view: 'profile', model: [user: user])
}
}
So the idea being, when the user clicks a tab, the letter of the tab is sent to the controller where the first letter of the last name in the database is compared and added to the list if it matches. Is this possible? Is there a slick way to do this?
Thanks in advance!
Edit: I can brute force this, I just would like to avoid having 26 groups of users for each letter of the alphabet.
Edit 2: I've tried something like this, but to no avail
<div id="A">
<nav>
<ul>
<g:each var="user" in="${users}">
<g:if test="${user.lastName.charAt(0)} == 'A'">
<li style="padding-left:5px;"><g:link controller="user" action="profile" id="${user.employeeNumber}">${user.lastName}, ${user.firstName}</g:link></li>
</g:if>
</g:each>
</ul>
</nav>
</div>
I've verified that user.lastName.charAt(0) prints out the correct letter, but for whatever reason, the g:if
statement doesn't limit the list to just A's. Any ideas why?