Isha
Isha

Reputation: 95

Accessing model variable from gsp

I have a table Employee in the db with columns like say emp id, emp name and emp address and emp phone number(This field can be more than one and is a unique id) and according to the no. of phone number's provided , the no. of row's for that emp id can increase as well. Table something like the below:

Emp Id  Emp Name    Emp Address     Emp Phone
10001   Jack        abc             70102938
10001   Jack        abc             39876538
10002   Jim         xyz             23492020

I have my gsp in which I would like to display a particular panel only if emp phone is present or else it should be hidden completely, something like

<div class="subbody  ${someBean?.aCondition ? 'shownItem':'hiddenItem'}">

with the css

.shownItem{
        display: block;
    }
    .hiddenItem{
        display: none;
    }

What I am not getting is how do I write that condition in the gsp ${someBean?.aCondition ?

Upvotes: 0

Views: 906

Answers (2)

PhilMr
PhilMr

Reputation: 495

Assuming you have passed your someBean correctly from your controller, and assuming the property of that Domain object is called empPhone something like this should work:

<div class="subbody ${(someBean?.empPhone != null) ? 'shownItem':'hiddenItem'}">

As a side note I think you should consider refactoring your domain model and normalize your database by creating a Phone domain object that is linked to Employee via a one-to-many relationship.

Upvotes: 0

Zoidberg
Zoidberg

Reputation: 571

You could use <g:if> instead:

<g:if test="${someBean?.aCondition}">
     Whatever you want to display
</g:if>

http://grails.github.io/grails-doc/3.0.x/ref/Tags/if.html

Upvotes: 2

Related Questions